mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* fix(artwork): re-resolve artwork when image files change on disk An image-only folder change (replaced, added, or deleted cover/artist images, with no audio files touched) was detected by the scanner but never reached the artwork queue, so clients kept seeing the old coverArt hash until something else forced a re-resolution. Phase 1 now diffs each changed folder's image list and imagesUpdatedAt against the previously persisted folder row, and at the end of the phase bulk-enqueues re-resolution for the affected entities: albums with tracks in the folder or its direct children (covering disc subfolder layouts), and, when an artist-pattern image is involved, artists with albums under the folder's subtree, mirroring the artist resolver's upward search. The artist mapping mirrors the resolver's sole-album-artist album selection. New repository helpers keep the mapping set-based and light: folder GetAllIDs, media_file GetAlbumIDsByFolder (distinct, indexed by folder_id), and album GetSoleAlbumArtistIDs. * refactor: simplify the image-change artwork enqueue after review Load the previous folder image state through the existing GetFolderUpdateInfo bulk pre-pass instead of a per-folder SELECT inside the persist transaction, and skip the diff for new folders, whose artwork the scanner already enqueues inline. Move the artist-image classification into core/artwork (IsArtistImageFile) so the scanner shares the resolver's ArtistArtPriority token grammar instead of re-parsing it (the copy mistreated image-folder as a filename glob). Move the folder-subtree query into the folder repository (GetSubtreeIDs) with LIKE escaping and expression-tree batching, share the sole-album-artist predicate between the resolver and the album repository (model.SoleAlbumArtistFilter), extract a chunked single-column query helper, and deduplicate the ArtworkQueueItem literals behind scanArtworkItem. * fix(persistence): keep slash-form paths in GetSubtreeIDs subtree predicates The scanner hands GetSubtreeIDs io/fs slash-form paths, but filepath.Clean rewrites them with backslashes on Windows while folder.path is stored with forward slashes, so the descendant predicates matched nothing and nested artist folders were never re-enqueued there. Normalize with path.Clean, like HasAudioOutsideFolders does, and cover a nested path in the repo test. * refactor(persistence): move the sole-album-artist rule into the album repository SQLizer filters belong in the persistence package, not model. The rule becomes an unexported filter shared by GetSoleAlbumArtistIDs and a new GetBySoleAlbumArtist repository method, which the artist artwork resolver now calls instead of building the squirrel filter itself. * perf(scanner): resolve image-change artists in one query over album.folder_ids The artist half of the image-change enqueue walked folder subtree IDs, then media_file rows, then album rows, marshalling thousands of bound IDs through the driver on each hop. Matching albums by their own folder_ids instead is one statement, and folder_ids is the same source the artist resolver uses to compute an artist's folders. Benchmarked against a copy of the production DB (97k tracks, 10k folders, 7k albums): 87ms +/-196% -> 17.4ms +/-8%, 7.1MB -> 172KB, 103k -> 1.5k allocs. The subtree predicate becomes a shared folderSubtreeFilter, so Folder GetSubtreeIDs and Album GetSoleAlbumArtistIDs are no longer needed. * fix(scanner): persist ancestor folders discovered by a quick scan A quick scan skipped any new folder with no files of its own, so an artist folder holding only album subfolders never got a row. Adding artist.jpg to it later then produced no artwork enqueue: the entry was new, so the image diff was skipped, and it has no tracks, so nothing was enqueued inline either. Skip only genuinely empty new folders, matching what a full scan already persists. This also fixes artist artwork resolving as absent for artists first imported by a quick scan, since the resolver's folder climb needs that row. Also normalizes the selective-scan preload paths with path.Clean, so its descendant predicates match the stored slash-form paths on Windows. * fix(persistence): chunk subtree paths and match artist globs by basename Two regressions from earlier commits on this branch. Collapsing the subtree query into a single statement dropped the chunking the old GetSubtreeIDs had: each path expands into 3 OR terms and SQLite rejects an expression tree deeper than 1000, measured at 166 paths. A library with more artist-image folders than that (the prod copy has 158) would fail the whole collect, dropping the album items with it, so the scanner now keeps them when the artist query fails. The artist-image classifier compared whole tokens after stripping album/, so a directory-bearing glob like images/artist.* never matched the basenames the scanner has. Match on path.Base, which is what album/artist.* already reduced to; the resolver climbs parent folders, so an exact prefix is not knowable here and a conservative match is the right failure direction. * refactor(persistence): halve the repository surface this PR adds Research on the four new repository methods found two were avoidable. GetAlbumIDsByFolder now expands the changed folders to their direct children in its own subquery, so Folder.GetAllIDs has no callers and is deleted, one round trip per scan disappears, and the previously unchunked id/parent_id IN lists are covered by the existing chunking. GetBySoleAlbumArtist becomes an exported SoleAlbumArtistFilter, matching the ParticipantIDFilter precedent for sharing a Sqlizer with core/, so the rule still lives in persistence but AlbumRepository gains nothing and the mock shim that ignored the artist filter is gone. Also drops queryAllSliceChunked, now callerless, in favour of the file-local slices.Chunk convention used by the sibling folder queries. Rejected on measurement: matching the album path by album.folder_ids is exactly equivalent (13975 pairs, zero difference) but has no index, so it scans every album and runs 5-200x slower than the media_file route. * refactor(persistence): stop reading the deprecated album_artist_id column Both artist lookups this PR touches now go through participation, matching the precedent in core/archiver.go and share_repository.go. SoleAlbumArtistFilter uses ParticipantIDFilter, which is also faster: the album_artists unique constraint is a covering index for it, while the old column needed album_artist_album_id plus a row fetch. GetSoleAlbumArtistIDsInSubtrees reads the sole artist out of the participants JSON it already parses for the sole-artist check, rather than joining back to album_artists, which measured ~1.6x slower on a prod-sized copy. Verified equivalent on that copy: 6828 sole-artist albums and 1088 subtree artists resolve identically via the column, the join and the JSON. The tests now set a deliberately wrong album_artist_id so they fail if either query starts reading it again. * docs: trim comments that carry rationale belonging in commit messages Five comments had grown past the budget with benchmark numbers, rejected alternatives, and a duplicate of the constant's own explanation. * refactor(scanner): move the image-change enqueue into phase_1_folders The three functions were methods on phaseFolders, so they belong with the type; phase_1_image_changes.go also read like a fifth phase, which it wasn't. * refactor(scanner): extract the image-change collector into its own type phaseFolders no longer owns the per-library map and the mapping methods; it records into a collector and asks it to enqueue once. The collector keeps the library alongside the folders, so enqueue needs only ctx and the datastore. * refactor(scanner): simplify enqueue method by removing redundant datastore parameter Signed-off-by: Deluan <deluan@navidrome.org> * docs(scanner): drop the stale zero-value claim on imageChangeCollector The collector now takes its datastore at construction, so the zero value is no longer usable. * fix(scanner): pin the persist stage to concurrency 1 and guard the collector The stage relied on go-pipeline defaulting to one worker; stating it at the stage makes the constraint visible where someone would change it. The collector takes a mutex too, so the type is safe on its own terms rather than by configuration. --------- Signed-off-by: Deluan <deluan@navidrome.org>
588 lines
19 KiB
Go
588 lines
19 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
"io"
|
|
"io/fs"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/core/agents"
|
|
"github.com/navidrome/navidrome/core/ffmpeg"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
)
|
|
|
|
// resolution is one attempted acquisition outcome for an entity.
|
|
type resolution struct {
|
|
reader io.ReadCloser // nil when no source yielded an image
|
|
source string // model.ItemArtwork.Source value: "folder", "embedded", "external", "upload", "generated"
|
|
sourcePath string // backing library/upload file (folder/upload: the image; embedded: the audio file); "" otherwise
|
|
refMtime int64 // sourcePath mtime (unix-nanoseconds) at resolution; 0 when no sourcePath
|
|
// external source errored/timed out. With no reader it forces failed (never absent);
|
|
// on a hit a higher-priority external step failed—serve this, but retry later.
|
|
extError bool
|
|
// a local source that should have been readable wasn't. With no reader it forces failed,
|
|
// so a transient I/O fault never records absent.
|
|
localError bool
|
|
}
|
|
|
|
// chainState carries what a priority walk has seen so far. A hit takes extErr with it so a
|
|
// transient external failure still retries; localErr is dropped, as the scanner re-lists changes.
|
|
type chainState struct {
|
|
extErr, localErr bool
|
|
trace *ChainTrace // nil unless the CLI asked for a trace
|
|
}
|
|
|
|
// try stamps the accumulated external failure onto a hit, and records the miss otherwise.
|
|
func (c *chainState) try(candidate string, res resolution, ok bool) (resolution, bool) {
|
|
if ok {
|
|
res.extError = c.extErr
|
|
c.record(candidate, OutcomeHit, res.sourcePath)
|
|
return res, true
|
|
}
|
|
c.localErr = c.localErr || res.localError
|
|
if res.localError {
|
|
c.record(candidate, OutcomeUnreadable, "")
|
|
} else {
|
|
c.record(candidate, OutcomeMiss, "")
|
|
}
|
|
return resolution{}, false
|
|
}
|
|
|
|
func (c *chainState) record(candidate string, out Outcome, detail string) {
|
|
c.trace.add(TraceStep{Candidate: candidate, Outcome: out, Detail: detail})
|
|
}
|
|
|
|
// exhausted is the outcome when no source in the chain yielded an image.
|
|
func (c *chainState) exhausted() resolution {
|
|
return resolution{extError: c.extErr, localError: c.localErr}
|
|
}
|
|
|
|
// externalSource holds the agents to ask and the rate limiter/circuit breaker to ask them through.
|
|
type externalSource struct {
|
|
agents *agents.Agents
|
|
gate gateFunc
|
|
}
|
|
|
|
// resolver walks a kind's priority chain and returns the first hit; a nil ext means local-only.
|
|
type resolver struct {
|
|
ds model.DataStore
|
|
ffmpeg ffmpeg.FFmpeg
|
|
ext *externalSource
|
|
}
|
|
|
|
func newResolver(ds model.DataStore, ag *agents.Agents, ffm ffmpeg.FFmpeg, gate gateFunc) *resolver {
|
|
if gate == nil {
|
|
gate = passthroughGate
|
|
}
|
|
return &resolver{ds: ds, ffmpeg: ffm, ext: &externalSource{agents: ag, gate: gate}}
|
|
}
|
|
|
|
// newLocalResolver builds a resolver that can neither reach the network nor sample album art
|
|
// for the worker-built grid.
|
|
func newLocalResolver(ds model.DataStore, ffm ffmpeg.FFmpeg) *resolver {
|
|
return &resolver{ds: ds, ffmpeg: ffm}
|
|
}
|
|
|
|
func (r *resolver) resolve(ctx context.Context, item model.ArtworkQueueItem) (resolution, error) {
|
|
kind, _ := model.ParseKind(item.ItemKind)
|
|
switch kind {
|
|
case model.KindAlbumArtwork:
|
|
return r.resolveAlbum(ctx, item.ItemID)
|
|
case model.KindArtistArtwork:
|
|
return r.resolveArtist(ctx, item.ItemID)
|
|
case model.KindPlaylistArtwork:
|
|
return r.resolvePlaylist(ctx, item.ItemID)
|
|
case model.KindRadioArtwork:
|
|
return r.resolveRadio(ctx, item.ItemID)
|
|
case model.KindMediaFileArtwork:
|
|
return r.resolveMediaFile(ctx, item.ItemID)
|
|
default:
|
|
return resolution{}, fmt.Errorf("artwork: kind %q is not resolvable by the worker", item.ItemKind)
|
|
}
|
|
}
|
|
|
|
// Explainable reports whether TracingResolver can walk this kind's sources and report which one
|
|
// won; playlists and radios resolve from a fixed internal order, with nothing configured to explain.
|
|
func Explainable(kind model.Kind) bool {
|
|
switch kind {
|
|
case model.KindArtistArtwork, model.KindAlbumArtwork, model.KindDiscArtwork, model.KindMediaFileArtwork:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// MayFetchExternal reports whether resolving this kind can issue an external request under the
|
|
// current config. Playlists inherit the album chain: the generated grid resolves album art.
|
|
func MayFetchExternal(kind model.Kind) bool {
|
|
switch kind {
|
|
case model.KindArtistArtwork:
|
|
return chainFetchesExternal(conf.Server.ArtistArtPriority)
|
|
case model.KindAlbumArtwork:
|
|
return chainFetchesExternal(conf.Server.CoverArtPriority)
|
|
case model.KindPlaylistArtwork:
|
|
return conf.Server.EnableM3UExternalAlbumArt || chainFetchesExternal(conf.Server.CoverArtPriority)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// ImageAgentCount is how many enabled agents provide artist and album images.
|
|
type ImageAgentCount struct{ Artist, Album int }
|
|
|
|
// ExternalLookupsPerItem reports what resolving one item of this kind can cost: every image agent is
|
|
// tried, and a zero count still bills one, so agents the caller cannot see never read as free.
|
|
func ExternalLookupsPerItem(kind model.Kind, agents ImageAgentCount) int64 {
|
|
if !MayFetchExternal(kind) {
|
|
return 0
|
|
}
|
|
switch kind {
|
|
case model.KindArtistArtwork:
|
|
return int64(max(agents.Artist, 1))
|
|
case model.KindAlbumArtwork:
|
|
return int64(max(agents.Album, 1))
|
|
case model.KindPlaylistArtwork:
|
|
var n int64
|
|
if conf.Server.EnableM3UExternalAlbumArt {
|
|
n++
|
|
}
|
|
if chainFetchesExternal(conf.Server.CoverArtPriority) {
|
|
n += PlaylistGridSamples * int64(max(agents.Album, 1))
|
|
}
|
|
return n
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func chainFetchesExternal(priority string) bool {
|
|
for pattern := range strings.SplitSeq(strings.ToLower(priority), ",") {
|
|
if strings.TrimSpace(pattern) == externalCandidate {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Album and artist fetches stop here when the resolver is local-only, rather than at each point in
|
|
// the chain walk; resolvePlaylist gates the third network path, the m3u image URL, itself.
|
|
func (r *resolver) fetchExternalAlbum(ctx context.Context, al model.Album) (io.ReadCloser, string, bool) {
|
|
if r.ext == nil {
|
|
return nil, "", false
|
|
}
|
|
return fetchAlbumImage(ctx, r.ext.agents, r.ext.gate, al)
|
|
}
|
|
|
|
func (r *resolver) fetchExternalArtist(ctx context.Context, ar model.Artist) (io.ReadCloser, string, bool) {
|
|
if r.ext == nil {
|
|
return nil, "", false
|
|
}
|
|
return fetchArtistImage(ctx, r.ext.agents, r.ext.gate, ar)
|
|
}
|
|
|
|
// resolveAlbum walks conf.Server.CoverArtPriority over the folder, embedded and external sources.
|
|
func (r *resolver) resolveAlbum(ctx context.Context, albumID string) (resolution, error) {
|
|
al, err := r.ds.Album(ctx).Get(albumID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
_, imgFiles, _, err := loadAlbumFoldersPaths(ctx, r.ds, *al)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
lib, err := loadLibraryView(ctx, r.ds, al.LibraryID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
|
|
chain := chainState{trace: traceFrom(ctx)}
|
|
for pattern := range strings.SplitSeq(strings.ToLower(conf.Server.CoverArtPriority), ",") {
|
|
pattern = strings.TrimSpace(pattern)
|
|
if pattern == "" {
|
|
continue
|
|
}
|
|
switch {
|
|
case pattern == "embedded":
|
|
res, ok := resolveEmbedded(ctx, lib, r.ffmpeg, al.EmbedArtPath)
|
|
if res, ok = chain.try(pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
case pattern == externalCandidate:
|
|
if rd, name, isErr := r.fetchExternalAlbum(ctx, *al); rd != nil {
|
|
return resolution{reader: rd, source: ExternalPrefix + name}, nil
|
|
} else if isErr {
|
|
chain.extErr = true
|
|
}
|
|
case len(imgFiles) > 0:
|
|
res, ok := resolveFolderFile(ctx, lib, imgFiles, pattern)
|
|
if res, ok = chain.try(pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
default:
|
|
chain.record(pattern, OutcomeSkipped, "no images in album folder")
|
|
}
|
|
}
|
|
return chain.exhausted(), nil
|
|
}
|
|
|
|
// resolveArtist tries the uploaded image first, then walks conf.Server.ArtistArtPriority.
|
|
func (r *resolver) resolveArtist(ctx context.Context, artistID string) (resolution, error) {
|
|
ar, err := r.ds.Artist(ctx).Get(artistID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
chain := chainState{trace: traceFrom(ctx)}
|
|
upload, uploadOK := resolveLocalFile(ar.UploadedImagePath(), "upload")
|
|
if res, ok := chain.try("upload", upload, uploadOK); ok {
|
|
return res, nil
|
|
}
|
|
if upload.localError {
|
|
// The upload outranks every other source; falling through would persist a lower-priority
|
|
// image as if the upload were gone.
|
|
return upload, nil
|
|
}
|
|
|
|
// Only consider albums where the artist is the sole album artist.
|
|
als, err := r.ds.Album(ctx).GetAll(model.QueryOptions{Filters: persistence.SoleAlbumArtistFilter(artistID)})
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
albumPaths, imgFiles, _, err := loadArtistAlbumRoots(ctx, r.ds, als)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
artistFolder, _, err := loadArtistFolder(ctx, r.ds, als, albumPaths)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
var lib libraryView
|
|
if len(als) > 0 {
|
|
lib, err = loadLibraryView(ctx, r.ds, als[0].LibraryID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
}
|
|
|
|
for pattern := range strings.SplitSeq(strings.ToLower(conf.Server.ArtistArtPriority), ",") {
|
|
pattern = strings.TrimSpace(pattern)
|
|
if pattern == "" {
|
|
continue
|
|
}
|
|
switch {
|
|
case pattern == externalCandidate:
|
|
if rd, name, isErr := r.fetchExternalArtist(ctx, *ar); rd != nil {
|
|
return resolution{reader: rd, source: ExternalPrefix + name}, nil
|
|
} else if isErr {
|
|
chain.extErr = true
|
|
}
|
|
case pattern == "image-folder":
|
|
res, ok := resolveArtistImageFolder(ar)
|
|
if res, ok = chain.try(pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
case strings.HasPrefix(pattern, "album/"):
|
|
if lib.FS == nil {
|
|
chain.record(pattern, OutcomeSkipped, "artist has no albums")
|
|
continue
|
|
}
|
|
res, ok := resolveFolderFile(ctx, lib, imgFiles, strings.TrimPrefix(pattern, "album/"))
|
|
if res, ok = chain.try(pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
default:
|
|
if lib.FS == nil {
|
|
chain.record(pattern, OutcomeSkipped, "artist has no albums")
|
|
continue
|
|
}
|
|
if artistFolder == "" {
|
|
chain.record(pattern, OutcomeSkipped, "no artist folder")
|
|
continue
|
|
}
|
|
res, ok := resolveArtistFolderPattern(ctx, lib, artistFolder, pattern)
|
|
if res, ok = chain.try(pattern, res, ok); ok {
|
|
return res, nil
|
|
}
|
|
}
|
|
}
|
|
return chain.exhausted(), nil
|
|
}
|
|
|
|
// PlaylistGridSamples is how many albums resolvePlaylist samples to build the generated grid.
|
|
const PlaylistGridSamples = 4
|
|
|
|
// resolvePlaylist tries the uploaded image, the sidecar and ExternalImageURL, then a generated grid.
|
|
func (r *resolver) resolvePlaylist(ctx context.Context, playlistID string) (resolution, error) {
|
|
pl, err := r.ds.Playlist(ctx).Get(playlistID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
|
|
var extErr bool
|
|
for _, src := range []struct{ path, source string }{
|
|
{pl.UploadedImagePath(), "upload"},
|
|
{findPlaylistSidecarPath(ctx, pl.Path), "folder"},
|
|
} {
|
|
res, ok := resolveLocalFile(src.path, src.source)
|
|
if ok {
|
|
return res, nil
|
|
}
|
|
if res.localError {
|
|
// These outrank the generated grid; falling through would replace them with it.
|
|
return res, nil
|
|
}
|
|
}
|
|
// A local ExternalImageURL is file-backed and served in place; only http(s) needs the gated fetch.
|
|
localImg, remoteImg := classifyPlaylistImage(pl.ExternalImageURL)
|
|
if localImg != "" {
|
|
res, ok := resolveLocalFile(localImg, "folder")
|
|
if ok {
|
|
return res, nil
|
|
}
|
|
if res.localError {
|
|
return res, nil
|
|
}
|
|
}
|
|
if r.ext == nil {
|
|
// The remote fetch and the generated grid are worker-only; a request must do neither.
|
|
return resolution{}, nil
|
|
}
|
|
if remoteImg != nil && conf.Server.EnableM3UExternalAlbumArt {
|
|
sf := func() (io.ReadCloser, string, error) { return fromURL(ctx, remoteImg) }
|
|
if res, ok, isErr := resolveExternalStep(r.ext.gate, "m3u", sf); ok {
|
|
return res, nil
|
|
} else if isErr {
|
|
extErr = true
|
|
}
|
|
}
|
|
|
|
albumIDs, err := r.ds.Playlist(ctx).Tracks(pl.ID, false).
|
|
GetAlbumIDs(model.QueryOptions{Max: PlaylistGridSamples, Sort: "random()"})
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
|
|
var tiles []image.Image
|
|
var tileErr error // first internal (non-external) tile failure, e.g. album deleted mid-flight
|
|
for _, albumID := range albumIDs {
|
|
res, err := r.resolveAlbum(ctx, albumID)
|
|
if err != nil {
|
|
if tileErr == nil {
|
|
tileErr = err
|
|
}
|
|
continue
|
|
}
|
|
if res.extError {
|
|
extErr = true
|
|
}
|
|
if res.reader == nil {
|
|
continue
|
|
}
|
|
tile, decErr := decodeTile(res.reader)
|
|
res.reader.Close()
|
|
if decErr == nil {
|
|
tiles = append(tiles, tile)
|
|
}
|
|
if len(tiles) == PlaylistGridSamples {
|
|
break
|
|
}
|
|
}
|
|
if len(tiles) == 0 {
|
|
// A tile-level failure must never resolve as a clean absent.
|
|
if tileErr != nil {
|
|
return resolution{}, fmt.Errorf("resolvePlaylist: sampled album art failed: %w", tileErr)
|
|
}
|
|
return resolution{extError: extErr}, nil
|
|
}
|
|
// Grow to 4 tiles by repeating what we have.
|
|
switch len(tiles) {
|
|
case 2:
|
|
tiles = append(tiles, tiles[1], tiles[0])
|
|
case 3:
|
|
tiles = append(tiles, tiles[0])
|
|
}
|
|
grid, err := assembleTiles(tiles)
|
|
if err != nil {
|
|
return resolution{extError: extErr}, nil //nolint:nilerr // encode failure is a soft "no image", not a resolution error
|
|
}
|
|
return resolution{reader: grid, source: "generated", extError: extErr}, nil
|
|
}
|
|
|
|
// resolveRadio serves only an uploaded image; there is no fallback.
|
|
func (r *resolver) resolveRadio(ctx context.Context, radioID string) (resolution, error) {
|
|
radio, err := r.ds.Radio(ctx).Get(radioID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
res, _ := resolveLocalFile(radio.UploadedImagePath(), "upload")
|
|
return res, nil
|
|
}
|
|
|
|
// resolveMediaFile resolves a track's own embedded art only, so disabled or missing cover art
|
|
// is a definitive absent.
|
|
func (r *resolver) resolveMediaFile(ctx context.Context, id string) (resolution, error) {
|
|
mf, err := r.ds.MediaFile(ctx).Get(id)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
chain := chainState{trace: traceFrom(ctx)}
|
|
switch {
|
|
case !conf.Server.EnableMediaFileCoverArt:
|
|
chain.record("embedded", OutcomeSkipped, "EnableMediaFileCoverArt is off")
|
|
return resolution{}, nil
|
|
case !mf.HasCoverArt:
|
|
chain.record("embedded", OutcomeMiss, "the track has no embedded cover art")
|
|
return resolution{}, nil
|
|
}
|
|
lib, err := loadLibraryView(ctx, r.ds, mf.LibraryID)
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
res, ok := resolveEmbedded(ctx, lib, r.ffmpeg, mf.Path)
|
|
if res, ok = chain.try("embedded", res, ok); ok {
|
|
return res, nil
|
|
}
|
|
return chain.exhausted(), nil
|
|
}
|
|
|
|
// resolveDisc walks conf.Server.DiscArtPriority. Disc artwork keeps no state row and is never
|
|
// queued: the serving path reads it through on every request, so this only ever explains.
|
|
func (r *resolver) resolveDisc(ctx context.Context, id string) (resolution, error) {
|
|
dr, err := newDiscArtworkReader(ctx, r.ds, model.ArtworkID{Kind: model.KindDiscArtwork, ID: id})
|
|
if err != nil {
|
|
return resolution{}, err
|
|
}
|
|
chain := chainState{trace: traceFrom(ctx)}
|
|
return dr.selectImage(ctx, r.ffmpeg, conf.Server.DiscArtPriority, &chain)
|
|
}
|
|
|
|
// resolveExternalStep runs a single external sourceFunc through the named gate. extErr excludes
|
|
// a not-found, which is a definitive "no" rather than a failure.
|
|
func resolveExternalStep(gate gateFunc, name string, sf sourceFunc) (res resolution, ok bool, extErr bool) {
|
|
r, path, err := gate(name, sf)
|
|
if r != nil {
|
|
return resolution{reader: r, source: externalCandidate, sourcePath: path}, true, false
|
|
}
|
|
return resolution{}, false, err != nil && !errors.Is(err, model.ErrNotFound)
|
|
}
|
|
|
|
// classifyPlaylistImage splits a playlist ExternalImageURL into a local filesystem path or a
|
|
// remote http(s) URL; at most one is set.
|
|
func classifyPlaylistImage(imageURL string) (localPath string, remote *url.URL) {
|
|
if imageURL == "" {
|
|
return "", nil
|
|
}
|
|
u, err := url.Parse(imageURL)
|
|
if err != nil {
|
|
return imageURL, nil // unparseable → treat as a local path
|
|
}
|
|
switch u.Scheme {
|
|
case "http", "https":
|
|
return "", u
|
|
case "file":
|
|
return u.Path, nil
|
|
default:
|
|
return imageURL, nil
|
|
}
|
|
}
|
|
|
|
func resolveEmbedded(ctx context.Context, lib libraryView, ffm ffmpeg.FFmpeg, embedRel string) (resolution, bool) {
|
|
if embedRel == "" {
|
|
return resolution{}, false
|
|
}
|
|
abs := lib.Abs(embedRel)
|
|
var unreadable bool
|
|
for _, sf := range []sourceFunc{fromTag(ctx, lib.FS, embedRel), fromFFmpegTag(ctx, ffm, abs)} {
|
|
r, _, err := sf()
|
|
if r != nil {
|
|
return resolution{reader: r, source: "embedded", sourcePath: abs, refMtime: mtimeViaFS(lib.FS, embedRel)}, true
|
|
}
|
|
unreadable = unreadable || errors.Is(err, errSourceUnreadable)
|
|
}
|
|
return resolution{localError: unreadable}, false
|
|
}
|
|
|
|
// resolveFolderSource turns a source that yields a library-relative image path into a folder
|
|
// resolution, keeping an existing-but-unopenable file distinct from an absent one.
|
|
func resolveFolderSource(lib libraryView, sf sourceFunc) (resolution, bool) {
|
|
r, path, err := sf()
|
|
if r == nil {
|
|
return resolution{localError: errors.Is(err, errSourceUnreadable)}, false
|
|
}
|
|
return resolution{reader: r, source: "folder", sourcePath: lib.Abs(path), refMtime: mtimeViaFS(lib.FS, path)}, true
|
|
}
|
|
|
|
func resolveFolderFile(ctx context.Context, lib libraryView, imgFiles []string, pattern string) (resolution, bool) {
|
|
return resolveFolderSource(lib, fromExternalFile(ctx, lib.FS, imgFiles, pattern))
|
|
}
|
|
|
|
// IsArtistImageFile reports whether a file name matches any file-glob token of ArtistArtPriority.
|
|
// Basename-only on purpose: the chain climbs parent folders, so a token's prefix is not fixed.
|
|
func IsArtistImageFile(name string) bool {
|
|
name = strings.ToLower(name)
|
|
for pattern := range strings.SplitSeq(strings.ToLower(conf.Server.ArtistArtPriority), ",") {
|
|
pattern = strings.TrimSpace(pattern)
|
|
if pattern == "" || pattern == externalCandidate || pattern == "image-folder" {
|
|
continue
|
|
}
|
|
if ok, _ := path.Match(path.Base(pattern), name); ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func resolveArtistImageFolder(ar *model.Artist) (resolution, bool) {
|
|
folder := conf.Server.ArtistImageFolder
|
|
if folder == "" {
|
|
return resolution{}, false
|
|
}
|
|
return resolveLocalFile(findImageInArtistFolder(folder, ar.MbzArtistID, ar.Name), "folder")
|
|
}
|
|
|
|
func resolveArtistFolderPattern(ctx context.Context, lib libraryView, artistFolder, pattern string) (resolution, bool) {
|
|
r, path, err := fromArtistFolder(ctx, lib.FS, lib.absRoot, artistFolder, pattern)()
|
|
if r == nil {
|
|
return resolution{localError: errors.Is(err, errSourceUnreadable)}, false
|
|
}
|
|
return resolution{reader: r, source: "folder", sourcePath: path, refMtime: mtimeOf(path)}, true
|
|
}
|
|
|
|
// resolveLocalFile opens an absolute path directly. A missing path is "no source"; any other
|
|
// open failure says nothing about whether the image exists.
|
|
func resolveLocalFile(path, source string) (resolution, bool) {
|
|
if path == "" {
|
|
return resolution{}, false
|
|
}
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return resolution{localError: !errors.Is(err, fs.ErrNotExist)}, false
|
|
}
|
|
return resolution{reader: f, source: source, sourcePath: path, refMtime: mtimeOf(path)}, true
|
|
}
|
|
|
|
func mtimeOf(path string) int64 {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return info.ModTime().UnixNano()
|
|
}
|
|
|
|
// mtimeViaFS stats through the library FS, since library roots in tests may not be real OS paths.
|
|
func mtimeViaFS(fsys fs.FS, name string) int64 {
|
|
if fsys == nil || name == "" {
|
|
return 0
|
|
}
|
|
info, err := fs.Stat(fsys, name)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return info.ModTime().UnixNano()
|
|
}
|