|
@@ -1,13 +1,12 @@
|
|
|
import os
|
|
|
from flask import request, logging, send_file, jsonify
|
|
|
-from werkzeug.datastructures import CombinedMultiDict
|
|
|
from app import app
|
|
|
from .resp import success_resp, error_resp
|
|
|
import numpy as np
|
|
|
from .req import ReplaceForm, is_empty
|
|
|
import tools
|
|
|
import cv2
|
|
|
-from .file import get_upload_file_path, get_output_dir, file_url, get_output_file_path
|
|
|
+from .file import get_upload_file_path, get_output_dir, file_url, get_output_file_path, get_file_id
|
|
|
|
|
|
log = logging.create_logger(app)
|
|
|
|
|
@@ -48,7 +47,7 @@ def replace():
|
|
|
result = tools.replace(img_path=img_path, background=bg_path, save_dir=get_output_dir())
|
|
|
|
|
|
return jsonify({
|
|
|
- "fileId": form.file_id.data,
|
|
|
+ "fileId": get_file_id(result, True),
|
|
|
"url": file_url(result, True)
|
|
|
})
|
|
|
|
|
@@ -74,8 +73,10 @@ def resize():
|
|
|
|
|
|
file_path = get_upload_file_path(file_id)
|
|
|
|
|
|
+ is_get = request.method.upper() == "GET"
|
|
|
+
|
|
|
if os.path.exists(file_path) is not True:
|
|
|
- return "文件上传失败,请重新上传!", 400
|
|
|
+ return "文件上传失败,请重新上传!" if is_get else jsonify(error_resp("文件上传失败,请重新上传")), 400
|
|
|
|
|
|
out_file = get_output_file_path(file_id, "resize")
|
|
|
if reset is False and os.path.exists(out_file):
|
|
@@ -116,9 +117,20 @@ def resize():
|
|
|
|
|
|
if dst is not None:
|
|
|
cv2.imwrite(out_file, dst)
|
|
|
- return send_file(out_file, mimetype="image/png")
|
|
|
-
|
|
|
- return send_file(file_path, mimetype="image/png")
|
|
|
+ if is_get:
|
|
|
+ return send_file(out_file, mimetype="image/png")
|
|
|
+ else:
|
|
|
+ return jsonify(success_resp({
|
|
|
+ "fileId": get_file_id(out_file),
|
|
|
+ "url": file_url(out_file)
|
|
|
+ }))
|
|
|
+
|
|
|
+ if is_get:
|
|
|
+ return send_file(file_path, mimetype="image/png")
|
|
|
+ return jsonify(success_resp({
|
|
|
+ "fileId": get_file_id(file_path),
|
|
|
+ "url": file_url(file_path)
|
|
|
+ }))
|
|
|
|
|
|
|
|
|
def crop(img, rect: str, w, h):
|