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 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}