library: fix Post ID sort for templated filenames

Post ID sort used filepath.stem which sorted templated filenames
like artist_12345.jpg alphabetically instead of by post ID. Now
resolves post_id via library_meta DB lookup, falls back to digit-stem
for legacy files, unknowns sort to the end.
This commit is contained in:
pax 2026-04-11 21:59:20 -05:00
parent 52b76dfc83
commit 5e8035cb1d

View File

@ -274,14 +274,18 @@ class LibraryView(QWidget):
def _sort_files(self) -> None: def _sort_files(self) -> None:
mode = self._sort_combo.currentText() mode = self._sort_combo.currentText()
if mode == "Post ID": if mode == "Post ID":
# Numeric sort by post id (filename stem). Library files are # Numeric sort by post id. Resolves templated filenames
# named {post_id}.{ext} in normal usage; anything with a # (e.g. artist_12345.jpg) via library_meta DB lookup, falls
# non-digit stem (someone manually dropped a file in) sorts # back to digit-stem parsing for legacy files. Anything
# to the end alphabetically so the numeric ordering of real # without a resolvable post_id sorts to the end alphabetically.
# posts isn't disrupted by stray names.
def _key(p: Path) -> tuple: def _key(p: Path) -> tuple:
stem = p.stem if self._db:
return (0, int(stem)) if stem.isdigit() else (1, stem.lower()) pid = self._db.get_library_post_id_by_filename(p.name)
if pid is not None:
return (0, pid)
if p.stem.isdigit():
return (0, int(p.stem))
return (1, p.stem.lower())
self._files.sort(key=_key) self._files.sort(key=_key)
elif mode == "Size": elif mode == "Size":
self._files.sort(key=lambda p: p.stat().st_size, reverse=True) self._files.sort(key=lambda p: p.stat().st_size, reverse=True)