From f17ddc12df0bdffeb2e291475b8f3505228a8aa7 Mon Sep 17 00:00:00 2001 From: anor Date: Tue, 2 Jun 2026 16:29:35 +1000 Subject: [PATCH] =?UTF-8?q?fix:=20=D1=81=D0=BA=D0=B0=D1=87=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=B8?= =?UTF-8?q?=D1=85=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=B2=20=D0=B0?= =?UTF-8?q?=D0=B4=D0=BC=D0=B8=D0=BD-=D0=BF=D0=B0=D0=BD=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=20(=D0=BD=D0=B0=D1=82=D0=B8=D0=B2=D0=BD=D1=8B=D0=B9=20download?= =?UTF-8?q?=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20blob)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Кнопка «Скачать» грузила файл через fetch→blob→createObjectURL — весь файл в память браузера. 14 МБ ок, 1.5 ГБ молча не начинался (лимиты blob, особенно Firefox). Теперь нативная загрузка через — браузер стримит на диск, размер не важен. Эндпоинт /api/admin/file_download принимает токен и через query-параметр (ссылка не шлёт заголовок). Бамп ADMIN_HTML_VERSION. Co-Authored-By: Claude Opus 4.8 (1M context) --- main.py | 10 ++++++++-- static/admin.html | 13 ++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index a14050e..83529c1 100644 --- a/main.py +++ b/main.py @@ -1008,7 +1008,13 @@ async def admin_download_file_endpoint(request: Request): Path components passed as query params to avoid URL-escaping pain with names like `!Реестр СМ..xlsm` and tab_ids containing slashes. """ - _require_admin(request) + # Native browser download of large files can't send an Authorization header, + # so accept the admin token via query param as a fallback to the header. + from auth import get_token_role + _tok = (request.headers.get("authorization", "").removeprefix("Bearer ").strip() + or request.query_params.get("token", "").strip()) + if not verify_token(_tok) or get_token_role(_tok) != "admin": + raise HTTPException(403, "Admin only") target_user = request.query_params.get("username", "").strip() tab_id = request.query_params.get("tab_id", "").strip() filename = request.query_params.get("filename", "").strip() @@ -1030,7 +1036,7 @@ async def admin_download_file_endpoint(request: Request): return FileResponse(fpath, filename=safe_name) -ADMIN_HTML_VERSION = "20260506g" +ADMIN_HTML_VERSION = "20260602a" @app.get("/admin", response_class=HTMLResponse) diff --git a/static/admin.html b/static/admin.html index a557336..e53df65 100644 --- a/static/admin.html +++ b/static/admin.html @@ -282,16 +282,15 @@ async function loadFiles() { tbl.appendChild(tr); } } -async function adminDownload(url, filename) { - // fetch with auth header, then trigger browser download - const r = await fetch(url, {headers: H}); - if (!r.ok) { alert('Ошибка скачивания: ' + r.status); return; } - const blob = await r.blob(); +function adminDownload(url, filename) { + // Native download: the browser streams straight to disk, so it works for + // multi-GB files. The old fetch→blob approach buffered the whole file in + // browser memory and silently failed on large ones. can't set an auth + // header, so the endpoint also accepts the token via query param. const a = document.createElement('a'); - a.href = URL.createObjectURL(blob); + a.href = url + '&token=' + encodeURIComponent(token); a.download = filename; document.body.appendChild(a); a.click(); a.remove(); - setTimeout(() => URL.revokeObjectURL(a.href), 1000); } // === Invites ===