Make sure album is updated if external cover changes

This commit is contained in:
Deluan 2022-12-23 10:51:21 -05:00 committed by Deluan Quintão
parent f5719a7571
commit 9ec349dce0
4 changed files with 28 additions and 11 deletions

View File

@ -182,7 +182,9 @@ func (a *artwork) fromCoverArtPriority(ctx context.Context, priority string, al
ff = append(ff, fromTag(al.EmbedArtPath), fromFFmpegTag(ctx, a.ffmpeg, al.EmbedArtPath))
continue
}
ff = append(ff, fromExternalFile(ctx, al.ImageFiles, pattern))
if al.ImageFiles != "" {
ff = append(ff, fromExternalFile(ctx, al.ImageFiles, pattern))
}
}
return ff
}
@ -201,6 +203,7 @@ func fromExternalFile(ctx context.Context, files string, pattern string) fromFun
}
f, err := os.Open(file)
if err != nil {
log.Warn(ctx, "Could not open cover art file", "file", file, err)
continue
}
return f, file, err

View File

@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/log"
@ -93,7 +94,11 @@ func (r *refresher) refreshAlbums(ids ...string) error {
for _, group := range grouped {
songs := model.MediaFiles(group)
a := songs.ToAlbum()
a.ImageFiles = r.getImageFiles(songs.Dirs())
var updatedAt time.Time
a.ImageFiles, updatedAt = r.getImageFiles(songs.Dirs())
if updatedAt.After(a.UpdatedAt) {
a.UpdatedAt = updatedAt
}
err := repo.Put(&a)
if err != nil {
return err
@ -102,14 +107,19 @@ func (r *refresher) refreshAlbums(ids ...string) error {
return nil
}
func (r *refresher) getImageFiles(dirs []string) string {
func (r *refresher) getImageFiles(dirs []string) (string, time.Time) {
var imageFiles []string
var updatedAt time.Time
for _, dir := range dirs {
for _, img := range r.dirMap[dir].Images {
stats := r.dirMap[dir]
for _, img := range stats.Images {
imageFiles = append(imageFiles, filepath.Join(dir, img))
}
if stats.ImagesUpdatedAt.After(updatedAt) {
updatedAt = stats.ImagesUpdatedAt
}
}
return strings.Join(imageFiles, string(filepath.ListSeparator))
return strings.Join(imageFiles, string(filepath.ListSeparator)), updatedAt
}
func (r *refresher) refreshArtists(ids ...string) error {

View File

@ -20,6 +20,7 @@ type (
Path string
ModTime time.Time
Images []string
ImagesUpdatedAt time.Time
HasPlaylist bool
AudioFilesCount uint32
}
@ -93,12 +94,15 @@ func loadDir(ctx context.Context, dirPath string) ([]string, *dirStats, error) {
if fileInfo.ModTime().After(stats.ModTime) {
stats.ModTime = fileInfo.ModTime()
}
if utils.IsAudioFile(entry.Name()) {
switch {
case utils.IsAudioFile(entry.Name()):
stats.AudioFilesCount++
} else {
stats.HasPlaylist = stats.HasPlaylist || model.IsValidPlaylist(entry.Name())
if utils.IsImageFile(entry.Name()) {
stats.Images = append(stats.Images, entry.Name())
case model.IsValidPlaylist(entry.Name()):
stats.HasPlaylist = true
case utils.IsImageFile(entry.Name()):
stats.Images = append(stats.Images, entry.Name())
if fileInfo.ModTime().After(stats.ImagesUpdatedAt) {
stats.ImagesUpdatedAt = fileInfo.ModTime()
}
}
}

View File

@ -127,7 +127,7 @@ func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {
log.Warn(ctx, "Error removing key from cache", "cache", fc.name, "key", key, err)
}
} else {
log.Trace(ctx, "File stored in cache", "cache", fc.name, "key", key)
log.Trace(ctx, "File successfully stored in cache", "cache", fc.name, "key", key)
}
}()
}