2022-12-25 16:07:28 -05:00
|
|
|
package artwork
|
2016-03-08 23:05:54 -05:00
|
|
|
|
|
|
|
|
import (
|
2020-01-22 08:32:31 -05:00
|
|
|
"context"
|
2022-12-19 15:34:21 -05:00
|
|
|
"errors"
|
2016-03-08 23:31:48 -05:00
|
|
|
_ "image/gif"
|
2016-03-18 11:33:50 -04:00
|
|
|
"io"
|
2022-12-25 01:25:10 -05:00
|
|
|
"time"
|
2016-03-08 23:31:48 -05:00
|
|
|
|
2023-01-31 18:22:49 -05:00
|
|
|
"github.com/navidrome/navidrome/consts"
|
2025-04-08 21:11:09 -04:00
|
|
|
"github.com/navidrome/navidrome/core/external"
|
2022-12-20 12:25:47 -05:00
|
|
|
"github.com/navidrome/navidrome/core/ffmpeg"
|
2022-12-19 15:34:21 -05:00
|
|
|
"github.com/navidrome/navidrome/log"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2023-01-31 18:22:49 -05:00
|
|
|
"github.com/navidrome/navidrome/resources"
|
2022-12-20 11:27:40 -05:00
|
|
|
"github.com/navidrome/navidrome/utils/cache"
|
2020-08-21 11:33:23 -04:00
|
|
|
_ "golang.org/x/image/webp"
|
2016-03-08 23:05:54 -05:00
|
|
|
)
|
|
|
|
|
|
2023-01-31 18:22:49 -05:00
|
|
|
var ErrUnavailable = errors.New("artwork unavailable")
|
|
|
|
|
|
2026-07-17 20:34:47 -04:00
|
|
|
// maxTeeBytes bounds the per-serve capture buffer; artwork is a few MB, and anything larger is not
|
|
|
|
|
// hashed (skipped), so a pathological source can't accumulate unbounded memory across serves.
|
|
|
|
|
const maxTeeBytes = 20 * 1024 * 1024
|
|
|
|
|
|
|
|
|
|
// capAtNow keeps a future artwork mtime (clock skew, a future-stamped file) from being stored as the
|
|
|
|
|
// blurhash version, which would let the DTO's !Before check pin the hash until wall time caught up.
|
|
|
|
|
func capAtNow(t time.Time) time.Time {
|
|
|
|
|
if now := time.Now(); t.After(now) {
|
|
|
|
|
return now
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:31:19 -04:00
|
|
|
type Artwork interface {
|
2024-05-24 20:19:26 -04:00
|
|
|
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)
|
2016-03-08 23:05:54 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-08 21:11:09 -04:00
|
|
|
func NewArtwork(ds model.DataStore, cache cache.FileCache, ffmpeg ffmpeg.FFmpeg, provider external.Provider) Artwork {
|
2026-07-17 21:46:12 -04:00
|
|
|
return &artwork{ds: ds, cache: cache, ffmpeg: ffmpeg, provider: provider, blurHashes: newBlurHashUpdater(ds)}
|
2016-03-08 23:05:54 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:31:19 -04:00
|
|
|
type artwork struct {
|
2026-07-16 00:15:12 -04:00
|
|
|
ds model.DataStore
|
|
|
|
|
cache cache.FileCache
|
|
|
|
|
ffmpeg ffmpeg.FFmpeg
|
|
|
|
|
provider external.Provider
|
|
|
|
|
blurHashes *blurHashUpdater
|
2020-07-24 13:30:27 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-27 11:36:23 -05:00
|
|
|
type artworkReader interface {
|
|
|
|
|
cache.Item
|
|
|
|
|
LastUpdated() time.Time
|
|
|
|
|
Reader(ctx context.Context) (io.ReadCloser, string, error)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 20:19:26 -04:00
|
|
|
func (a *artwork) GetOrPlaceholder(ctx context.Context, id string, size int, square bool) (reader io.ReadCloser, lastUpdate time.Time, err error) {
|
2022-12-28 12:32:46 -05:00
|
|
|
artID, err := a.getArtworkId(ctx, id)
|
2023-01-31 18:22:49 -05:00
|
|
|
if err == nil {
|
2024-05-24 20:19:26 -04:00
|
|
|
reader, lastUpdate, err = a.Get(ctx, artID, size, square)
|
2022-12-20 11:27:40 -05:00
|
|
|
}
|
2023-01-31 18:22:49 -05:00
|
|
|
if errors.Is(err, ErrUnavailable) {
|
2026-07-17 21:46:12 -04:00
|
|
|
// The client is receiving the placeholder, so a stored hash describing the old cover must
|
|
|
|
|
// clear — hash-what-you-serve applies to the fallback too.
|
|
|
|
|
a.blurHashes.clearIfStored(ctx, artID)
|
2023-01-31 18:22:49 -05:00
|
|
|
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
|
|
|
|
|
}
|
2022-12-20 11:27:40 -05:00
|
|
|
|
2024-05-24 20:19:26 -04:00
|
|
|
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)
|
2022-12-19 17:07:29 -05:00
|
|
|
if err != nil {
|
2022-12-27 12:54:51 -05:00
|
|
|
return nil, time.Time{}, err
|
2022-12-19 15:34:21 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-27 11:36:23 -05:00
|
|
|
r, err := a.cache.Get(ctx, artReader)
|
2022-12-28 10:19:52 -05:00
|
|
|
if err != nil {
|
2023-02-02 10:36:27 -05:00
|
|
|
if !errors.Is(err, context.Canceled) && !errors.Is(err, ErrUnavailable) {
|
2023-01-31 18:22:49 -05:00
|
|
|
log.Error(ctx, "Error accessing image cache", "id", artID, "size", size, err)
|
2022-12-28 10:19:52 -05:00
|
|
|
}
|
|
|
|
|
return nil, time.Time{}, err
|
2022-12-22 13:53:49 -05:00
|
|
|
}
|
2026-07-17 20:34:47 -04:00
|
|
|
reader = r
|
2026-07-17 21:46:12 -04:00
|
|
|
if size == 0 && !square && eligibleKind(artID) {
|
2026-07-17 20:34:47 -04:00
|
|
|
// Tee the served bytes: the blurhash is computed from exactly what the client downloads, so it
|
|
|
|
|
// changes precisely when the served cover changes. Placeholder bytes (playlist fallback) clear.
|
2026-07-17 21:13:03 -04:00
|
|
|
// The tee wraps r directly, so Close reaches the underlying stream (no fd leak).
|
2026-07-17 20:34:47 -04:00
|
|
|
version := capAtNow(artReader.LastUpdated())
|
2026-07-17 21:13:03 -04:00
|
|
|
reader = newTeeReader(r, maxTeeBytes,
|
refactor(artwork): compute the blurhash inline, drop the background worker
Decode+encode is a few ms (the encoder downscales before its pixel loops), and the
tee fires on Close after the response is fully written, so the hash can be computed
in the serving goroutine: the queue, wake/stop lifecycle, lazy-start admin context,
and the e2e worker-teardown ordering all become unnecessary and are removed. This
also closes two correctness holes the queue had: a deletion signal could be dropped
behind a pending serve job, and buffered image bytes had no global cap.
The in-memory dedup now remembers a checksum of the served bytes plus the persisted
version: identical serves skip the decode and the write, but the same bytes under a
newer entity version re-persist, so blur_hash_updated_at keeps pace with row updates
and the Jellyfin DTO's staleness gate keeps emitting the stored hash after scans.
Placeholder-triggered clears check the seen map, then the stored row, so coverless
entities cost one row read once per process instead of a probe and write per serve.
UpdateBlurHash is a plain UPDATE with no user filtering, so the request context is
used directly (a client abort is survived via context.WithoutCancel).
2026-07-17 21:20:46 -04:00
|
|
|
func(data []byte) { a.blurHashes.update(ctx, artID, data, version) })
|
2026-07-16 00:15:12 -04:00
|
|
|
}
|
2026-07-17 20:34:47 -04:00
|
|
|
return reader, artReader.LastUpdated(), nil
|
2022-12-27 12:54:51 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-08 13:49:05 -05:00
|
|
|
type coverArtGetter interface {
|
|
|
|
|
CoverArtID() model.ArtworkID
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-28 12:32:46 -05:00
|
|
|
func (a *artwork) getArtworkId(ctx context.Context, id string) (model.ArtworkID, error) {
|
|
|
|
|
if id == "" {
|
2023-01-31 18:22:49 -05:00
|
|
|
return model.ArtworkID{}, ErrUnavailable
|
2022-12-28 12:32:46 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-02-08 13:49:05 -05:00
|
|
|
if e, ok := entity.(coverArtGetter); ok {
|
|
|
|
|
artID = e.CoverArtID()
|
|
|
|
|
}
|
2022-12-28 12:32:46 -05:00
|
|
|
switch e := entity.(type) {
|
2022-12-30 23:52:53 -05:00
|
|
|
case *model.Artist:
|
|
|
|
|
log.Trace(ctx, "ID is for an Artist", "id", id, "name", e.Name, "artist", e.Name)
|
2022-12-28 12:32:46 -05:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 20:19:26 -04:00
|
|
|
func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, size int, square bool) (artworkReader, error) {
|
2022-12-27 12:54:51 -05:00
|
|
|
var artReader artworkReader
|
|
|
|
|
var err error
|
2024-05-24 20:19:26 -04:00
|
|
|
if size > 0 || square {
|
|
|
|
|
artReader, err = resizedFromOriginal(ctx, a, artID, size, square)
|
2022-12-27 12:54:51 -05:00
|
|
|
} else {
|
|
|
|
|
switch artID.Kind {
|
2022-12-30 23:52:53 -05:00
|
|
|
case model.KindArtistArtwork:
|
2025-05-22 15:48:24 -04:00
|
|
|
artReader, err = newArtistArtworkReader(ctx, a, artID, a.provider)
|
2022-12-27 12:54:51 -05:00
|
|
|
case model.KindAlbumArtwork:
|
2025-04-08 21:11:09 -04:00
|
|
|
artReader, err = newAlbumArtworkReader(ctx, a, artID, a.provider)
|
2022-12-27 12:54:51 -05:00
|
|
|
case model.KindMediaFileArtwork:
|
|
|
|
|
artReader, err = newMediafileArtworkReader(ctx, a, artID)
|
2022-12-27 18:28:56 -05:00
|
|
|
case model.KindPlaylistArtwork:
|
|
|
|
|
artReader, err = newPlaylistArtworkReader(ctx, a, artID)
|
feat(artwork): add per-disc cover art support (#5182)
* feat(artwork): add KindDiscArtwork and ParseDiscArtworkID
Add new disc artwork kind with 'dc' prefix for per-disc cover art
support. The composite ID format is albumID:discNumber, parsed by
the new ParseDiscArtworkID helper.
* feat(conf): add DiscArtPriority configuration option
Default: 'disc*.*, cd*.*, embedded'. Controls how per-disc cover
art is resolved, following the same pattern as CoverArtPriority
and ArtistArtPriority.
* feat(artwork): implement extractDiscNumber helper
Extracts disc number from filenames based on glob patterns by
parsing leading digits from the wildcard-matched portion.
Used for matching disc-specific artwork files like disc1.jpg.
* feat(artwork): implement fromDiscExternalFile source function
Disc-aware variant of fromExternalFile that filters image files
by disc number (extracted from filename) or folder association
(for multi-folder albums).
* feat(artwork): implement discArtworkReader
Resolves disc artwork using DiscArtPriority config patterns.
Supports glob patterns with disc number extraction, embedded
images from first track, and falls back to album cover art.
Handles both multi-folder and single-folder multi-disc albums.
* feat(artwork): register disc artwork reader in dispatcher
Add KindDiscArtwork case to getArtworkReader switch, routing
disc artwork requests to the new discArtworkReader.
* feat(subsonic): add CoverArt field to DiscTitle response
Implements OpenSubsonic PR #220: optional cover art ID in
DiscTitle responses for per-disc artwork support.
* feat(subsonic): populate CoverArt in DiscTitle responses
Each DiscTitle now includes a disc artwork ID (dc-albumID:discNum)
that clients can use with getCoverArt to retrieve per-disc artwork.
* style: fix file permission in test to satisfy gosec
* feat(ui): add disc cover art display and lightbox functionality
Signed-off-by: Deluan <deluan@navidrome.org>
* refactor: simplify disc artwork code
- Add DiscArtworkID constructor to encapsulate the "albumID:discNumber"
format in one place
- Convert fromDiscExternalFile to a method on discArtworkReader,
reducing parameter count from 6 to 2
- Remove unused rootFolder field from discArtworkReader
* style: fix prettier formatting in subsonic index
* style(ui): move cursor style to makeStyles in SongDatagrid
* feat(artwork): add discsubtitle option to DiscArtPriority
Allow matching disc cover art by the disc's subtitle/name.
When the "discsubtitle" keyword is in the priority list, image files
whose stem matches the disc subtitle (case-insensitive) are used.
This is useful for box sets with named discs (e.g., "The Blue Disc.jpg").
* feat(configuration): update discartpriority to include cover art options
Signed-off-by: Deluan <deluan@navidrome.org>
---------
Signed-off-by: Deluan <deluan@navidrome.org>
2026-03-13 18:33:18 -04:00
|
|
|
case model.KindDiscArtwork:
|
|
|
|
|
artReader, err = newDiscArtworkReader(ctx, a, artID)
|
2026-03-18 18:57:33 -04:00
|
|
|
case model.KindRadioArtwork:
|
|
|
|
|
artReader, err = newRadioArtworkReader(ctx, a, artID)
|
2022-12-27 12:54:51 -05:00
|
|
|
default:
|
2023-01-31 18:22:49 -05:00
|
|
|
return nil, ErrUnavailable
|
2022-12-27 12:54:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return artReader, err
|
2022-12-22 13:53:49 -05:00
|
|
|
}
|