32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
from fastapi import APIRouter, HTTPException, Query
|
|
from fastapi.responses import FileResponse
|
|
|
|
from ..media_catalog import KNOWN_MEDIA_ROLES, published_assets, published_file
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/api/v1/media/catalog")
|
|
def media_catalog(
|
|
entity_type: str | None = Query(None, pattern="^(fish|waterbody|tackle|reference)$"),
|
|
media_role: str | None = Query(None, pattern="^(waterbody_cover|waterbody_map|waterbody_depth_map|waterbody_screenshot|tackle_card|tackle_detail|rig_diagram|tackle_screenshot)$"),
|
|
) -> list[dict]:
|
|
if media_role and entity_type not in {"waterbody", "tackle"}:
|
|
raise HTTPException(status_code=422, detail="media_role requires waterbody or tackle entity_type")
|
|
if media_role and media_role not in KNOWN_MEDIA_ROLES:
|
|
raise HTTPException(status_code=422, detail="unknown media role")
|
|
return published_assets(entity_type, media_role)
|
|
|
|
|
|
@router.get("/api/v1/media/assets/{digest}", response_class=FileResponse)
|
|
def media_asset(digest: str) -> FileResponse:
|
|
item = published_file(digest)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Media asset not found")
|
|
path, media_type = item
|
|
return FileResponse(path, media_type=media_type, headers={
|
|
"Cache-Control": "public, max-age=31536000, immutable",
|
|
"ETag": f'"{digest}"',
|
|
})
|