From 81fc4d93eb76f4bdd3765c8e61949d455fdda91c Mon Sep 17 00:00:00 2001 From: pax Date: Thu, 9 Apr 2026 17:45:20 -0500 Subject: [PATCH] main_window: library tab info panel + preview work for templated files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two more digit-stem-only callsites I missed in the saved-dot fix sweep. _set_library_info and _show_library_post both did 'if not stem.isdigit(): return' before consulting library_meta or building the toolbar Post. Templated files (post-template-refactor saves like 12345_hatsune_miku.jpg) bailed out silently — clicking one in the Library tab left the info panel showing the previous selection's data and the toolbar actions did nothing. Extracted a small helper _post_id_from_library_path that resolves either layout: look up library_meta.filename first (templated), fall back to int(stem) for legacy digit-stem files. Both call sites go through the helper now. Same pattern as the find_library_files / _is_post_in_library fixes from the earlier saved-dot bug. With this commit there are no remaining "is templated file in the library?" callsites that fall back to digit-stem matching alone — every check is format-agnostic via the DB. --- booru_viewer/gui/main_window.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/booru_viewer/gui/main_window.py b/booru_viewer/gui/main_window.py index 34ad606..821ecee 100644 --- a/booru_viewer/gui/main_window.py +++ b/booru_viewer/gui/main_window.py @@ -1447,16 +1447,32 @@ class BooruApp(QMainWindow): if evicted_thumbs: log.info(f"Auto-evicted {evicted_thumbs} thumbnails") + def _post_id_from_library_path(self, path: Path) -> int | None: + """Resolve a library file path back to its post_id. + + Templated filenames look up library_meta.filename (post-refactor + saves like 12345_hatsune_miku.jpg). Legacy v0.2.3 digit-stem + files (12345.jpg) use int(stem) directly. Returns None if + neither resolves — e.g. an unrelated file dropped into the + library directory. + """ + pid = self._db.get_library_post_id_by_filename(path.name) + if pid is not None: + return pid + if path.stem.isdigit(): + return int(path.stem) + return None + def _set_library_info(self, path: str) -> None: """Update info panel with library metadata for the given file.""" - stem = Path(path).stem - if not stem.isdigit(): + post_id = self._post_id_from_library_path(Path(path)) + if post_id is None: return - meta = self._db.get_library_meta(int(stem)) + meta = self._db.get_library_meta(post_id) if meta: from ..core.api.base import Post p = Post( - id=int(stem), file_url=meta.get("file_url", ""), + id=post_id, file_url=meta.get("file_url", ""), preview_url=None, tags=meta.get("tags", ""), score=meta.get("score", 0), rating=meta.get("rating"), source=meta.get("source"), tag_categories=meta.get("tag_categories", {}), @@ -1475,10 +1491,11 @@ class BooruApp(QMainWindow): self._set_preview_media(path, Path(path).name) self._update_fullscreen(path, Path(path).name) self._set_library_info(path) - # Build a Post from library metadata so toolbar actions work - stem = Path(path).stem - if stem.isdigit(): - post_id = int(stem) + # Build a Post from library metadata so toolbar actions work. + # Templated filenames go through library_meta.filename; + # legacy digit-stem files use int(stem). + post_id = self._post_id_from_library_path(Path(path)) + if post_id is not None: from ..core.api.base import Post meta = self._db.get_library_meta(post_id) or {} post = Post(