def read_all(): """Return the data of all records.""" file = File.query.order_by(File.id).all() file_schema = FileSchema(many=True) data = file_schema.dump(file) return data
def read_md5(md5): """Return the data of the file that has the MD5 hash.""" file = (File.query.filter(File.md5 == md5).one_or_none()) if file is not None: file_schema = FileSchema() data = file_schema.dump(file) return data else: abort(404, "File with the MD5 hash {md5} not found".format(md5=md5))
def read_sha1(sha1): """Return the data of the file that has the SHA1 hash.""" file = (File.query.filter(File.sha1 == sha1).one_or_none()) if file is not None: file_schema = FileSchema() data = file_schema.dump(file) return data else: abort(404, "File with the SHA1 hash {sha1} not found".format(sha1=sha1))
def create(file): # insert meta_data to web_service_db schema = FileSchema() new_file = schema.load(file, session=db.session) db.session.add(new_file) db.session.commit() data = schema.dump(new_file) return data, 201
def update_md5(md5, file): """Update the data of the file that has the MD5 hash.""" update_file = File.query.filter(File.md5 == md5).one_or_none() if update_file is not None: schema = FileSchema() up = schema.load(file, session=db.session) up.md5 = update_file.md5 db.session.merge(up) db.session.commit() data = schema.dump(update_file) return data, 200 else: abort(404, f"File not found for MD5 has: {md5}")
def update_sha1(sha1, file): """Update the data of the file that has the SHA1 hash.""" update_file = File.query.filter(File.sha1 == sha1).one_or_none() if update_file is not None: schema = FileSchema() up = schema.load(file, session=db.session) up.sha1 = update_file.sha1 db.session.merge(up) db.session.commit() data = schema.dump(update_file) return data, 200 else: abort(404, f"File not found for SHA1 has: {sha1}")
def delete_md5(md5): """Delete the data of the file that has the MD5 hash.""" file = File.query.filter(File.md5 == md5).one_or_none() schema = FileSchema() result = schema.dump(file) if file is not None: filename = f"{result['file_name']}.{result['file_type']}" if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) folder = app.config['UPLOAD_FOLDER'] file_path = os.path.join(folder, filename) os.remove(file_path) db.session.delete(file) db.session.commit() return make_response(f"File with MD5 hash {md5} deleted.", 200) else: abort(404, f"File not found with MD5 hash: {md5}")
'{0}.{1}'.format(file.file_name, file.file_format), as_attachment=as_attachment) @file_bp.route('', methods=[Method.POST.value]) def file_upload(): """ 文件上传 :return: """ file = request.files.get('file') print(file.filename) if file.filename == '4.JPG': return render_json(MyResponse('上传成功', code='22')) return render_json(MyResponse('上传成功')) @file_bp.route('/md5', methods=[Method.POST.value]) @validated(FileSchema, only=FileSchema().only_md5_upload(), consumes=ContentType.JSON.value) def file_md5_upload(file): """ 文件md5上传 :param file: :return: """ print(file) if file.md5_id == '5d8d2a735876b2132fc4618dbf367c4c': return render_info(MyResponse('查询成功'), status=500) if file.md5_id == '47a7af5260e7d8d50c9b66374a63264e': return render_info(MyResponse('查询失败', '22')) return render_info(MyResponse('查询成功'))