diff --git a/backend/api/admin_gallery_api.py b/backend/api/admin_gallery_api.py index 96f2179..67c02e8 100644 --- a/backend/api/admin_gallery_api.py +++ b/backend/api/admin_gallery_api.py @@ -22,20 +22,49 @@ def _get_id(item: dict) -> str: def _find_file(media_dir: str, photo_id: str) -> Path | None: - """Cherche un fichier image correspondant à l'identifiant dans le répertoire media.""" + """Cherche un fichier image correspondant à l'identifiant. + + Cherche dans media_dir, processed_full/ et les chemins photobooth standard. + """ base = Path(media_dir) - if not base.exists(): - return None - for ext in (".jpg", ".jpeg", ".png"): - f = base / f"{photo_id}{ext}" - if f.exists(): - return f - matches = list(base.rglob(f"{photo_id}{ext}")) - if matches: - return matches[0] + candidates = [ + base, + base / "processed_full", + Path("/home/pi/photobooth-data/media/processed_full"), + Path("/home/pi/photobooth-data/media"), + ] + for d in candidates: + if not d.exists(): + continue + for ext in (".jpg", ".jpeg", ".png"): + f = d / f"{photo_id}{ext}" + if f.exists(): + return f return None +def _build_mtime_index(media_dir: Path) -> dict[str, float]: + """Construit un index {stem: mtime, name: mtime} en cherchant dans les dossiers connus.""" + index: dict[str, float] = {} + checked: set[Path] = set() + candidates = [ + media_dir, + media_dir / "processed_full", + Path("/home/pi/photobooth-data/media/processed_full"), + Path("/home/pi/photobooth-data/media"), + ] + for d in candidates: + if not d.exists() or d in checked: + continue + checked.add(d) + for f in d.iterdir(): + if f.is_file() and f.suffix.lower() in (".jpg", ".jpeg", ".png"): + mt = f.stat().st_mtime + index[f.name] = mt + index[f.stem] = mt + return index + + def _require_auth(request: Request): return request.session.get("authenticated") is True @@ -66,20 +95,7 @@ async def admin_get_photos( photos = [p for p in all_photos if _is_image(p)] # Enrichit chaque photo avec mtime depuis le disque - # Indexe par stem ET par nom complet pour couvrir les deux formats d'id - mtime_index: dict[str, float] = {} - for media_dir_candidate in [ - media_dir, - Path("/home/pi/photobooth-data/media/processed_full"), - Path("/home/pi/photobooth-data/media"), - ]: - if media_dir_candidate.exists(): - for f in media_dir_candidate.iterdir(): - if f.is_file(): - mt = f.stat().st_mtime - mtime_index[f.name] = mt # uuid.jpg - mtime_index[f.stem] = mt # uuid - break + mtime_index = _build_mtime_index(media_dir) for p in photos: pid = _get_id(p) diff --git a/backend/api/gallery.py b/backend/api/gallery.py index 6690530..ad2fece 100644 --- a/backend/api/gallery.py +++ b/backend/api/gallery.py @@ -50,20 +50,24 @@ async def api_gallery_photos( if ev: started_at = ev["started_at"] or 0.0 ended_at = ev["ended_at"] or datetime.now().timestamp() - # Construire un index mtime par stem ET par nom complet + # Construire un index mtime : cherche dans media_dir et ses sous-dossiers connus mtime_index: dict[str, float] = {} - for media_dir_candidate in [ - Path(cfg.photobooth.media_dir), + base = Path(cfg.photobooth.media_dir) + checked: set = set() + for candidate in [ + base, + base / "processed_full", Path("/home/pi/photobooth-data/media/processed_full"), Path("/home/pi/photobooth-data/media"), ]: - if media_dir_candidate.exists(): - for f in media_dir_candidate.iterdir(): - if f.is_file(): - mt = f.stat().st_mtime - mtime_index[f.name] = mt # uuid.jpg - mtime_index[f.stem] = mt # uuid - break + if not candidate.exists() or candidate in checked: + continue + checked.add(candidate) + for f in candidate.iterdir(): + if f.is_file() and f.suffix.lower() in (".jpg", ".jpeg", ".png"): + mt = f.stat().st_mtime + mtime_index[f.name] = mt + mtime_index[f.stem] = mt def _in_event(p: dict) -> bool: pid = _get_id(p) diff --git a/frontend/templates/admin/gallery.html b/frontend/templates/admin/gallery.html index 9198aa6..aaa80de 100644 --- a/frontend/templates/admin/gallery.html +++ b/frontend/templates/admin/gallery.html @@ -14,7 +14,7 @@ {% block head %}