From 228e0dfd6edc0c5236767607074a01668cb330a8 Mon Sep 17 00:00:00 2001 From: anor Date: Wed, 6 May 2026 17:34:05 +1000 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=D0=BA?= =?UTF-8?q?=D0=B0=20=E2=80=94=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=20?= =?UTF-8?q?=C2=AB=D0=A4=D0=B0=D0=B9=D0=BB=D1=8B=C2=BB=20=D1=81=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=81=D0=BC=D0=BE=D1=82=D1=80=D0=BE=D0=BC=20=D0=B8?= =?UTF-8?q?=20=D1=81=D0=BA=D0=B0=D1=87=D0=B8=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=BC=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=BB=D1=8E?= =?UTF-8?q?=D0=B1=D0=BE=D0=B3=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Бэкенд: - GET /api/admin/files?username=X — плоский список файлов всех табов пользователя (tab_id, filename, size, mtime). - GET /api/admin/file_download?username=X&tab_id=Y&filename=Z — скачивание через FileResponse. Имена с спецсимволами («!Реестр СМ..xlsm») передаются query-параметрами вместо path — не страдает от URL-escaping. Защита от path traversal через Path(x).name + resolve().relative_to(DATA_DIR). UI: - Раздел «Файлы» в /admin (новый пункт сайдбара). - Селект пользователей наполняется из существующего /api/admin/users. - Кнопка «Скачать» делает fetch с Authorization-заголовком и триггерит download через blob — стандартному JWT не передать. Co-Authored-By: Claude Opus 4.7 (1M context) --- main.py | 57 ++++++++++++++++++++++++++++++++++++++++ static/admin.html | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/main.py b/main.py index df1fccd..bb5f935 100644 --- a/main.py +++ b/main.py @@ -785,6 +785,63 @@ async def admin_verify_endpoint(request: Request): return {"ok": True} +@app.get("/api/admin/files") +async def admin_list_files_endpoint(request: Request): + """All files uploaded by a specific user (across every tab).""" + _require_admin(request) + target_user = request.query_params.get("username", "").strip() + if not target_user: + raise HTTPException(400, "username query param required") + safe_user = Path(target_user).name + if safe_user != target_user or not safe_user: + raise HTTPException(400, "Invalid username") + user_dir = Path(os.environ.get("DATA_DIR", "./smeta-web-data")) / safe_user + files = [] + if user_dir.exists() and user_dir.is_dir(): + for tab_dir in sorted(user_dir.iterdir()): + if not tab_dir.is_dir() or tab_dir.name.startswith("."): + continue + for f in sorted(tab_dir.iterdir()): + if f.is_file() and not f.name.startswith("."): + st = f.stat() + files.append({ + "tab_id": tab_dir.name, + "filename": f.name, + "size": st.st_size, + "mtime": st.st_mtime, + }) + return {"username": safe_user, "count": len(files), "files": files} + + +@app.get("/api/admin/file_download") +async def admin_download_file_endpoint(request: Request): + """Download any user's uploaded file (admin only). + + Path components passed as query params to avoid URL-escaping pain + with names like `!Реестр СМ..xlsm` and tab_ids containing slashes. + """ + _require_admin(request) + target_user = request.query_params.get("username", "").strip() + tab_id = request.query_params.get("tab_id", "").strip() + filename = request.query_params.get("filename", "").strip() + if not (target_user and tab_id and filename): + raise HTTPException(400, "username, tab_id, filename required") + safe_user = Path(target_user).name + safe_tab = Path(tab_id).name + safe_name = Path(filename).name + if not (safe_user and safe_tab and safe_name): + raise HTTPException(400, "Invalid path component") + data_dir = Path(os.environ.get("DATA_DIR", "./smeta-web-data")).resolve() + fpath = (data_dir / safe_user / safe_tab / safe_name).resolve() + try: + fpath.relative_to(data_dir) + except ValueError: + raise HTTPException(400, "Path traversal denied") + if not fpath.exists() or not fpath.is_file(): + raise HTTPException(404, "File not found") + return FileResponse(fpath, filename=safe_name) + + @app.get("/admin", response_class=HTMLResponse) async def admin_page(): # Auth check is done client-side (JS) + all /api/admin/* endpoints require admin diff --git a/static/admin.html b/static/admin.html index 137cadc..5785573 100644 --- a/static/admin.html +++ b/static/admin.html @@ -46,6 +46,7 @@ a { color: var(--accent); text-decoration: none; } Приглашения Сессии Журнал + Файлы ← Вернуться в чат @@ -102,6 +103,21 @@ a { color: var(--accent); text-decoration: none; } + +
+

Файлы пользователей

+
+ + + +
+ + +
Tab IDФайлРазмерЗагруженДействие
+
+