Merge branch 'main' into dev

This commit is contained in:
anor
2026-05-06 17:34:55 +10:00
2 changed files with 124 additions and 0 deletions
+57
View File
@@ -853,6 +853,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