fix: 视频工具响应改二进制流+标准Content-Disposition(修复multipart包裹导致的客户端文件损坏); 修cgi bool异常
This commit is contained in:
1 parent
0ec5624add
commit
6bc2120c05
2 files changed
+18
-8
No files matched your search
@@ -900,7 +900,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
|
env = {"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype, "CONTENT_LENGTH": str(length)}
|
||||||
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
|
fs = cgi.FieldStorage(fp=io.BytesIO(raw), environ=env, keep_blank_values=True)
|
||||||
fobj = fs["file"] if ("file" in fs.keys()) else None
|
fobj = fs["file"] if ("file" in fs.keys()) else None
|
||||||
if not fobj or not getattr(fobj, "filename", None):
|
if fobj is None or not fobj.filename:
|
||||||
self._reply(400, json.dumps({"code": 400, "message": "缺少视频文件"}, ensure_ascii=False).encode(), "application/json")
|
self._reply(400, json.dumps({"code": 400, "message": "缺少视频文件"}, ensure_ascii=False).encode(), "application/json")
|
||||||
return
|
return
|
||||||
fname = os.path.basename(fobj.filename or "video")
|
fname = os.path.basename(fobj.filename or "video")
|
||||||
@@ -931,15 +931,14 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
out = f.read()
|
out = f.read()
|
||||||
base = os.path.splitext(fname)[0][:80]
|
base = os.path.splitext(fname)[0][:80]
|
||||||
out_name = base + "-out." + out_ext
|
out_name = base + "-out." + out_ext
|
||||||
body = (b"--vidboundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" +
|
|
||||||
out_name.encode("utf-8") + b"\"\r\nContent-Type: application/octet-stream\r\n\r\n" +
|
|
||||||
out + b"\r\n--vidboundary--\r\n")
|
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header("Content-Type", "multipart/form-data; boundary=vidboundary")
|
self.send_header("Content-Type", "application/octet-stream")
|
||||||
self.send_header("Content-Length", str(len(body)))
|
self.send_header("Content-Length", str(len(out)))
|
||||||
|
self.send_header("Content-Disposition",
|
||||||
|
"attachment; filename=\"result." + out_ext + "\"; filename*=UTF-8''" + parse.quote(out_name))
|
||||||
self.send_header("X-Duration-Seconds", str(round(duration, 2)))
|
self.send_header("X-Duration-Seconds", str(round(duration, 2)))
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body)
|
self.wfile.write(out)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
|
self._reply(400, json.dumps({"code": 400, "message": str(e)}, ensure_ascii=False).encode("utf-8"), "application/json")
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
|
|||||||
@@ -218,9 +218,20 @@ async function process() {
|
|||||||
}
|
}
|
||||||
const blob = await res.blob();
|
const blob = await res.blob();
|
||||||
const disp = res.headers.get('Content-Disposition') || '';
|
const disp = res.headers.get('Content-Disposition') || '';
|
||||||
|
const mStar = disp.match(/filename\*=UTF-8''([^;]+)/);
|
||||||
const m = disp.match(/filename="([^"]+)"/);
|
const m = disp.match(/filename="([^"]+)"/);
|
||||||
|
let outName = 'result';
|
||||||
|
if (mStar) {
|
||||||
|
try {
|
||||||
|
outName = decodeURIComponent(mStar[1]);
|
||||||
|
}
|
||||||
|
catch { outName = 'result'; }
|
||||||
|
}
|
||||||
|
else if (m) {
|
||||||
|
outName = m[1];
|
||||||
|
}
|
||||||
resultBlob.value = blob;
|
resultBlob.value = blob;
|
||||||
resultName.value = m ? m[1] : 'result';
|
resultName.value = outName;
|
||||||
resultUrl.value = URL.createObjectURL(blob);
|
resultUrl.value = URL.createObjectURL(blob);
|
||||||
spent.value = Math.round((Date.now() - t0) / 1000);
|
spent.value = Math.round((Date.now() - t0) / 1000);
|
||||||
genHist.add({ title: `${current.value.name}: ${file.value.name}`, detail: `→ ${resultName.value}(${(blob.size / 1024 / 1024).toFixed(1)}MB, ${spent.value}s)` });
|
genHist.add({ title: `${current.value.name}: ${file.value.name}`, detail: `→ ${resultName.value}(${(blob.size / 1024 / 1024).toFixed(1)}MB, ${spent.value}s)` });
|
||||||
|
|||||||
Reference in new issue
Block a user