feat(upload): 分片断点续传 + 前端静态资源去 CDN 化

1) 分片上传(routes/chunked.py)
   - POST /api/upload/init  建会话,校验表单/字段/扩展名/体积
   - POST /api/upload/chunk 单片上传,幂等可重传,http body 直写磁盘
   - GET  /api/upload/status 查询已收分片
   - POST /api/upload/complete 按序合并 + sha256 校验 + 清理分片
   - /api/submit 新增 field fileTokens 挂载已上传文件(零拷贝 os.replace)
   - 过期会话(24h)由定时任务清理

2) 弱网下的样式可靠性
   - Tailwind Play CDN / GSAP 改为本地自托管(static/js/vendor/)
   - vendor 资源 1 年 immutable 缓存,自家 css/js 1 小时
   - CSS link 失败自动带 ?r= 重试一次
   - status 页加关键内联样式兜底,极端情况下仍可读

3) 大文件落盘沿用流式写入(无 400MB 内存峰值)
This commit is contained in:
zzy
2026-11-13 12:06:32 +08:00
parent 92dedc38e7
commit 84f8516c37
20 changed files with 614 additions and 59 deletions
+47 -1
View File
@@ -296,9 +296,55 @@ def submit_form():
)
# 处理文件上传 - 存储为BLOB
from database import save_file, save_file_stream
from database import save_file, save_file_stream, attach_uploaded_file
from routes.chunked import consume_upload, discard_upload
# 分片上传:客户端把文件切片传完并 complete 后,这里用 token 挂载,
# 不再走 multipart 重复上传一遍。
import json as _json_tokens
try:
file_tokens = _json_tokens.loads(request.form.get('fileTokens') or '{}')
if not isinstance(file_tokens, dict):
file_tokens = {}
except (ValueError, TypeError):
file_tokens = {}
for field in form['fields']:
if field['type'] == 'file':
token = (file_tokens.get(field['name']) or '').strip()
if token:
try:
src_path, meta = consume_upload(token, form_id, field['name'])
except (ValueError, FileNotFoundError) as e:
return jsonify(success=False, message=f'分片上传无效:{e}'), 400
ext = (meta['filename'].rsplit('.', 1)[-1] if '.' in meta['filename'] else '').lower()
if anonymous_mode:
base_name = f"匿名_{full_id[5:9]}"
else:
base_name = f"{student_name}{full_id}"
if has_multiple_files:
filename = f"{base_name}_{field['name']}.{ext}"
else:
filename = f"{base_name}.{ext}"
file_id = attach_uploaded_file(
form_id=form_id,
src_path=src_path,
filename=filename,
original_name=meta['filename'],
mime_type=meta.get('mime_type') or 'application/octet-stream',
field_name=field['name'],
submission_id=submission_id,
)
new_file_ids.append(file_id)
uploaded_files.append(filename)
try:
insert_business_metric('files_uploaded', meta['size'])
except Exception:
pass
discard_upload(token) # 会话用完即删,避免残留
continue
file = request.files.get(field['name'])
if file and file.filename:
ext = file.filename.rsplit('.', 1)[-1].lower()