def save_qiniu_file(self, upload_file): """保存单个文件""" 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': ''} files_content = upload_file['body'] old_file = Files.file_is_existed(files_content) if old_file is not None: file_path = 'http://192.168.169.138:8000/images?uuid=' + old_file.uuid # 这里要改成七牛云的链接 file_id = Files.by_uuid(old_file.uuid).id # 老文件的uuid # 连表查询 查询删除的表中是否有老文件的id isExist = self.db.query( Files.id, DelFilesToUser.files_id).join(DelFilesToUser).filter_by( files_id=file_id).first() # 如果存在则从回收站中恢复 if isExist != None: files = Files.by_uuid(old_file.uuid) files.files_users_del.remove(self.current_user) files.files_users.append(self.current_user) self.db.add(files) self.db.commit() return {'status': True, 'msg': '文件上传成功(硬盘中存在文件)', 'data': file_path} ret, info = upload_qiniu_file_content(files_content) print ret print info if info.status_code != 200: return {'status': False, 'msg': '文件上传到七牛失败', 'data': ''} file_name = upload_file['filename'] files = Files() files.uuid = ret # 保存的七牛返回的文件名 files.filename = file_name files.content_length = len(files_content) files.content_type = upload_file['content_type'] files.updata_time = datetime.now() files.file_hash = upload_file['body'] files.files_users.append(self.current_user) files.user_id = self.current_user.id self.db.add(files) self.db.commit() file_path = 'http://192.168.169.138:8000/images/' + files.uuid # 这里要改成七牛云的链接 return {'status': True, 'msg': '文件上传到七牛成功', 'data': file_path}
def save_file(self, upload_file): """保存单个文件""" 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 file_content = upload_file['body'] old_file = Files.file_is_existed(file_content) if old_file is not None: file_path = 'http://192.168.169.138:8000/images?uuid=' + old_file.uuid file_id = Files.by_uuid(old_file.uuid).id # 老文件的uuid # 连表查询 查询删除的表中是否有老文件的id isExist = self.db.query( Files.id, DelFilesToUser.files_id).join(DelFilesToUser).filter_by( files_id=file_id).first() # 如果存在则从回收站中恢复 if isExist != None: files = Files.by_uuid(old_file.uuid) files.files_users_del.remove(self.current_user) files.files_users.append(self.current_user) self.db.add(files) self.db.commit() return {'status': True, 'msg': '文件上传成功(硬盘中存在文件)', 'data': file_path} url = 'files/' + uuidname with open(url, 'wb') as f: f.write(file_content) file_name = upload_file['filename'] files = Files() files.uuid = uuidname files.filename = file_name files.content_length = len(file_content) files.content_type = upload_file['content_type'] files.updata_time = datetime.now() files.file_hash = upload_file['body'] files.files_users.append(self.current_user) files.user_id = self.current_user.id self.db.add(files) self.db.commit() file_path = 'http://192.168.169.138:8000/images/' + files.uuid return {'status': True, 'msg': '文件上传成功', 'data': file_path}