Move cover art discovery (temporarily) to model

This commit is contained in:
Deluan
2022-12-19 11:13:39 -05:00
committed by Deluan Quintão
parent 28e7371d93
commit 83ff44f5f4
2 changed files with 112 additions and 1 deletions
+54 -1
View File
@@ -2,9 +2,12 @@ package model
import (
"mime"
"os"
"path/filepath"
"strings"
"time"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/number"
@@ -75,6 +78,7 @@ func (mfs MediaFiles) ToAlbum() Album {
var songArtistIds []string
var mbzAlbumIds []string
var comments []string
var firstPath string
for _, m := range mfs {
// We assume these attributes are all the same for all songs on an album
a.ID = m.AlbumID
@@ -115,8 +119,11 @@ func (mfs MediaFiles) ToAlbum() Album {
m.SortAlbumName, m.SortAlbumArtistName, m.SortArtistName,
m.DiscSubtitle)
if m.HasCoverArt {
// TODO CoverArtPriority
a.CoverArtId = m.ID
a.CoverArtPath = m.Path
}
if firstPath == "" {
firstPath = m.Path
}
}
comments = slices.Compact(comments)
@@ -132,6 +139,14 @@ func (mfs MediaFiles) ToAlbum() Album {
slices.Sort(songArtistIds)
a.AllArtistIDs = strings.Join(slices.Compact(songArtistIds), " ")
a.MbzAlbumID = slice.MostFrequent(mbzAlbumIds)
if a.CoverArtPath == "" || !strings.HasPrefix(conf.Server.CoverArtPriority, "embedded") {
if path := getCoverFromPath(firstPath, a.CoverArtPath); path != "" {
a.CoverArtId = "al-" + a.ID
a.CoverArtPath = path
}
}
return a
}
@@ -169,6 +184,44 @@ func fixAlbumArtist(a Album, albumArtistIds []string) Album {
return a
}
// GetCoverFromPath accepts a path to a file, and returns a path to an eligible cover image from the
// file's directory (as configured with CoverArtPriority). If no cover file is found, among
// 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.
// TODO: Move to scanner (or at least out of here)
func getCoverFromPath(mediaPath string, embeddedPath string) string {
n, err := os.Open(filepath.Dir(mediaPath))
if err != nil {
return ""
}
defer n.Close()
names, err := n.Readdirnames(-1)
if err != nil {
return ""
}
for _, p := range strings.Split(conf.Server.CoverArtPriority, ",") {
pat := strings.ToLower(strings.TrimSpace(p))
if pat == "embedded" {
if embeddedPath != "" {
return ""
}
continue
}
for _, name := range names {
match, _ := filepath.Match(pat, strings.ToLower(name))
if match && utils.IsImageFile(name) {
return filepath.Join(filepath.Dir(mediaPath), name)
}
}
}
return ""
}
type MediaFileRepository interface {
CountAll(options ...QueryOptions) (int64, error)
Exists(id string) (bool, error)