def files_message_lib(self, uuid): """04返回文件""" files = Files.by_uuid(uuid) try: username = files.users[0].username except IndexError: username = files.files_users_del[0].username return files, username
def download_files_qiniu_lib(self, uuid): """12 从下载文件""" if uuid == '': return {'status': False, 'msg': '没有文件ID'} old_file = Files.by_uuid(uuid) if old_file is None: return {'status': False, 'msg': '文件不存在'} qiniu_url = 'http://p7bj6aatj.bkt.clouddn.com/{}'.format(uuid) url = down_qiniu_file(qiniu_url) print(url) return {'status': True, 'data': url}
def file_finaly_del_libs(self, uuid): """07彻底删除文件""" file = Files.by_uuid(uuid) if file: try: user = self.current_user except Exception as e: print "nonexitence current_user" # 从回收站列表删除 user.users_files_del.remove(file) self.db.add(user) self.db.commit() return {'status': True, 'msg': '彻底删除成功'}
def save_qiniu_file(self, upload_file): """ 11 保存单个文件到七牛""" files_ext = upload_file['filename'].split('.')[-1] files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt'] if files_ext not in files_type: return {'status': False, 'msg': '文件格式不正确', 'data': ''} # 'sefaegaer.txt' 'sejfiajelgaeirf.jpg' file_content = upload_file['body'] old_file = Files.file_is_existed(file_content) if old_file is not None: file_path = 'http://p7bj6aatj.bkt.clouddn.com/' + old_file.uuid return {'status': True, 'msg': '文件保存成功(其实文件在硬盘上)', 'data': file_path} # 上传到七牛 print("=" * 80) ret, info = upload_qiniu_file_content(file_content) print("*" * 80) print ret print info if info.status_code != 200: return {'status': False, 'msg': '文件上传到七牛不正确', 'data': ''} files = Files() file_name = upload_file['filename'] files.filename = file_name files.uuid = ret # 保存的七牛返回到的文件名 files.content_length = len(file_content) files.content_type = upload_file['content_type'] files.update_time = datetime.now() files.file_hash = upload_file['body'] # files.user_id = self.current_user.id files.users.append(self.current_user) self.db.add(files) self.db.commit() file_path = 'http://p7bj6aatj.bkt.clouddn.com/' + files.uuid return {'status': True, 'msg': '文件保存成功', 'data': file_path}
def file_rev_libs(self, uuid): """08恢复文件""" file = Files.by_uuid(uuid) if file: try: user = self.current_user except Exception as e: print "nonexitence current_user" # 添加到文件列表添加 user.files.append(file) # 回收站列表中移除 user.users_files_del.remove(file) self.db.add(user) self.db.commit() return {'status': True, 'msg': '恢复成功'}
def save_file(self, upload_file): """ 03 保存单个文件""" files_ext = upload_file['filename'].split('.')[-1] files_type = ['jpg', 'bmp', 'png', 'mp4', 'ogg', 'mp3', 'txt'] if files_ext not in files_type: return {'status': False, 'msg': '文件格式不正确', 'data': ''} uuidname = str(uuid4()) + '.%s' % files_ext # 'sefaegaer.txt' 'sejfiajelgaeirf.jpg' file_content = upload_file['body'] old_file = Files.file_is_existed(file_content) if old_file is not None: file_path = 'http://127.0.0.1:9000/images/' + old_file.uuid return {'status': True, 'msg': '文件保存成功(其实文件在硬盘上)', 'data': file_path} dirname = "images" if not os.path.exists(dirname): os.mkdir(dirname) url = 'images/' + uuidname with open(url, 'wb') as f: f.write(file_content) file_name = upload_file['filename'] files = Files() files.filename = file_name files.uuid = uuidname files.content_length = len(file_content) files.content_type = upload_file['content_type'] files.update_time = datetime.now() files.file_hash = upload_file['body'] # files.user_id = self.current_user.id files.users.append(self.current_user) self.db.add(files) self.db.commit() file_path = 'http://127.0.0.1:9000/images/' + files.uuid return {'status': True, 'msg': '文件保存成功', 'data': file_path}
def file_del_libs(self, uuid): """06删除文件""" file = Files.by_uuid(uuid) if file: try: user = self.current_user except Exception: print "nonexitence current_user" user.files.remove(file) # 从文件列表删除 user.users_files_del.append(file) # 添加到回收站列表 self.db.add(user) self.db.commit() # print "files_del type is :%s"% type(user.users_files_del) # print "files_del is :%s"% user.users_files_del return {'status': True, 'msg': '删除到回收站成功'}