mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-18 16:18:52 +01:00
The blurhash worker previously recomputed on every artwork serve and reconciled a row-level freshness oracle against serve-time hints (force, sourceGone, imageUpdatedAt) to decide whether the served bytes had actually changed. Every staleness bug found in review was one case where that weak oracle disagreed with the true one, and each fix imported one more serve-time fragment into it. Move the trigger to the image-cache fill instead: an original-size cache miss is exactly when the served bytes change, so the reader's LastUpdated snapshot is the truth and no reconciliation is needed. Resized fills recurse through Get(size=0) and hash the original once; a disabled cache reports every original serve as a fill, keeping real hashes available (the worker's unchanged-hash guard keeps that write-free). This deletes the force/sourceGone/imageUpdatedAt flags, the double-freshness comparison, and the entire negative cache. Only the two irreducible pieces survive: the placeholder byte-compare and the ErrUnavailable clear-hook (EnqueueGone), since deletion-without-rescan is invisible to every passive signal. Adds an e2e test for in-place cover swaps, the scenario that drove the deleted machinery, now covered structurally by the fill trigger. Schema, DTO, repositories and the blurhash encoder package are unchanged.
157 lines
5.1 KiB
Go
157 lines
5.1 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
_ "image/gif"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/core/external"
|
|
"github.com/navidrome/navidrome/core/ffmpeg"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/resources"
|
|
"github.com/navidrome/navidrome/utils/cache"
|
|
_ "golang.org/x/image/webp"
|
|
)
|
|
|
|
var ErrUnavailable = errors.New("artwork unavailable")
|
|
|
|
type Artwork interface {
|
|
Get(ctx context.Context, artID model.ArtworkID, size int, square bool) (io.ReadCloser, time.Time, error)
|
|
GetOrPlaceholder(ctx context.Context, id string, size int, square bool) (io.ReadCloser, time.Time, error)
|
|
}
|
|
|
|
func NewArtwork(ds model.DataStore, cache cache.FileCache, ffmpeg ffmpeg.FFmpeg, provider external.Provider) Artwork {
|
|
a := &artwork{ds: ds, cache: cache, ffmpeg: ffmpeg, provider: provider}
|
|
a.blurHashes = newBlurHashUpdater(a)
|
|
return a
|
|
}
|
|
|
|
// Close stops the background blurhash worker. The server never calls it; tests must, so a leaked
|
|
// worker can't touch mocks/filesystems being torn down by the next spec.
|
|
func (a *artwork) Close() error {
|
|
if a.blurHashes != nil {
|
|
a.blurHashes.stop()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type artwork struct {
|
|
ds model.DataStore
|
|
cache cache.FileCache
|
|
ffmpeg ffmpeg.FFmpeg
|
|
provider external.Provider
|
|
blurHashes *blurHashUpdater
|
|
}
|
|
|
|
type artworkReader interface {
|
|
cache.Item
|
|
LastUpdated() time.Time
|
|
Reader(ctx context.Context) (io.ReadCloser, string, error)
|
|
}
|
|
|
|
func (a *artwork) GetOrPlaceholder(ctx context.Context, id string, size int, square bool) (reader io.ReadCloser, lastUpdate time.Time, err error) {
|
|
artID, err := a.getArtworkId(ctx, id)
|
|
if err == nil {
|
|
reader, lastUpdate, err = a.Get(ctx, artID, size, square)
|
|
}
|
|
if errors.Is(err, ErrUnavailable) {
|
|
if artID.Kind == model.KindArtistArtwork {
|
|
reader, _ = resources.FS().Open(consts.PlaceholderArtistArt)
|
|
} else {
|
|
reader, _ = resources.FS().Open(consts.PlaceholderAlbumArt)
|
|
}
|
|
return reader, consts.ServerStart, nil
|
|
}
|
|
return reader, lastUpdate, err
|
|
}
|
|
|
|
func (a *artwork) Get(ctx context.Context, artID model.ArtworkID, size int, square bool) (reader io.ReadCloser, lastUpdate time.Time, err error) {
|
|
artReader, err := a.getArtworkReader(ctx, artID, size, square)
|
|
if err != nil {
|
|
return nil, time.Time{}, err
|
|
}
|
|
|
|
r, err := a.cache.Get(ctx, artReader)
|
|
if err != nil {
|
|
if !errors.Is(err, context.Canceled) && !errors.Is(err, ErrUnavailable) {
|
|
log.Error(ctx, "Error accessing image cache", "id", artID, "size", size, err)
|
|
}
|
|
// A vanished source must still reach the worker, or a stored hash would keep describing
|
|
// artwork that no longer exists.
|
|
if a.blurHashes != nil && errors.Is(err, ErrUnavailable) {
|
|
a.blurHashes.EnqueueGone(artID)
|
|
}
|
|
return nil, time.Time{}, err
|
|
}
|
|
if a.blurHashes != nil && size == 0 && !square && !r.Cached {
|
|
// An original-size cache fill is exactly when the served bytes change: the single recompute
|
|
// trigger. Resized fills recurse through Get(size=0); a disabled cache reports every serve as
|
|
// a fill (the worker's unchanged-hash guard keeps that write-free).
|
|
a.blurHashes.Enqueue(artID, artReader.LastUpdated())
|
|
}
|
|
return r, artReader.LastUpdated(), nil
|
|
}
|
|
|
|
type coverArtGetter interface {
|
|
CoverArtID() model.ArtworkID
|
|
}
|
|
|
|
func (a *artwork) getArtworkId(ctx context.Context, id string) (model.ArtworkID, error) {
|
|
if id == "" {
|
|
return model.ArtworkID{}, ErrUnavailable
|
|
}
|
|
artID, err := model.ParseArtworkID(id)
|
|
if err == nil {
|
|
return artID, nil
|
|
}
|
|
|
|
log.Trace(ctx, "ArtworkID invalid. Trying to figure out kind based on the ID", "id", id)
|
|
entity, err := model.GetEntityByID(ctx, a.ds, id)
|
|
if err != nil {
|
|
return model.ArtworkID{}, err
|
|
}
|
|
if e, ok := entity.(coverArtGetter); ok {
|
|
artID = e.CoverArtID()
|
|
}
|
|
switch e := entity.(type) {
|
|
case *model.Artist:
|
|
log.Trace(ctx, "ID is for an Artist", "id", id, "name", e.Name, "artist", e.Name)
|
|
case *model.Album:
|
|
log.Trace(ctx, "ID is for an Album", "id", id, "name", e.Name, "artist", e.AlbumArtist)
|
|
case *model.MediaFile:
|
|
log.Trace(ctx, "ID is for a MediaFile", "id", id, "title", e.Title, "album", e.Album)
|
|
case *model.Playlist:
|
|
log.Trace(ctx, "ID is for a Playlist", "id", id, "name", e.Name)
|
|
}
|
|
return artID, nil
|
|
}
|
|
|
|
func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, size int, square bool) (artworkReader, error) {
|
|
var artReader artworkReader
|
|
var err error
|
|
if size > 0 || square {
|
|
artReader, err = resizedFromOriginal(ctx, a, artID, size, square)
|
|
} else {
|
|
switch artID.Kind {
|
|
case model.KindArtistArtwork:
|
|
artReader, err = newArtistArtworkReader(ctx, a, artID, a.provider)
|
|
case model.KindAlbumArtwork:
|
|
artReader, err = newAlbumArtworkReader(ctx, a, artID, a.provider)
|
|
case model.KindMediaFileArtwork:
|
|
artReader, err = newMediafileArtworkReader(ctx, a, artID)
|
|
case model.KindPlaylistArtwork:
|
|
artReader, err = newPlaylistArtworkReader(ctx, a, artID)
|
|
case model.KindDiscArtwork:
|
|
artReader, err = newDiscArtworkReader(ctx, a, artID)
|
|
case model.KindRadioArtwork:
|
|
artReader, err = newRadioArtworkReader(ctx, a, artID)
|
|
default:
|
|
return nil, ErrUnavailable
|
|
}
|
|
}
|
|
return artReader, err
|
|
}
|