Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_file():
"""保存表单上传文件到 GridFS
"""
if 'file' not in request.files or request.files['file'].filename == '':
raise NotFound()
id = mongo.save_file(request.files['file'].filename, request.files["file"])
_, ext = path.splitext(request.files['file'].filename)
return json_response(id='{}{}'.format(id, ext))
def file_response(id, download=False):
# 获取 GridFS 文件
try:
file = GridFS(mongo.db).get(id)
except NoFile:
raise NotFound()
# 将 GridFS 文件对象包装为一个 WSGI 文件对象
data = wrap_file(request.environ, file, buffer_size=1024 * 255)
# 创建一个 Flask Response 对象来响应文件内容
response = current_app.response_class(
data,
mimetype=file.content_type,
direct_passthrough=True,
)
# 设置内容长度响应头
response.content_length = file.length
# 设置内容最后修改时间 和 Etag 响应头,浏览器可根据这些信息来判断文件内容是否有更新
response.last_modified = file.upload_date
response.set_etag(file.md5)