feat: докачиваемая загрузка файлов до 3 ГБ (tus 1.0.0)

Бэкенд (tus_upload.py): самодостаточное ядро tus — потоковая запись
кусков на диск (память не растёт), докачка с серверного offset,
изоляция по владельцу, перенос готового файла в work_dir как у legacy
/api/upload, лимит 3 ГБ + guard по свободному диску, авто-чистка
брошенных загрузок старше суток.

Фронт: tus-js-client (с SRI) — прогресс, авто-ретраи, обновление
токена перед истечением, докачка после обрыва/перезагрузки; мягкий
fallback на /api/upload если CDN недоступен.

Аудит: tus-куски не логируются по PATCH, только финализация (action=upload).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-06-02 15:43:56 +10:00
co-authored by Claude Opus 4.8
parent ac17dec3ce
commit 522b157acc
3 changed files with 290 additions and 10 deletions
+18 -1
View File
@@ -12,6 +12,7 @@ from fastapi.staticfiles import StaticFiles
from auth import authenticate, verify_token
from db import get_audit_log, get_history, get_history_any_user, init_db, list_sessions, log_action, save_message
from tus_upload import register_tus
# Backend: "claude" (Docker sandbox), "glm" (GLM-5 + CAG), "auto" (route by task)
AI_BACKEND = os.environ.get("AI_BACKEND", "claude")
@@ -187,7 +188,7 @@ _AUDIT_MAP = [
("POST", "/api/admin/", "admin"),
]
_SKIP_PREFIXES = ("/static/", "/favicon")
_SKIP_PREFIXES = ("/static/", "/favicon", "/api/tus") # tus chunks are logged on completion, not per-PATCH
def _resolve_action(method: str, path: str) -> str | None:
@@ -286,6 +287,22 @@ async def index():
return Path("static/chat.html").read_text(encoding="utf-8")
# Resumable uploads (tus 1.0.0). Same-process, reuses auth + per-user work dir;
# the finished file lands in the work dir exactly like legacy /api/upload.
TUS_MAX_SIZE = 3 * 1024 * 1024 * 1024 # 3 GB
register_tus(
app,
get_user=_get_user,
get_work_dir=get_work_dir,
is_allowed_file=is_allowed_file,
log_action=log_action,
save_message=save_message,
max_size=TUS_MAX_SIZE,
staging_root=Path(os.environ.get("DATA_DIR", "./smeta-web-data")) / "_tus",
allowed_hint="xlsx, xls, docx, pdf, csv, txt, xml, json, yaml, gge, esw, zip",
)
@app.post("/api/login")
async def login(request: Request):
body = await request.json()