uploads and downloads
uploads
python:3.6 flask:1.0.2
UPLOAD_PATH = "/users/alex/desktop/static/uploads"
DOWNLOAD_PATH = "/users/alex/desktop/static/downloads"
# post
def upload_file(*args, **kwargs):
"""
上传文件,兼写入库
:return:
"""
file = request.files["file"]
if not file:
result = formalReturn(40002, 'file can\'t be null', data=None)
if not file.filename.endswith("json"):
result = formalReturn(40002, 'file name must be json', data=None)
if file:
# 上传文件路径不存在就创建
upload_path = UPLOAD_PATH
if not os.path.exists(upload_path):
os.makedirs(upload_path)
# file.save() 时候确保汉字路径被保存
file_path = os.path.join(upload_path, file.filename)
if isinstance(file_path, bytes):
file_path = file_path.decode("utf-8")
try:
file.save(file_path) #保存到服务器位置
except Exception as e:
result = formalReturn(404, 'upload failed', data=str(e))
return jsonify(result), 200
file_name = file.filename
file_path = file_path
file_size = os.path.getsize(file_path) # 单位字节/1024
pre_name = file_name.split(".")[0] # 去掉后缀格式的文件名
try:
pass # 保存字段到数据库
result = formalReturn(201, 'upload success', data=None)
return jsonify(result), 201
except mongoengine.NotUniqueError as e:
result = formalReturn(40001, 'file already exists', data=str(e))
except Exception as e:
result = formalReturn(40004, 'unexpected error', data=str(e))
else:
result = formalReturn(404, 'please upload json file', data=None)
return jsonify(result), 200
downloads
python:3.6 flask:1.0.2
from flask import Flask, jsonify, request, abort
from flask import send_file, send_from_directory
# get methods
def export_file_for_pick(*args, **kwargs):
"""
:param args:
:param kwargs:
:return:
"""
task_id = request.args.get("task_id")
image_picks = ImagePick.objects.filter(task_id=task_id)
filename = "export_pick_{task_id}.json".format(task_id=task_id)
download_path = DOWNLOAD_PATH
if not os.path.exists(download_path):
os.makedirs(download_path, 0o777)
filepath = os.path.join(download_path, filename)
print(filepath)
with open(filepath, "w", encoding="utf-8") as f:
for pick in image_picks:
pick_item = {}
image_id = pick.image_id
pick_tag = pick.pick_tag
status = pick.status
image = Image.objects.filter(image_id=image_id).first()
url = image.url
pick_item["image_id"] = image_id
pick_item["url"] = url
if pick_tag:
pick_item["pick_tag"] = pick_tag
else:
pick_item["pick_tag"] = ""
pick_item["status"] = status
f.write(json.dumps(pick_item, ensure_ascii=False) + "\n")
try:
directory, filename = os.path.split(filepath)
return send_from_directory(directory, filename, as_attachment=True)
except Exception as e:
return jsonify({"code": 40004, "message": "download failed!", "data": str(e)})
def export_file_for_tag(*args, **kwargs):
"""
:param args:
:param kwargs:
:return:
"""
task_id = request.args.get("task_id")
image_annotations = ImageAnnotation.objects.filter(task_id=task_id)
filename = "export_tag_{task_id}.json".format(task_id=task_id)
download_path = DOWNLOAD_PATH
if not os.path.exists(download_path):
os.makedirs(download_path, 0o777)
filepath = os.path.join(download_path, filename)
print(filepath)
with open(filepath, "w", encoding="utf-8") as f:
for anno in image_annotations:
# print(anno)
anno_item = {}
image_id = anno.image_id
definition = anno.definition
first = anno.first
second = anno.second
is_skip = anno.is_skip
image = Image.objects.filter(image_id=image_id).first()
url = image.url
anno_item["image_id"] = image_id
anno_item["url"] = url
anno_item["is_skip"] = is_skip
if definition:
anno_item["definition"] = definition
else:
anno_item["definition"] = ""
if first:
anno_item["first"] = first
else:
anno_item["first"] = ""
if second:
anno_item["second"] = second
else:
anno_item["second"] = ""
f.write(json.dumps(anno_item, ensure_ascii=False) + "\n")
try:
directory, filename = os.path.split(filepath)
return send_from_directory(directory, filename, as_attachment=True)
except Exception as e:
return jsonify({"code": 40004, "message": "download failed!", "data": str(e)})
def export_file_for_pick(*args, **kwargs):
"""
:param args:
:param kwargs:
:return:
"""
task_id = request.args.get("task_id")
image_picks = ImagePick.objects.filter(task_id=task_id)
filename = "export_pick_{task_id}.json".format(task_id=task_id)
download_path = DOWNLOAD_PATH
if not os.path.exists(download_path):
os.makedirs(download_path, 0o777)
filepath = os.path.join(download_path, filename)
print(filepath)
with open(filepath, "w", encoding="utf-8") as f:
for pick in image_picks:
pick_item = {}
image_id = pick.image_id
pick_tag = pick.pick_tag
status = pick.status
image = Image.objects.filter(image_id=image_id).first()
url = image.url
pick_item["image_id"] = image_id
pick_item["url"] = url
if pick_tag:
pick_item["pick_tag"] = pick_tag
else:
pick_item["pick_tag"] = ""
pick_item["status"] = status
f.write(json.dumps(pick_item, ensure_ascii=False) + "\n")
try:
directory, filename = os.path.split(filepath)
return send_from_directory(directory, filename, as_attachment=True)
except Exception as e:
return jsonify({"code": 40004, "message": "download failed!", "data": str(e)})