Files
photoBooth/backend/api/gallery.py
T
admin 209f81aa3d
🚀 Deploy — JH Photomaton / 🔍 Vérification (push) Has been cancelled
🚀 Deploy — JH Photomaton / 🍓 Deploy sur le Pi (push) Has been cancelled
first commit
2026-07-16 00:55:29 +02:00

79 lines
2.4 KiB
Python

"""Galerie publique — accessible sans authentification."""
import logging
from fastapi import APIRouter, Request, Query
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from pathlib import Path
logger = logging.getLogger(__name__)
router = APIRouter()
_templates = Jinja2Templates(directory=Path(__file__).parent.parent.parent / "frontend" / "templates")
@router.get("/", response_class=HTMLResponse)
async def index(request: Request):
"""Page d'accueil → redirige vers la galerie publique."""
return RedirectResponse(url="/gallery")
@router.get("/gallery", response_class=HTMLResponse)
async def gallery_page(request: Request):
cfg = request.app.state.config
if not cfg.gallery.public_enabled:
return HTMLResponse("<h1>Galerie désactivée</h1>", status_code=403)
return _templates.TemplateResponse("public/gallery.html", {"request": request, "config": cfg})
@router.get("/api/gallery/photos")
async def api_gallery_photos(
request: Request,
page: int = Query(default=1, ge=1),
limit: int = Query(default=24, ge=1, le=100),
):
"""Liste des photos depuis photobooth-app (paginée)."""
pb = request.app.state.photobooth_service
cfg = request.app.state.config
all_photos = await pb.get_media_collection(limit=500)
# Filtre sur les images uniquement
photos = [p for p in all_photos if _is_image(p)]
total = len(photos)
start = (page - 1) * limit
end = start + limit
page_photos = photos[start:end]
# Enrichit avec les URLs
for p in page_photos:
pid = _get_id(p)
p["full_url"] = pb.media_url(pid)
p["thumb_url"] = pb.thumbnail_url(pid)
p["download_url"] = f"/api/gallery/download/{pid}"
return {
"photos": page_photos,
"total": total,
"page": page,
"pages": max(1, (total + limit - 1) // limit),
}
@router.get("/api/gallery/download/{photo_id}")
async def download_photo(request: Request, photo_id: str):
"""Redirige vers le fichier full-res sur photobooth-app."""
pb = request.app.state.photobooth_service
return RedirectResponse(url=pb.media_url(photo_id))
def _is_image(item: dict) -> bool:
t = item.get("type", item.get("mediaitem_type", "image"))
return str(t).lower() in ("image", "still", "photo")
def _get_id(item: dict) -> str:
return str(item.get("id", item.get("filename", item.get("uid", ""))))