From ac5d99c079afa94e53b561e6be0fd89d539104fd Mon Sep 17 00:00:00 2001 From: Alex Palaistras Date: Sun, 21 Jun 2020 14:37:59 +0100 Subject: [PATCH] Check MIME type for cover on refresh, display Files that match the `CoverArtPriority` setting will now be considered eligible only if their extensions are of an 'image/*' MIME type (e.g. '.png' for 'image/png', '.jpg' for 'image/jpeg'). This prevents matching files that will likely not be valid during display. In addition to the above, code for returning the cover image file from scanned data will also check against the MIME type for the path stored, instead of attempting to re-trace `CoverArtPriority` matches. This simplifies the code and bypasses a number of edge-cases related to inconsistent matching. --- engine/cover.go | 25 ++++++++++++++----------- persistence/album_repository.go | 8 +++++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/engine/cover.go b/engine/cover.go index 7aa2363a0..df054ae83 100644 --- a/engine/cover.go +++ b/engine/cover.go @@ -10,6 +10,7 @@ import ( "image/jpeg" _ "image/png" "io" + "mime" "os" "path/filepath" "strings" @@ -134,18 +135,16 @@ func (c *cover) getCover(ctx context.Context, path string, size int) (reader io. reader, err = resources.AssetFile().Open(consts.PlaceholderAlbumArt) } }() + + if path == "" { + return nil, errors.New("empty path given for cover") + } + var data []byte - err = errors.New("no matching cover found") - for _, p := range strings.Split(conf.Server.CoverArtPriority, ",") { - pat := strings.ToLower(strings.TrimSpace(p)) - if pat == "embedded" { - data, err = readFromTag(path) - } else if ok, _ := filepath.Match(pat, strings.ToLower(filepath.Base(path))); ok { - data, err = readFromFile(path) - } - if err == nil { - break - } + if isAudioFile(filepath.Ext(path)) { + data, err = readFromTag(path) + } else { + data, err = readFromFile(path) } if err != nil { @@ -208,6 +207,10 @@ func readFromFile(path string) ([]byte, error) { return buf.Bytes(), nil } +func isAudioFile(extension string) bool { + return strings.HasPrefix(mime.TypeByExtension(extension), "audio/") +} + func NewImageCache() (ImageCache, error) { return newFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems) } diff --git a/persistence/album_repository.go b/persistence/album_repository.go index 2776e5e7c..0ef49c6cd 100644 --- a/persistence/album_repository.go +++ b/persistence/album_repository.go @@ -2,6 +2,7 @@ package persistence import ( "context" + "mime" "os" "path/filepath" "sort" @@ -220,7 +221,8 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string { } for _, name := range names { - if ok, _ := filepath.Match(pat, strings.ToLower(name)); ok { + match, _ := filepath.Match(pat, strings.ToLower(name)) + if match && isImageFile(filepath.Ext(name)) { return filepath.Join(filepath.Dir(path), name) } } @@ -229,6 +231,10 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string { return "" } +func isImageFile(extension string) bool { + return strings.HasPrefix(mime.TypeByExtension(extension), "image/") +} + func (r *albumRepository) purgeEmpty() error { del := Delete(r.tableName).Where("id not in (select distinct(album_id) from media_file)") c, err := r.executeSQL(del)