|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import logging |
4 | | -from datetime import datetime |
| 4 | +import html |
| 5 | +from datetime import datetime, timedelta |
5 | 6 | from pathlib import Path |
6 | 7 | from urllib.parse import quote |
7 | 8 |
|
8 | 9 | from fastapi import APIRouter, Depends, File, HTTPException, Request, UploadFile |
9 | 10 | from fastapi.responses import FileResponse, HTMLResponse, JSONResponse |
10 | 11 | from sqlmodel import Session, select |
11 | 12 |
|
12 | | -from app.config import CACHE_MAX_AGE_SECONDS, MAX_FILE_SIZE, RATE_LIMIT_PER_MINUTE, UPLOAD_DIR |
| 13 | +from app.config import ( |
| 14 | + ADMIN_LOCK_STEP_SECONDS, |
| 15 | + ADMIN_PASSWORD, |
| 16 | + CACHE_MAX_AGE_SECONDS, |
| 17 | + MAX_FILE_SIZE, |
| 18 | + RATE_LIMIT_PER_MINUTE, |
| 19 | + UPLOAD_DIR, |
| 20 | +) |
13 | 21 | from app.core.metrics import metrics |
14 | 22 | from app.core.rate_limit import RateLimiter |
15 | 23 | from app.core.templates import render_template |
|
25 | 33 | rate_limiter = RateLimiter(RATE_LIMIT_PER_MINUTE) |
26 | 34 | MAX_FILE_SIZE_MB = MAX_FILE_SIZE / (1024 * 1024) |
27 | 35 | UPLOAD_ROOT = Path(UPLOAD_DIR).resolve() |
| 36 | +_admin_attempts: dict[str, dict] = {} |
28 | 37 |
|
29 | 38 |
|
30 | 39 | async def enforce_rate_limit(request: Request): |
@@ -68,6 +77,190 @@ async def api_info(): |
68 | 77 | return HTMLResponse(content=html) |
69 | 78 |
|
70 | 79 |
|
| 80 | +def _render_admin_table(files: list[FileModel]) -> str: |
| 81 | + rows: list[str] = [] |
| 82 | + for file in files: |
| 83 | + preview = f"<img src='/{quote(file.stored_name)}' alt='preview' loading='lazy' />" |
| 84 | + rows.append( |
| 85 | + "<tr>" |
| 86 | + f"<td>{html.escape(file.id)}</td>" |
| 87 | + f"<td class='preview-cell'>{preview}</td>" |
| 88 | + f"<td>{html.escape(file.original_name)}</td>" |
| 89 | + f"<td>{file.size_bytes} B</td>" |
| 90 | + f"<td>{file.created_at}</td>" |
| 91 | + "<td>" |
| 92 | + "<form method='post' action='/admin/delete' class='inline'>" |
| 93 | + f"<input type='hidden' name='file_id' value='{html.escape(file.id)}' />" |
| 94 | + "<input type='password' name='password' placeholder='Admin password' required />" |
| 95 | + "<button type='submit'>Delete</button>" |
| 96 | + "</form>" |
| 97 | + "</td>" |
| 98 | + "</tr>" |
| 99 | + ) |
| 100 | + return "".join(rows) or "<tr><td colspan='5'>No files yet</td></tr>" |
| 101 | + |
| 102 | + |
| 103 | +def _human_bytes(value: int) -> str: |
| 104 | + units = ["B", "KB", "MB", "GB", "TB"] |
| 105 | + size = float(max(value, 0)) |
| 106 | + for unit in units: |
| 107 | + if size < 1024 or unit == units[-1]: |
| 108 | + formatted = f"{size:.1f}".rstrip("0").rstrip(".") |
| 109 | + return f"{formatted or '0'} {unit}" |
| 110 | + size /= 1024 |
| 111 | + return "0 B" |
| 112 | + |
| 113 | + |
| 114 | +def _render_admin_login(message: str | None = None) -> str: |
| 115 | + flash_html = f"<div class='flash'>{html.escape(message)}</div>" if message else "" |
| 116 | + return render_template("pages/admin_login.html", {"flash_message": flash_html}) |
| 117 | + |
| 118 | + |
| 119 | +def _render_admin_page(session: Session, message: str | None = None) -> str: |
| 120 | + totals = fetch_storage_totals(session) |
| 121 | + snapshot = metrics.snapshot() |
| 122 | + stmt = select(FileModel).order_by(FileModel.created_at.desc()).limit(50) |
| 123 | + files = session.exec(stmt).all() |
| 124 | + flash_html = f"<div class='flash'>{html.escape(message)}</div>" if message else "" |
| 125 | + return render_template( |
| 126 | + "pages/admin.html", |
| 127 | + { |
| 128 | + "uploads": totals["total_files"], |
| 129 | + "downloads": snapshot.get("downloads", 0), |
| 130 | + "deleted": snapshot.get("deleted", 0), |
| 131 | + "storage_human": _human_bytes(totals["total_bytes"]), |
| 132 | + "table_rows": _render_admin_table(files), |
| 133 | + "flash_message": flash_html, |
| 134 | + }, |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +async def _get_admin_password(request: Request) -> str | None: |
| 139 | + password = request.headers.get("x-admin-password") or request.query_params.get("password") |
| 140 | + if password: |
| 141 | + return password |
| 142 | + if request.method in {"POST", "PUT", "DELETE"}: |
| 143 | + form_data = getattr(request.state, "admin_form", None) |
| 144 | + if form_data is None: |
| 145 | + try: |
| 146 | + form_data = await request.form() |
| 147 | + except Exception: |
| 148 | + form_data = None |
| 149 | + else: |
| 150 | + request.state.admin_form = form_data |
| 151 | + if form_data: |
| 152 | + return form_data.get("password") |
| 153 | + return None |
| 154 | + |
| 155 | + |
| 156 | +async def _auth_admin(request: Request, allow_blank: bool): |
| 157 | + client = request.client.host if request.client else "unknown" |
| 158 | + state = _admin_attempts.setdefault(client, {"failures": 0, "penalty": 0, "lock_until": None}) |
| 159 | + now = datetime.utcnow() |
| 160 | + lock_until = state.get("lock_until") |
| 161 | + if lock_until and now < lock_until: |
| 162 | + remaining = lock_until - now |
| 163 | + minutes = max(1, int(remaining.total_seconds() // 60) + 1) |
| 164 | + msg = f"Too many attempts. Try again in {minutes} minutes." |
| 165 | + if allow_blank: |
| 166 | + return False, msg, True |
| 167 | + raise HTTPException(status_code=429, detail=msg) |
| 168 | + if lock_until and now >= lock_until: |
| 169 | + state["lock_until"] = None |
| 170 | + |
| 171 | + password = await _get_admin_password(request) |
| 172 | + if not password: |
| 173 | + if allow_blank: |
| 174 | + return False, None, False |
| 175 | + raise HTTPException(status_code=401, detail="Admin password required") |
| 176 | + |
| 177 | + if ADMIN_PASSWORD and password == ADMIN_PASSWORD: |
| 178 | + state["failures"] = 0 |
| 179 | + return True, None, False |
| 180 | + |
| 181 | + state["failures"] = state.get("failures", 0) + 1 |
| 182 | + if state["failures"] >= 3: |
| 183 | + state["failures"] = 0 |
| 184 | + state["penalty"] = state.get("penalty", 0) + 1 |
| 185 | + duration = state["penalty"] * ADMIN_LOCK_STEP_SECONDS |
| 186 | + state["lock_until"] = now + timedelta(seconds=duration) |
| 187 | + minutes = max(1, duration // 60) |
| 188 | + msg = f"Too many failures. Locked for {minutes} minutes." |
| 189 | + if allow_blank: |
| 190 | + return False, msg, True |
| 191 | + raise HTTPException(status_code=429, detail=msg) |
| 192 | + msg = "Invalid password" |
| 193 | + if allow_blank: |
| 194 | + return False, msg, False |
| 195 | + raise HTTPException(status_code=401, detail=msg) |
| 196 | + |
| 197 | + |
| 198 | +async def require_admin(request: Request) -> str: |
| 199 | + success, _, _ = await _auth_admin(request, allow_blank=False) |
| 200 | + if success: |
| 201 | + return ADMIN_PASSWORD |
| 202 | + raise HTTPException(status_code=500, detail="Admin authentication failed") |
| 203 | + |
| 204 | + |
| 205 | +def _remove_file_from_disk(stored_name: str) -> None: |
| 206 | + try: |
| 207 | + path = (UPLOAD_ROOT / stored_name).resolve() |
| 208 | + path.relative_to(UPLOAD_ROOT) |
| 209 | + except (ValueError, RuntimeError): |
| 210 | + return |
| 211 | + path.unlink(missing_ok=True) |
| 212 | + |
| 213 | + |
| 214 | +@router.api_route("/admin", methods=["GET", "POST"], response_class=HTMLResponse) |
| 215 | +async def admin_dashboard(request: Request, session: Session = Depends(get_session)): |
| 216 | + success, message, locked = await _auth_admin(request, allow_blank=True) |
| 217 | + if success: |
| 218 | + html = _render_admin_page(session, message) |
| 219 | + return HTMLResponse(content=html) |
| 220 | + html = _render_admin_login(message) |
| 221 | + status = 429 if locked and message else 200 |
| 222 | + return HTMLResponse(content=html, status_code=status) |
| 223 | + |
| 224 | + |
| 225 | +@router.post("/admin/delete", response_class=HTMLResponse) |
| 226 | +async def admin_delete_file( |
| 227 | + request: Request, |
| 228 | + session: Session = Depends(get_session), |
| 229 | + _: str = Depends(require_admin), |
| 230 | +): |
| 231 | + form = getattr(request.state, "admin_form", None) or await request.form() |
| 232 | + file_id = form.get("file_id") |
| 233 | + if not file_id: |
| 234 | + raise HTTPException(status_code=400, detail="Missing file_id") |
| 235 | + file = session.get(FileModel, file_id) |
| 236 | + if not file: |
| 237 | + html = _render_admin_page(session, "File not found.") |
| 238 | + return HTMLResponse(content=html, status_code=404) |
| 239 | + |
| 240 | + _remove_file_from_disk(file.stored_name) |
| 241 | + session.delete(file) |
| 242 | + session.commit() |
| 243 | + html = _render_admin_page(session, "File deleted.") |
| 244 | + return HTMLResponse(content=html) |
| 245 | + |
| 246 | + |
| 247 | +@router.post("/admin/delete-all", response_class=HTMLResponse) |
| 248 | +async def admin_delete_all( |
| 249 | + request: Request, |
| 250 | + session: Session = Depends(get_session), |
| 251 | + _: str = Depends(require_admin), |
| 252 | +): |
| 253 | + files = session.exec(select(FileModel)).all() |
| 254 | + deleted = 0 |
| 255 | + for file in files: |
| 256 | + _remove_file_from_disk(file.stored_name) |
| 257 | + session.delete(file) |
| 258 | + deleted += 1 |
| 259 | + session.commit() |
| 260 | + html = _render_admin_page(session, f"Deleted {deleted} files.") |
| 261 | + return HTMLResponse(content=html) |
| 262 | + |
| 263 | + |
71 | 264 | @router.get("/list", dependencies=[Depends(enforce_rate_limit)]) |
72 | 265 | def list_files(session: Session = Depends(get_session)): |
73 | 266 | files = session.exec(select(FileModel).order_by(FileModel.created_at.desc())).all() |
|
0 commit comments