🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Fix `cover.jpg` discovery
Deluan890ca64f51b5
diff --git a/engine/cover.go b/engine/cover.goindex 055147495..180da8e8b 100644--- a/engine/cover.go+++ b/engine/cover.go@@ -89,6 +89,7 @@ func (c *cover) Get(ctx context.Context, id string, size int, out io.Writer) errfunc (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastUpdated time.Time, err error) {// If id is an album cover IDif strings.HasPrefix(id, "al-") {+ log.Trace(ctx, "Looking for album art", "id", id)id = strings.TrimPrefix(id, "al-")var al *model.Albumal, err = c.ds.Album(ctx).Get(id)@@ -101,6 +102,7 @@ func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastUreturn al.CoverArtPath, al.UpdatedAt, err}+ log.Trace(ctx, "Looking for media file art", "id", id)// if id is a mediafile cover idvar mf *model.MediaFilemf, err = c.ds.MediaFile(ctx).Get(id)@@ -112,6 +114,7 @@ func (c *cover) getCoverPath(ctx context.Context, id string) (path string, lastU}// if the mediafile does not have a coverArt, fallback to the album cover+ log.Trace(ctx, "Media file does not contain art. Falling back to album art", "id", id, "albumId", "al-"+mf.AlbumID)return c.getCoverPath(ctx, "al-"+mf.AlbumID)}diff --git a/persistence/album_repository.go b/persistence/album_repository.goindex fb4240ad9..3a4f3e59e 100644--- a/persistence/album_repository.go+++ b/persistence/album_repository.go@@ -121,11 +121,12 @@ func (r *albumRepository) Refresh(ids ...string) error {SongArtists stringYears stringDiscSubtitles string+ Path string}var albums []refreshAlbumsel := Select(`f.album_id as id, f.album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id,f.sort_album_name, f.sort_artist_name, f.sort_album_artist_name,- f.order_album_name, f.order_album_artist_name,+ f.order_album_name, f.order_album_artist_name, f.path,f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration,count(*) as song_count, a.id as current_id,f2.id as cover_art_id, f2.path as cover_art_path, f2.has_cover_art,@@ -144,12 +145,13 @@ func (r *albumRepository) Refresh(ids ...string) error {toUpdate := 0for _, al := range albums {if !al.HasCoverArt || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {- if path := getCoverFromPath(al.CoverArtPath, al.HasCoverArt); path != "" {+ if path := getCoverFromPath(al.Path, al.CoverArtPath); path != "" {al.CoverArtId = "al-" + al.IDal.CoverArtPath = path} else if !al.HasCoverArt {al.CoverArtId = ""}+ log.Trace(r.ctx, "Found album art", "id", al.ID, "name", al.Name, "coverArtPath", al.CoverArtPath, "coverArtId", al.CoverArtId, "hasCoverArt", al.HasCoverArt)}if al.Compilation {@@ -201,8 +203,8 @@ func getMinYear(years string) int {// available choices, or an error occurs, an empty string is returned. If HasEmbeddedCover is true,// and 'embedded' is matched among eligible choices, GetCoverFromPath will return early with an// empty path.-func getCoverFromPath(path string, hasEmbeddedCover bool) string {- n, err := os.Open(filepath.Dir(path))+func getCoverFromPath(mediaPath string, embeddedPath string) string {+ n, err := os.Open(filepath.Dir(mediaPath))if err != nil {return ""}@@ -216,7 +218,7 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string {for _, p := range strings.Split(conf.Server.CoverArtPriority, ",") {pat := strings.ToLower(strings.TrimSpace(p))if pat == "embedded" {- if hasEmbeddedCover {+ if embeddedPath != "" {return ""}continue@@ -225,7 +227,7 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string {for _, name := range names {match, _ := filepath.Match(pat, strings.ToLower(name))if match && utils.IsImageFile(name) {- return filepath.Join(filepath.Dir(path), name)+ return filepath.Join(filepath.Dir(mediaPath), name)}}}diff --git a/persistence/album_repository_test.go b/persistence/album_repository_test.goindex d0d291745..3f8c3c616 100644--- a/persistence/album_repository_test.go+++ b/persistence/album_repository_test.go@@ -104,34 +104,35 @@ var _ = Describe("AlbumRepository", func() {}testPath := filepath.Join(testFolder, "somefile.test")+ embeddedPath := filepath.Join(testFolder, "somefile.mp3")It("returns audio file for embedded cover", func() {conf.Server.CoverArtPriority = "embedded, cover.*, front.*"- Expect(getCoverFromPath(testPath, true)).To(Equal(""))+ Expect(getCoverFromPath(testPath, embeddedPath)).To(Equal(""))})It("returns external file when no embedded cover exists", func() {conf.Server.CoverArtPriority = "embedded, cover.*, front.*"- Expect(getCoverFromPath(testPath, false)).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))+ Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))})It("returns embedded cover even if not first choice", func() {conf.Server.CoverArtPriority = "something.png, embedded, cover.*, front.*"- Expect(getCoverFromPath(testPath, true)).To(Equal(""))+ Expect(getCoverFromPath(testPath, embeddedPath)).To(Equal(""))})It("returns first correct match case-insensitively", func() {conf.Server.CoverArtPriority = "embedded, cover.jpg, front.svg, front.png"- Expect(getCoverFromPath(testPath, false)).To(Equal(filepath.Join(testFolder, "FRONT.PNG")))+ Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "FRONT.PNG")))})It("returns match for embedded pattern", func() {conf.Server.CoverArtPriority = "embedded, cover.jp?g, front.png"- Expect(getCoverFromPath(testPath, false)).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))+ Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))})It("returns empty string if no match was found", func() {conf.Server.CoverArtPriority = "embedded, cover.jpg, front.apng"- Expect(getCoverFromPath(testPath, false)).To(Equal(""))+ Expect(getCoverFromPath(testPath, "")).To(Equal(""))})// Reset configuration to default.