fix: gallery download - use local URL for proxy, rename file with event name + photo date
This commit is contained in:
+23
-7
@@ -87,19 +87,35 @@ async def api_gallery_photos(
|
||||
|
||||
@router.get("/api/gallery/download/{photo_id}")
|
||||
async def download_photo(request: Request, photo_id: str):
|
||||
"""Proxy la photo full-res avec un nom de fichier lie a l'evenement en cours."""
|
||||
"""Proxy la photo full-res avec Content-Disposition et nom basé sur l'événement + date photo."""
|
||||
pb = request.app.state.photobooth_service
|
||||
cfg = request.app.state.config
|
||||
event_svc = getattr(request.app.state, "event_service", None)
|
||||
|
||||
img_url = pb.media_url(photo_id)
|
||||
# Toujours fetcher depuis localhost (base_url), pas l'URL publique
|
||||
local_url = f"{pb._base.rstrip('/')}/media/full/{photo_id}"
|
||||
|
||||
# Nom de l'événement (lisible) + date de la photo depuis le disque
|
||||
event_name = (cfg.event.name or cfg.event.slug or "Photomaton").replace(" ", "_")
|
||||
slug = cfg.event.slug or "photomaton"
|
||||
date_str = datetime.now().strftime("%Y-%m-%d")
|
||||
filename = f"{slug}_By_LSDW_{date_str}.jpg"
|
||||
|
||||
# Tenter de récupérer la vraie date depuis le fichier
|
||||
photo_date = datetime.now().strftime("%Y-%m-%d_%H-%M")
|
||||
try:
|
||||
media_dir = Path(cfg.photobooth.media_dir)
|
||||
candidates = list(media_dir.glob(f"{photo_id}*"))
|
||||
if candidates:
|
||||
mtime = candidates[0].stat().st_mtime
|
||||
photo_date = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d_%H-%M")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
safe_name = "".join(c if c.isalnum() or c in "-_." else "_" for c in event_name)
|
||||
filename = f"{safe_name}_{photo_date}.jpg"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
r = await client.get(img_url)
|
||||
r = await client.get(local_url)
|
||||
r.raise_for_status()
|
||||
|
||||
if event_svc:
|
||||
@@ -116,8 +132,8 @@ async def download_photo(request: Request, photo_id: str):
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("Download proxy echoue (%s), fallback redirect: %s", photo_id, e)
|
||||
return RedirectResponse(url=img_url)
|
||||
logger.warning("Download proxy échoue (%s): %s", photo_id, e)
|
||||
return RedirectResponse(url=pb.media_url(photo_id))
|
||||
|
||||
|
||||
def _is_image(item: dict) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user