feat: show real on-disk tracklist in the Library album modal (#9)
The Library modal showed MusicBrainz's canonical tracklist, hiding incomplete downloads / edition or track-order mismatches, and showed nothing for albums without an rgMbid. Now it shows the REAL files on disk. - LibraryItem gains trackNames String[] (migration add_library_track_names): the album's on-disk "## Title.ext" audio filenames. - Captured at import (pipeline._import lists the final album folder) and at scan (scan._record_owned now also refreshes trackNames on re-scan via ON CONFLICT DO UPDATE + xmax=0 to preserve the newly-imported flag). New shared library.list_audio_files() helper. - /api/library exposes trackNames; the AlbumModal prefers on-disk tracks when present (parsed "## Title" → position/title, zero network, labeled "On disk · N tracks"), falling back to the MusicBrainz listing otherwise. Items imported before this column show the MB fallback until re-scanned. worker 221 tests / 7-skip, web 153, tsc + build clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -26,6 +26,20 @@ _COVER_STEMS = {"cover", "folder", "front"}
|
||||
_COVER_EXT = {".jpg", ".jpeg", ".png"}
|
||||
|
||||
|
||||
def list_audio_files(folder: str) -> list[str]:
|
||||
"""The album's on-disk audio filenames (basenames, sorted) — captured into
|
||||
LibraryItem.trackNames so the UI can show the real tracks on disk. Top-level only
|
||||
(albums are one flat folder here); returns [] if the folder is missing/unreadable."""
|
||||
try:
|
||||
names = [
|
||||
n for n in os.listdir(folder)
|
||||
if os.path.isfile(os.path.join(folder, n)) and os.path.splitext(n)[1].lower() in _AUDIO_EXT
|
||||
]
|
||||
except OSError:
|
||||
return []
|
||||
return sorted(names)
|
||||
|
||||
|
||||
def staging_dir(staging_root: str, job_id: str) -> str:
|
||||
"""A per-job download staging directory under `staging_root`. The staging root may be a
|
||||
volume separate from the library (e.g. local disk while the library is an SMB share):
|
||||
|
||||
@@ -9,7 +9,7 @@ import psycopg
|
||||
|
||||
from lyra_worker.adapters.base import SourceAdapter
|
||||
from lyra_worker.confidence import score_confidence
|
||||
from lyra_worker.library import album_dir, import_album, staging_dir
|
||||
from lyra_worker.library import album_dir, import_album, list_audio_files, staging_dir
|
||||
from lyra_worker.quality import quality_class
|
||||
from lyra_worker.ranker import rank_candidates
|
||||
from lyra_worker.types import Candidate, MBTarget
|
||||
@@ -186,18 +186,19 @@ def _mark_chosen(conn: psycopg.Connection, job_id: str, source_ref: str) -> None
|
||||
def _import(conn: psycopg.Connection, job_id: str, target: MBTarget,
|
||||
winner: Candidate, path: str) -> None:
|
||||
request_id = _request_id(conn, job_id)
|
||||
track_names = list_audio_files(path) # the real files just imported into the album folder
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
'INSERT INTO "LibraryItem" (id, "requestId", artist, album, path, source, '
|
||||
'format, "qualityClass", "importedAt") '
|
||||
"VALUES (gen_random_uuid()::text, %s, %s, %s, %s, %s, %s, %s, now()) "
|
||||
'format, "qualityClass", "trackNames", "importedAt") '
|
||||
"VALUES (gen_random_uuid()::text, %s, %s, %s, %s, %s, %s, %s, %s, now()) "
|
||||
'ON CONFLICT (artist, album) DO UPDATE SET '
|
||||
' "requestId" = EXCLUDED."requestId", path = EXCLUDED.path, source = EXCLUDED.source, '
|
||||
' format = EXCLUDED.format, "qualityClass" = EXCLUDED."qualityClass", '
|
||||
' "importedAt" = now() '
|
||||
' "trackNames" = EXCLUDED."trackNames", "importedAt" = now() '
|
||||
'WHERE EXCLUDED."qualityClass" > "LibraryItem"."qualityClass"',
|
||||
(request_id, target.artist, target.album, path, winner.source,
|
||||
winner.quality.fmt, quality_class(winner.quality)),
|
||||
winner.quality.fmt, quality_class(winner.quality), track_names),
|
||||
)
|
||||
cur.execute(
|
||||
"UPDATE \"Request\" SET status = 'completed' WHERE id = %s", (request_id,)
|
||||
|
||||
@@ -5,6 +5,7 @@ from dataclasses import dataclass
|
||||
|
||||
import psycopg
|
||||
|
||||
from lyra_worker.library import list_audio_files
|
||||
from lyra_worker.probe import AudioProbe
|
||||
|
||||
_AUDIO_EXT = {".flac", ".mp3", ".m4a", ".opus", ".ogg", ".aac", ".wav"}
|
||||
@@ -80,14 +81,18 @@ def _record_owned(conn, target, quality_class: int, fmt: str, path: str, watched
|
||||
' "watchedArtistId" = COALESCE("MonitoredRelease"."watchedArtistId", EXCLUDED."watchedArtistId")',
|
||||
(watched_id, target.artist_mbid, target.artist, target.rg_mbid, target.album, quality_class),
|
||||
)
|
||||
track_names = list_audio_files(path) # capture the real on-disk tracks
|
||||
cur.execute(
|
||||
'INSERT INTO "LibraryItem" (id, "requestId", artist, album, path, source, format, '
|
||||
'"qualityClass", "importedAt") '
|
||||
"VALUES (gen_random_uuid()::text, NULL, %s, %s, %s, 'scan', %s, %s, now()) "
|
||||
'ON CONFLICT (artist, album) DO NOTHING',
|
||||
(target.artist, target.album, path, fmt, quality_class),
|
||||
'"qualityClass", "trackNames", "importedAt") '
|
||||
"VALUES (gen_random_uuid()::text, NULL, %s, %s, %s, 'scan', %s, %s, %s, now()) "
|
||||
# refresh trackNames on re-scan (populates items imported before this column);
|
||||
# xmax=0 ⇒ this was an INSERT (a genuinely new library item) vs an UPDATE.
|
||||
'ON CONFLICT (artist, album) DO UPDATE SET "trackNames" = EXCLUDED."trackNames" '
|
||||
"RETURNING (xmax = 0) AS inserted",
|
||||
(target.artist, target.album, path, fmt, quality_class, track_names),
|
||||
)
|
||||
newly = cur.rowcount == 1 # a new LibraryItem means this album wasn't recorded before
|
||||
newly = cur.fetchone()[0] # a new LibraryItem means this album wasn't recorded before
|
||||
conn.commit()
|
||||
return newly
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from lyra_worker.library import list_audio_files
|
||||
|
||||
|
||||
def test_lists_sorted_audio_basenames_only(tmp_path):
|
||||
(tmp_path / "02 Two.flac").write_bytes(b"")
|
||||
(tmp_path / "01 One.flac").write_bytes(b"")
|
||||
(tmp_path / "10 Ten.mp3").write_bytes(b"")
|
||||
(tmp_path / "cover.jpg").write_bytes(b"") # non-audio excluded
|
||||
(tmp_path / "notes.txt").write_bytes(b"") # non-audio excluded
|
||||
(tmp_path / "Disc 1").mkdir() # a subdir is not a file
|
||||
|
||||
assert list_audio_files(str(tmp_path)) == ["01 One.flac", "02 Two.flac", "10 Ten.mp3"]
|
||||
|
||||
|
||||
def test_missing_dir_returns_empty():
|
||||
assert list_audio_files("/no/such/album/dir") == []
|
||||
|
||||
|
||||
def test_empty_dir_returns_empty(tmp_path):
|
||||
assert list_audio_files(str(tmp_path)) == []
|
||||
Reference in New Issue
Block a user