From 522b157acc5e4c096dbfbc7219cc81d0b4696d39 Mon Sep 17 00:00:00 2001 From: anor Date: Tue, 2 Jun 2026 15:43:56 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=BA=D0=B0=D1=87=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=B5=D0=BC=D0=B0=D1=8F=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B0=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B4=D0=BE=203=20=D0=93=D0=91=20(tus=201.0.0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Бэкенд (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) --- main.py | 19 ++++- static/chat.html | 86 ++++++++++++++++++--- tus_upload.py | 195 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+), 10 deletions(-) create mode 100644 tus_upload.py diff --git a/main.py b/main.py index fc4b482..a14050e 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/static/chat.html b/static/chat.html index 8a08dc6..b037fc0 100644 --- a/static/chat.html +++ b/static/chat.html @@ -154,6 +154,7 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo +