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}")
|
@router.get("/api/gallery/download/{photo_id}")
|
||||||
async def download_photo(request: Request, photo_id: str):
|
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
|
pb = request.app.state.photobooth_service
|
||||||
cfg = request.app.state.config
|
cfg = request.app.state.config
|
||||||
event_svc = getattr(request.app.state, "event_service", None)
|
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"
|
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:
|
try:
|
||||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
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()
|
r.raise_for_status()
|
||||||
|
|
||||||
if event_svc:
|
if event_svc:
|
||||||
@@ -116,8 +132,8 @@ async def download_photo(request: Request, photo_id: str):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning("Download proxy echoue (%s), fallback redirect: %s", photo_id, e)
|
logger.warning("Download proxy échoue (%s): %s", photo_id, e)
|
||||||
return RedirectResponse(url=img_url)
|
return RedirectResponse(url=pb.media_url(photo_id))
|
||||||
|
|
||||||
|
|
||||||
def _is_image(item: dict) -> bool:
|
def _is_image(item: dict) -> bool:
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
<button class="lightbox-close">×</button>
|
<button class="lightbox-close">×</button>
|
||||||
<img id="lightbox-img" src="" alt="Photo">
|
<img id="lightbox-img" src="" alt="Photo">
|
||||||
<div class="lightbox-actions">
|
<div class="lightbox-actions">
|
||||||
<a class="btn btn-primary" id="lb-download" download>⬇ Télécharger la photo</a>
|
<a class="btn btn-primary" id="lb-download">⬇ Télécharger la photo</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="text-xs text-muted">Connectez-vous au WiFi Photomaton pour télécharger</div>
|
<div class="text-xs text-muted">Connectez-vous au WiFi Photomaton pour télécharger</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -75,7 +75,7 @@ async function loadPhotos(page = 1) {
|
|||||||
<div class="photo-card" onclick="openLightbox('${p.full_url}', '${p.download_url}')">
|
<div class="photo-card" onclick="openLightbox('${p.full_url}', '${p.download_url}')">
|
||||||
<img src="${p.thumb_url}" loading="lazy" alt="Photo" onerror="this.src='${p.full_url}'">
|
<img src="${p.thumb_url}" loading="lazy" alt="Photo" onerror="this.src='${p.full_url}'">
|
||||||
<div class="photo-card-actions">
|
<div class="photo-card-actions">
|
||||||
<a class="btn btn-primary btn-sm" href="${p.download_url}" download onclick="event.stopPropagation()">⬇ Télécharger</a>
|
<a class="btn btn-primary btn-sm" href="${p.download_url}" onclick="event.stopPropagation()">⬇ Télécharger</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
`).join('');
|
`).join('');
|
||||||
|
|||||||
Reference in New Issue
Block a user