Proxy SeaweedFS covers through API so private buckets display in UI.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d27387cb9e
commit
03888989d2
@ -201,11 +201,8 @@ def _row_to_dict(row: sqlite3.Row | None) -> dict[str, Any] | None:
|
|||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
data["meta"] = {}
|
data["meta"] = {}
|
||||||
if data.get("cover_path"):
|
if data.get("cover_path"):
|
||||||
cp = str(data.get("cover_path") or "")
|
# Always expose via API proxy so SeaweedFS private buckets still display in UI
|
||||||
if cp.startswith("http://") or cp.startswith("https://"):
|
data["cover_url"] = f"/api/sessions/{data['id']}/cover"
|
||||||
data["cover_url"] = cp
|
|
||||||
else:
|
|
||||||
data["cover_url"] = f"/api/sessions/{data['id']}/cover"
|
|
||||||
else:
|
else:
|
||||||
if "cover_path" in data:
|
if "cover_path" in data:
|
||||||
data["cover_url"] = ""
|
data["cover_url"] = ""
|
||||||
|
|||||||
@ -12,6 +12,7 @@ from fastapi import FastAPI, HTTPException, Query
|
|||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.responses import FileResponse, StreamingResponse
|
from fastapi.responses import FileResponse, StreamingResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
import httpx
|
||||||
|
|
||||||
from . import cookie_loader, db, pb_sync
|
from . import cookie_loader, db, pb_sync
|
||||||
from .config import settings
|
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")
|
@app.get("/api/sessions/{session_id}/cover")
|
||||||
async def sessions_cover(session_id: str):
|
async def sessions_cover(session_id: str):
|
||||||
from fastapi.responses import RedirectResponse
|
|
||||||
|
|
||||||
session = db.get_session(session_id)
|
session = db.get_session(session_id)
|
||||||
if not session:
|
if not session:
|
||||||
raise HTTPException(404, "场次不存在")
|
raise HTTPException(404, "场次不存在")
|
||||||
cp = str(session.get("cover_path") or "")
|
cp = str(session.get("cover_path") or "")
|
||||||
if cp.startswith("http://") or cp.startswith("https://"):
|
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)
|
path = cover_path_for(session_id)
|
||||||
if not path:
|
if not path:
|
||||||
raise HTTPException(404, "无截图")
|
raise HTTPException(404, "无截图")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user