fix(coverart): album art goes to Minstrel's data folder, never beside the music (#8286)
release / go (push) Successful in 1m58s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m24s
release / android (push) Successful in 4m33s
release / Build signed APK (push) Successful in 5m4s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped

Another program files the library now. The cover enricher wrote a
cover.jpg into each album folder it found art for, from the files' tags or
from a provider, and an admin's cover retry deleted that file again, even
if the other program had since put its own cover at that path.

The enricher now keeps every cover it writes under
<data>/album-art/<album>/cover.jpg, and a retry removes a cover only from
there. A cover.jpg an older build left beside the music stays, and is read
as the library's own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 23:24:55 -05:00
co-authored by Claude Opus 5.5
parent c073e41613
commit 56285881aa
5 changed files with 108 additions and 53 deletions
+56 -9
View File
@@ -36,7 +36,21 @@ func newTestEnricher(t *testing.T, pool *pgxpool.Pool) *Enricher {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
return NewEnricher(pool, discardLogger(), s)
return NewEnricher(pool, discardLogger(), s, t.TempDir())
}
// cachedCover is where e keeps the cover it writes for an album.
func cachedCover(e *Enricher, albumID pgtype.UUID) string {
return filepath.Join(e.artCacheRoot(), uuidString(albumID), "cover.jpg")
}
// assertNoCoverBeside fails when a cover.jpg sits in the album's folder:
// the enricher reads the library and never writes into it.
func assertNoCoverBeside(t *testing.T, albumDir string) {
t.Helper()
if _, err := os.Stat(filepath.Join(albumDir, "cover.jpg")); !os.IsNotExist(err) {
t.Errorf("a cover.jpg was written beside the music in %s (stat err %v)", albumDir, err)
}
}
// seedAlbumWithTrack creates an artist + album + track triple in a temp
@@ -145,7 +159,7 @@ func TestEnrichAlbum_ProviderFetchSuccess(t *testing.T) {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
e := NewEnricher(pool, discardLogger(), s)
e := NewEnricher(pool, discardLogger(), s, t.TempDir())
if err := e.EnrichAlbum(context.Background(), id); err != nil {
t.Fatalf("enrich: %v", err)
@@ -158,10 +172,11 @@ func TestEnrichAlbum_ProviderFetchSuccess(t *testing.T) {
if row.CoverArtSource == nil || *row.CoverArtSource != "mbcaa" {
t.Errorf("source = %v, want 'mbcaa'", row.CoverArtSource)
}
expectedPath := filepath.Join(dir, "cover.jpg")
expectedPath := cachedCover(e, id)
if row.CoverArtPath == nil || *row.CoverArtPath != expectedPath {
t.Errorf("path = %v, want %s", row.CoverArtPath, expectedPath)
}
assertNoCoverBeside(t, dir)
body, err := os.ReadFile(expectedPath)
if err != nil {
t.Fatalf("read file: %v", err)
@@ -193,7 +208,7 @@ func TestEnrichAlbum_All404_RecordsNone(t *testing.T) {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
e := NewEnricher(pool, discardLogger(), s)
e := NewEnricher(pool, discardLogger(), s, t.TempDir())
if err := e.EnrichAlbum(context.Background(), id); err != nil {
t.Fatalf("enrich: %v", err)
@@ -306,15 +321,16 @@ func TestRetryAlbum_DeletesWrittenFileAndRefetches(t *testing.T) {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
e := NewEnricher(pool, discardLogger(), s)
e := NewEnricher(pool, discardLogger(), s, t.TempDir())
if err := e.EnrichAlbum(context.Background(), id); err != nil {
t.Fatalf("first enrich: %v", err)
}
written := filepath.Join(dir, "cover.jpg")
written := cachedCover(e, id)
if _, err := os.Stat(written); err != nil {
t.Fatalf("expected file at %s: %v", written, err)
}
assertNoCoverBeside(t, dir)
if err := e.RetryAlbum(context.Background(), id); err != nil {
t.Fatalf("retry: %v", err)
@@ -328,6 +344,36 @@ func TestRetryAlbum_DeletesWrittenFileAndRefetches(t *testing.T) {
}
}
// A cover an older Minstrel wrote beside the music is recorded as fetched
// from a provider. Retrying the album leaves that file alone: it is in a
// folder another program files into, and may be that program's by now.
func TestRetryAlbum_LeavesACoverBesideTheMusic(t *testing.T) {
pool := dbtest.Pool(t)
id, dir := seedAlbumWithTrack(t, pool, "Beside", "Artist", "")
beside := filepath.Join(dir, "cover.jpg")
if err := os.WriteFile(beside, []byte("filed"), 0o644); err != nil {
t.Fatal(err)
}
mbcaa := "mbcaa"
if err := dbq.New(pool).SetAlbumCover(context.Background(), dbq.SetAlbumCoverParams{
ID: id, Column2: beside, CoverArtSource: &mbcaa,
}); err != nil {
t.Fatal(err)
}
e := newTestEnricher(t, pool)
if err := e.RetryAlbum(context.Background(), id); err != nil {
t.Fatalf("retry: %v", err)
}
if body, err := os.ReadFile(beside); err != nil || string(body) != "filed" {
t.Fatalf("the cover beside the music was removed or changed: body %q, err %v", body, err)
}
row, _ := dbq.New(pool).GetAlbumWithFirstTrackPath(context.Background(), id)
if row.CoverArtSource == nil || *row.CoverArtSource != "sidecar" {
t.Errorf("source after retry = %v, want 'sidecar' (the file is read as the library's own)", row.CoverArtSource)
}
}
func TestEnrichRetryMissing_DrainsNullAndNone(t *testing.T) {
pool := dbtest.Pool(t)
// Three albums:
@@ -430,7 +476,7 @@ func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
e := NewEnricher(pool, discardLogger(), s)
e := NewEnricher(pool, discardLogger(), s, t.TempDir())
if err := e.EnrichAlbum(context.Background(), id); err != nil {
t.Fatalf("enrich: %v", err)
@@ -443,9 +489,10 @@ func TestEnrichAlbum_MultiProviderChain(t *testing.T) {
if row.CoverArtSource == nil || *row.CoverArtSource != "test-provider-b" {
t.Errorf("source = %v, want 'test-provider-b' (provider B should win)", row.CoverArtSource)
}
if body, err := os.ReadFile(filepath.Join(dir, "cover.jpg")); err != nil || string(body) != "PROVIDER_B_BYTES" {
if body, err := os.ReadFile(cachedCover(e, id)); err != nil || string(body) != "PROVIDER_B_BYTES" {
t.Errorf("cover.jpg body = %q, err = %v", body, err)
}
assertNoCoverBeside(t, dir)
}
// TestEnrichAlbum_TransientLeavesNull verifies that a transient error
@@ -472,7 +519,7 @@ func TestEnrichAlbum_TransientLeavesNull(t *testing.T) {
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
e := NewEnricher(pool, discardLogger(), s)
e := NewEnricher(pool, discardLogger(), s, t.TempDir())
if err := e.EnrichAlbum(context.Background(), id); err != nil {
t.Fatalf("enrich: %v", err)