diff --git a/backend/app/db.py b/backend/app/db.py index a12a732..27f42ee 100644 --- a/backend/app/db.py +++ b/backend/app/db.py @@ -201,11 +201,8 @@ def _row_to_dict(row: sqlite3.Row | None) -> dict[str, Any] | None: except json.JSONDecodeError: data["meta"] = {} if data.get("cover_path"): - cp = str(data.get("cover_path") or "") - if cp.startswith("http://") or cp.startswith("https://"): - data["cover_url"] = cp - else: - data["cover_url"] = f"/api/sessions/{data['id']}/cover" + # Always expose via API proxy so SeaweedFS private buckets still display in UI + data["cover_url"] = f"/api/sessions/{data['id']}/cover" else: if "cover_path" in data: data["cover_url"] = "" diff --git a/backend/app/main.py b/backend/app/main.py index e145f84..e06b9d1 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -12,6 +12,7 @@ from fastapi import FastAPI, HTTPException, Query from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse, StreamingResponse from fastapi.staticfiles import StaticFiles +import httpx from . import cookie_loader, db, pb_sync from .config import settings @@ -454,14 +455,32 @@ async def sessions_detail(session_id: str) -> dict[str, Any]: @app.get("/api/sessions/{session_id}/cover") async def sessions_cover(session_id: str): - from fastapi.responses import RedirectResponse - session = db.get_session(session_id) if not session: raise HTTPException(404, "场次不存在") cp = str(session.get("cover_path") or "") if cp.startswith("http://") or cp.startswith("https://"): - return RedirectResponse(cp, status_code=302) + # Proxy SeaweedFS object with credentials (bucket may not be anonymously readable) + try: + from . import s3_storage + + if s3_storage.s3_enabled() and settings.s3_bucket in cp: + key = cp.split(f"/{settings.s3_bucket}/", 1)[-1] + client = s3_storage._client() # noqa: SLF001 + obj = client.get_object(Bucket=settings.s3_bucket, Key=key) + body = obj["Body"].read() + return StreamingResponse(iter([body]), media_type="image/jpeg") + except Exception: + pass + # fallback: direct fetch + try: + async with httpx.AsyncClient(timeout=20.0, trust_env=False) as client: + resp = await client.get(cp) + if resp.status_code < 300 and resp.content: + return StreamingResponse(iter([resp.content]), media_type="image/jpeg") + except Exception: + pass + raise HTTPException(404, "远程封面不可用") path = cover_path_for(session_id) if not path: raise HTTPException(404, "无截图")