// Package coverart finds and fetches album cover artwork. // // Two-layer model: a directory-walking sidecar lookup (cover.jpg / folder.jpg // next to the audio files) and a MusicBrainz Cover Art Archive (MBCAA) // fallback for albums with an MBID but no sidecar. The Enricher orchestrates // the two; the API and Subsonic packages reuse the sidecar helper directly. package coverart import ( "cmp" "context" "os" "path/filepath" "slices" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" ) // SidecarNames is the lookup order for cover art living next to audio files. // Matches common library conventions: cover.* wins over folder.*; JPEG wins // over PNG when both are present. var SidecarNames = []string{ "cover.jpg", "cover.jpeg", "cover.png", "folder.jpg", "folder.jpeg", "folder.png", } // FindSidecar returns the absolute path to a sidecar cover image in // albumDir, or "" when none of the conventional names are present as a // regular file. Directories named like sidecar files are skipped. func FindSidecar(albumDir string) string { for _, name := range SidecarNames { candidate := filepath.Join(albumDir, name) info, err := os.Stat(candidate) if err != nil || info.IsDir() { continue } return candidate } return "" } // ResolveAlbumPath returns the on-disk path to an album's cover image, // preferring the explicit album.cover_art_path when set and the file // exists, falling back to a sidecar (cover.jpg / folder.jpg) next to the // first track in the album's directory. "" means no art was found. // // Shared by internal/api/media.go (browser endpoint) and // internal/subsonic/stream.go (Subsonic endpoint); they were byte-identical // duplicates before extraction. func ResolveAlbumPath(ctx context.Context, q *dbq.Queries, album dbq.Album) string { if album.CoverArtPath != nil && *album.CoverArtPath != "" { if _, err := os.Stat(*album.CoverArtPath); err == nil { return *album.CoverArtPath } } tracks, err := q.ListTracksByAlbum(ctx, dbq.ListTracksByAlbumParams{AlbumID: album.ID}) if err != nil || len(tracks) == 0 { return "" } return FindSidecar(filepath.Dir(tracks[0].FilePath)) } // artistAlbumsTried bounds how many of an artist's albums ResolveArtistPath // looks through for a cover, so an artist with many coverless albums costs a // few lookups rather than one per album. const artistAlbumsTried = 10 // ResolveArtistPath returns the image file to show for an artist: the // artist picture the artist-art providers fetched, if its file is there, or // else the cover of the artist's most recently added album that has one, // which is what Minstrel's artist cards show. "" means no image was found. func ResolveArtistPath(ctx context.Context, q *dbq.Queries, artist dbq.Artist) string { if artist.ArtistThumbPath != nil && *artist.ArtistThumbPath != "" { if _, err := os.Stat(*artist.ArtistThumbPath); err == nil { return *artist.ArtistThumbPath } } albums, err := q.ListAlbumsByArtist(ctx, artist.ID) if err != nil { return "" } slices.SortStableFunc(albums, func(a, b dbq.Album) int { return cmp.Compare(b.CreatedAt.Time.UnixNano(), a.CreatedAt.Time.UnixNano()) }) for i, album := range albums { if i == artistAlbumsTried { break } if path := ResolveAlbumPath(ctx, q, album); path != "" { return path } } return "" }