From 98f436947eca09f96ec473845e053bbbe6b798b8 Mon Sep 17 00:00:00 2001 From: Sleeps Date: Wed, 7 Oct 2026 11:56:51 +0100 Subject: [PATCH] Add safe legacy Jellyfin thumbnail cleanup --- scene_search/maintenance.py | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/scene_search/maintenance.py b/scene_search/maintenance.py index 80fb5d1..37d4e19 100644 --- a/scene_search/maintenance.py +++ b/scene_search/maintenance.py @@ -59,3 +59,71 @@ def remove_media_by_source( shutil.rmtree(thumbs_dir / item.media_id, ignore_errors=True) return matches + + + +@dataclass(frozen=True) +class LegacyJellyfinThumbs: + media_id: str + title: str + directory: Path + files: int + bytes: int + + +def list_legacy_jellyfin_thumbs( + db_path: Path, + thumbs_dir: Path, +) -> list[LegacyJellyfinThumbs]: + """List duplicated local thumbs for Jellyfin items backed by Trickplay.""" + init_db(db_path) + with connect(db_path) as con: + rows = con.execute( + """ + SELECT id, title + FROM media + WHERE source_kind = 'jellyfin' + AND trickplay_width IS NOT NULL + ORDER BY title, id + """ + ).fetchall() + + results: list[LegacyJellyfinThumbs] = [] + for row in rows: + directory = thumbs_dir / str(row["id"]) + if not directory.is_dir(): + continue + + files = 0 + size = 0 + for path in directory.rglob("*"): + if not path.is_file(): + continue + files += 1 + try: + size += path.stat().st_size + except OSError: + pass + + results.append( + LegacyJellyfinThumbs( + media_id=str(row["id"]), + title=str(row["title"]), + directory=directory, + files=files, + bytes=size, + ) + ) + + return results + + +def remove_legacy_jellyfin_thumbs( + db_path: Path, + thumbs_dir: Path, +) -> list[LegacyJellyfinThumbs]: + """Remove only duplicated Jellyfin JPEGs; canonical vectors remain untouched.""" + matches = list_legacy_jellyfin_thumbs(db_path, thumbs_dir) + for item in matches: + shutil.rmtree(item.directory, ignore_errors=True) + return matches