From 403c099beda98838a93a8cab14c26dfad4a49150 Mon Sep 17 00:00:00 2001 From: pax Date: Thu, 9 Apr 2026 19:58:28 -0500 Subject: [PATCH] library: clean up library_meta on delete (templated + digit-stem) The Library tab's single-delete and multi-delete context menu actions called .unlink() directly, bypassing delete_from_library entirely. They only extracted post_id from digit-stem filenames (int(stem) if stem.isdigit()), so templated files like artist_12345.jpg got deleted from disk but left orphan library_meta rows that made get_saved_post_ids lie forever. Fix: resolve post_id via db.get_library_post_id_by_filename first (handles templated filenames), fall back to int(stem) for legacy digit-stem files, then call db.remove_library_meta(post_id) after unlinking. Both single-delete and multi-delete paths are fixed. This was the last source of orphan library_meta rows. With this fix + the earlier delete_from_library cleanup, every deletion path in the app now cleans up its meta row: - Library tab single delete (this commit) - Library tab multi delete (this commit) - Browse/preview "Unsave from Library" (via delete_from_library) - Browse multi-select "Unsave All" (via delete_from_library) - Bookmarks "Unsave from Library" (via delete_from_library) - Bookmarks multi-select "Unsave All" (via delete_from_library) --- booru_viewer/gui/library.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/booru_viewer/gui/library.py b/booru_viewer/gui/library.py index 6aa1fce..a53b4d2 100644 --- a/booru_viewer/gui/library.py +++ b/booru_viewer/gui/library.py @@ -497,10 +497,14 @@ class LibraryView(QWidget): QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, ) if reply == QMessageBox.StandardButton.Yes: - post_id = int(filepath.stem) if filepath.stem.isdigit() else None + post_id = self._db.get_library_post_id_by_filename(filepath.name) + if post_id is None and filepath.stem.isdigit(): + post_id = int(filepath.stem) filepath.unlink(missing_ok=True) lib_thumb = thumbnails_dir() / "library" / f"{filepath.stem}.jpg" lib_thumb.unlink(missing_ok=True) + if post_id is not None: + self._db.remove_library_meta(post_id) self.refresh() if post_id is not None: self.files_deleted.emit([post_id]) @@ -548,11 +552,15 @@ class LibraryView(QWidget): if reply == QMessageBox.StandardButton.Yes: deleted_ids = [] for f in files: - if f.stem.isdigit(): - deleted_ids.append(int(f.stem)) + post_id = self._db.get_library_post_id_by_filename(f.name) + if post_id is None and f.stem.isdigit(): + post_id = int(f.stem) f.unlink(missing_ok=True) lib_thumb = thumbnails_dir() / "library" / f"{f.stem}.jpg" lib_thumb.unlink(missing_ok=True) + if post_id is not None: + self._db.remove_library_meta(post_id) + deleted_ids.append(post_id) self.refresh() if deleted_ids: self.files_deleted.emit(deleted_ids)