mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-11 03:47:18 +02:00
* refactor(persistence): adopt generic deluan/rest repository API Pin deluan/rest to the refactor branch. REST-facing repository methods take a context and return typed values. Drop DataStore.Resource and ResourceRepository; the native API names typed repositories directly through a per-request adapter that later commits remove. * refactor(persistence): base repository helpers take a context * refactor(persistence): LibraryRepository takes a context per call * refactor(persistence): PropertyRepository takes a context per call * refactor(persistence): UserPropsRepository takes a context per call * refactor(persistence): TranscodingRepository takes a context per call * refactor(persistence): ShareRepository takes a context per call * refactor(persistence): PlayerRepository takes a context per call * refactor(persistence): RadioRepository takes a context per call * refactor(persistence): PlayQueueRepository takes a context per call * refactor(persistence): Tag and Genre repositories take a context per call * refactor(persistence): PluginRepository takes a context per call * refactor(persistence): Scrobble repositories take a context per call * refactor(persistence): FolderRepository takes a context per call * refactor(persistence): Artwork repositories take a context per call * refactor(persistence): UserRepository takes a context per call * refactor(persistence): ArtistRepository takes a context per call ReadAll no longer rewrites the shared sort mappings for the role filter; it works on a per-call copy. * test(persistence): assert artist role sort sanitization in ReadAll * refactor(persistence): AlbumRepository takes a context per call * test(persistence): pass the test context to album repository helpers * refactor(persistence): MediaFileRepository takes a context per call * refactor(persistence): Playlist repositories take a context per call * refactor(persistence): build all repositories once per store * refactor(core): REST repository wrappers are built once * refactor(persistence): repositories are stateless Remove the context field from the base repository and the per-request REST adapter. Enable the containedctx linter so no repository can hold a request context again. * chore(lint): skip containedctx in test files * refactor: share simplifications from the stateless repositories sweep Add deleteOwnedAll on sqlRepository and use it in player/share Delete to remove the duplicated bulk-delete loop; have Share.Repository() return model.ShareRepository so subsonic sharing.go drops its repeated type assertions. * chore(core): assert REST wrappers implement Persistable * chore: reformat imports * perf(persistence): build repositories on first use Each transaction store used to construct all 21 repositories up front, paying for filter and sort mapping setup the block never touched. Fields are now sync.OnceValue thunks, so a store only builds what it uses. * fix(persistence): clean plugin references per deleted user A bulk user delete that fails on a later id had already removed the earlier rows but skipped their plugin cleanup. Cleanup now runs right after each successful delete. * fix(core): unload disabled plugins even when a user delete fails A bulk delete can fail on a later id after earlier users were removed and their plugins auto-disabled. The wrapper returned before unloading, leaving those plugins running until the next successful delete or a restart. * chore(deps): pin deluan/rest to v1.0.1 Replaces the pseudo-version of the refactor branch with the tagged release. REST error messages now name the bare type (Artist, not model.Artist). * test: use the spec context instead of context.Background() Replace the context.Background()/context.TODO() calls this branch added to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so repository calls are bound to the running spec's lifetime. * test: declare the spec context once per Describe Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
281 lines
8.5 KiB
Go
281 lines
8.5 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/core/ffmpeg"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
)
|
|
|
|
// discArtworkReader resolves disc-level artwork from a library's folder images
|
|
// and embedded tags. It is used by the serving path's provisional disc read-through.
|
|
type discArtworkReader struct {
|
|
album model.Album
|
|
discNumber int
|
|
imgFiles []string // library-relative, forward-slash, no leading slash
|
|
discFoldersRel map[string]bool // library-relative folder paths
|
|
isMultiFolder bool
|
|
firstTrackRel string // library-relative; for fromTag / ffmpeg via lib.Abs
|
|
lib libraryView
|
|
// Newest ImagesUpdatedAt across the album's and this disc's folders: an image can be
|
|
// replaced without the album row changing, so this is what makes a cache key notice it.
|
|
imagesUpdatedAt time.Time
|
|
}
|
|
|
|
// cacheTime is the disc image's validity stamp: any of these moving means the selection may
|
|
// have changed.
|
|
func (d *discArtworkReader) cacheTime() time.Time {
|
|
return utils.TimeNewest(d.album.UpdatedAt, d.album.ImportedAt, d.imagesUpdatedAt)
|
|
}
|
|
|
|
func newDiscArtworkReader(ctx context.Context, ds model.DataStore, artID model.ArtworkID) (*discArtworkReader, error) {
|
|
albumID, discNumber, err := model.ParseDiscArtworkID(artID.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid disc artwork id '%s': %w", artID.ID, err)
|
|
}
|
|
|
|
al, err := ds.Album().Get(ctx, albumID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, imgFiles, albumImagesAt, err := loadAlbumFoldersPaths(ctx, ds, *al)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var imagesUpdatedAt time.Time
|
|
if albumImagesAt != nil {
|
|
imagesUpdatedAt = *albumImagesAt
|
|
}
|
|
|
|
// Query mediafiles for this album + disc to find folder associations and first track
|
|
mfs, err := ds.MediaFile().GetAll(ctx, model.QueryOptions{
|
|
Sort: "track_number",
|
|
Order: "ASC",
|
|
Filters: squirrel.Eq{"album_id": albumID, "disc_number": discNumber},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lib, err := loadLibraryView(ctx, ds, al.LibraryID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Build disc folder set and find first track. mf.Path is already library-relative.
|
|
var firstTrackRel string
|
|
for _, mf := range mfs {
|
|
if mf.Path != "" {
|
|
firstTrackRel = filepath.ToSlash(mf.Path)
|
|
break
|
|
}
|
|
}
|
|
folderIDs := slice.Unique(slice.Map(mfs, func(mf model.MediaFile) string { return mf.FolderID }))
|
|
|
|
// Resolve folder IDs to library-relative paths
|
|
discFoldersRel := make(map[string]bool)
|
|
if len(folderIDs) > 0 {
|
|
folders, err := ds.Folder().GetAll(ctx, model.QueryOptions{
|
|
Filters: squirrel.Eq{"folder.id": folderIDs},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, f := range folders {
|
|
rel := strings.TrimPrefix(path.Join(f.Path, f.Name), "/")
|
|
discFoldersRel[rel] = true
|
|
imagesUpdatedAt = utils.TimeNewest(imagesUpdatedAt, f.ImagesUpdatedAt)
|
|
}
|
|
}
|
|
|
|
return &discArtworkReader{
|
|
album: *al,
|
|
discNumber: discNumber,
|
|
imgFiles: imgFiles,
|
|
discFoldersRel: discFoldersRel,
|
|
isMultiFolder: len(al.FolderIDs) > 1,
|
|
firstTrackRel: firstTrackRel,
|
|
lib: lib,
|
|
imagesUpdatedAt: imagesUpdatedAt,
|
|
}, nil
|
|
}
|
|
|
|
// discCandidate is one DiscArtPriority entry. skip is set when the entry maps to no source at
|
|
// all, so a chain walk can say why instead of leaving a configured entry unaccounted for.
|
|
type discCandidate struct {
|
|
pattern string
|
|
resolve func() (resolution, bool)
|
|
skip string
|
|
}
|
|
|
|
func (d *discArtworkReader) discCandidates(ctx context.Context, ffmpeg ffmpeg.FFmpeg, priority string) []discCandidate {
|
|
folder := func(sf sourceFunc) func() (resolution, bool) {
|
|
return func() (resolution, bool) { return resolveFolderSource(d.lib, sf) }
|
|
}
|
|
var cc []discCandidate
|
|
for pattern := range strings.SplitSeq(strings.ToLower(priority), ",") {
|
|
pattern = strings.TrimSpace(pattern)
|
|
if pattern == "" {
|
|
continue
|
|
}
|
|
c := discCandidate{pattern: pattern}
|
|
switch {
|
|
case pattern == "embedded":
|
|
c.resolve = func() (resolution, bool) {
|
|
return resolveEmbedded(ctx, d.lib, ffmpeg, d.firstTrackRel)
|
|
}
|
|
case pattern == externalCandidate:
|
|
c.skip = "external sources are not supported for disc artwork"
|
|
case pattern == "discsubtitle":
|
|
subtitle := strings.TrimSpace(d.album.Discs[d.discNumber])
|
|
if subtitle == "" {
|
|
c.skip = "disc has no subtitle"
|
|
} else {
|
|
c.resolve = folder(d.fromDiscSubtitle(ctx, subtitle))
|
|
}
|
|
case len(d.imgFiles) == 0:
|
|
c.skip = "no images in album folder"
|
|
default:
|
|
c.resolve = folder(d.fromExternalFile(ctx, pattern))
|
|
}
|
|
cc = append(cc, c)
|
|
}
|
|
return cc
|
|
}
|
|
|
|
// selectImage walks the DiscArtPriority entries and returns the first that yields an image.
|
|
// chain records the walk; the serving path passes an untraced one and pays nothing for it.
|
|
func (d *discArtworkReader) selectImage(ctx context.Context, ffmpeg ffmpeg.FFmpeg, priority string,
|
|
chain *chainState) (resolution, error) {
|
|
for _, c := range d.discCandidates(ctx, ffmpeg, priority) {
|
|
if err := ctx.Err(); err != nil {
|
|
return resolution{}, err
|
|
}
|
|
if c.skip != "" {
|
|
chain.record(c.pattern, OutcomeSkipped, c.skip)
|
|
continue
|
|
}
|
|
start := time.Now()
|
|
res, ok := c.resolve()
|
|
log.Trace(ctx, "Artwork: Tried a disc artwork candidate", "albumID", d.album.ID,
|
|
"disc", d.discNumber, "pattern", c.pattern, "hit", ok, "path", res.sourcePath,
|
|
"elapsed", time.Since(start))
|
|
if res, ok = chain.try(c.pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
}
|
|
return chain.exhausted(), nil
|
|
}
|
|
|
|
// fromDiscSubtitle returns a sourceFunc that matches image files whose stem
|
|
// (filename without extension) equals the disc subtitle (case-insensitive).
|
|
func (d *discArtworkReader) fromDiscSubtitle(ctx context.Context, subtitle string) sourceFunc {
|
|
return func() (io.ReadCloser, string, error) {
|
|
for _, file := range d.imgFiles {
|
|
stem := utils.BaseName(file)
|
|
if !strings.EqualFold(stem, subtitle) {
|
|
continue
|
|
}
|
|
f, err := d.lib.FS.Open(file)
|
|
if err != nil {
|
|
log.Warn(ctx, "Artwork: Could not open disc art file", "file", file, err)
|
|
continue
|
|
}
|
|
return f, file, nil
|
|
}
|
|
return nil, "", fmt.Errorf("disc %d: no image file matching subtitle %q", d.discNumber, subtitle)
|
|
}
|
|
}
|
|
|
|
// filepath.Match's '\' escape is excluded on purpose: treating it as a metachar
|
|
// would misalign the literal-prefix extraction in extractDiscNumber.
|
|
const globMetaChars = "*?["
|
|
|
|
// extractDiscNumber parses the disc number from a filename matched by a filepath.Match-style
|
|
// glob. Caller must lowercase both args and have already verified the match.
|
|
func extractDiscNumber(pattern, filename string) (int, bool) {
|
|
metaIdx := strings.IndexAny(pattern, globMetaChars)
|
|
if metaIdx < 0 {
|
|
return 0, false
|
|
}
|
|
prefix := pattern[:metaIdx]
|
|
if !strings.HasPrefix(filename, prefix) {
|
|
return 0, false
|
|
}
|
|
|
|
start := len(prefix)
|
|
end := start
|
|
for end < len(filename) && filename[end] >= '0' && filename[end] <= '9' {
|
|
end++
|
|
}
|
|
if end == start {
|
|
return 0, false
|
|
}
|
|
num, err := strconv.Atoi(filename[start:end])
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
return num, true
|
|
}
|
|
|
|
// fromExternalFile matches image files against a (lowercase) glob pattern. A numbered
|
|
// filename whose number equals the target disc wins over any unnumbered candidate.
|
|
func (d *discArtworkReader) fromExternalFile(ctx context.Context, pattern string) sourceFunc {
|
|
isLiteral := !strings.ContainsAny(pattern, globMetaChars)
|
|
return func() (io.ReadCloser, string, error) {
|
|
var fallbacks []string
|
|
for _, file := range d.imgFiles {
|
|
name := strings.ToLower(path.Base(file))
|
|
match, err := filepath.Match(pattern, name)
|
|
if err != nil {
|
|
log.Warn(ctx, "Artwork: Error matching disc art file to pattern", "pattern", pattern, "file", file)
|
|
continue
|
|
}
|
|
if !match {
|
|
continue
|
|
}
|
|
|
|
if !isLiteral {
|
|
if num, hasNum := extractDiscNumber(pattern, name); hasNum {
|
|
if num != d.discNumber {
|
|
continue
|
|
}
|
|
f, err := d.lib.FS.Open(file)
|
|
if err != nil {
|
|
log.Warn(ctx, "Artwork: Could not open disc art file", "file", file, err)
|
|
continue
|
|
}
|
|
return f, file, nil
|
|
}
|
|
}
|
|
|
|
if d.isMultiFolder && !d.discFoldersRel[path.Dir(file)] {
|
|
continue
|
|
}
|
|
fallbacks = append(fallbacks, file)
|
|
}
|
|
|
|
for _, file := range fallbacks {
|
|
f, err := d.lib.FS.Open(file)
|
|
if err != nil {
|
|
log.Warn(ctx, "Artwork: Could not open disc art file", "file", file, err)
|
|
continue
|
|
}
|
|
return f, file, nil
|
|
}
|
|
return nil, "", fmt.Errorf("disc %d: pattern '%s' not matched by files", d.discNumber, pattern)
|
|
}
|
|
}
|