Files
minstrel/internal/coverart/sidecar.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

95 lines
3.3 KiB
Go

// 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"
"github.com/Fabled-Sword/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 ""
}