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")
|
|
|
|
|
|
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 {
|
|
|
|
|
return &artwork{ds: ds, cache: cache, ffmpeg: ffmpeg, provider: provider}
|
2016-03-08 23:05:54 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:31:19 -04:00
|
|
|
type artwork struct {
|
2025-04-08 21:11:09 -04:00
|
|
|
ds model.DataStore
|
|
|
|
|
cache cache.FileCache
|
|
|
|
|
ffmpeg ffmpeg.FFmpeg
|
|
|
|
|
provider external.Provider
|
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) {
|
|
|
|
|
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
|
|
|
}
|
2022-12-28 10:19:52 -05:00
|
|
|
return r, 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)
|
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
|
|
|
}
|