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.
This commit is contained in:
Alex Palaistras 2020-06-21 14:37:59 +01:00 committed by Deluan Quintão
parent d9c991e325
commit ac5d99c079
2 changed files with 21 additions and 12 deletions

View File

@ -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)
}

View File

@ -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)