navidrome/core/artwork.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

330 lines
8.6 KiB
Go
Raw Normal View History

package core
import (
2022-12-23 00:06:29 +01:00
"bufio"
2022-12-19 21:34:21 +01:00
"bytes"
"context"
2022-12-19 21:34:21 +01:00
"errors"
2022-12-19 23:07:29 +01:00
"fmt"
"image"
_ "image/gif"
2022-12-19 23:07:29 +01:00
"image/jpeg"
"image/png"
"io"
2022-12-23 00:06:29 +01:00
"net/http"
2022-12-19 21:34:21 +01:00
"os"
2022-12-19 23:07:29 +01:00
"path/filepath"
2022-12-20 18:25:47 +01:00
"reflect"
"runtime"
2022-12-19 23:07:29 +01:00
"strings"
2022-12-19 21:34:21 +01:00
"github.com/dhowden/tag"
2022-12-19 23:07:29 +01:00
"github.com/disintegration/imaging"
"github.com/navidrome/navidrome/conf"
2020-04-05 04:23:20 +02:00
"github.com/navidrome/navidrome/consts"
2022-12-20 18:25:47 +01:00
"github.com/navidrome/navidrome/core/ffmpeg"
2022-12-19 21:34:21 +01:00
"github.com/navidrome/navidrome/log"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/resources"
2022-12-20 17:27:40 +01:00
"github.com/navidrome/navidrome/utils/cache"
"github.com/navidrome/navidrome/utils/singleton"
2020-08-21 17:33:23 +02:00
_ "golang.org/x/image/webp"
)
type Artwork interface {
2020-11-17 19:30:37 +01:00
Get(ctx context.Context, id string, size int) (io.ReadCloser, error)
}
2022-12-20 18:25:47 +01:00
func NewArtwork(ds model.DataStore, cache cache.FileCache, ffmpeg ffmpeg.FFmpeg) Artwork {
return &artwork{ds: ds, cache: cache, ffmpeg: ffmpeg}
}
type artwork struct {
2022-12-20 18:25:47 +01:00
ds model.DataStore
cache cache.FileCache
ffmpeg ffmpeg.FFmpeg
2020-07-24 19:30:27 +02:00
}
2022-12-19 19:59:24 +01:00
func (a *artwork) Get(ctx context.Context, id string, size int) (io.ReadCloser, error) {
var artID model.ArtworkID
var err error
if id != "" {
artID, err = model.ParseArtworkID(id)
if err != nil {
return nil, errors.New("invalid ID")
}
2022-12-20 17:27:40 +01:00
}
key := &artworkKey{a: a, artID: artID, size: size}
2022-12-19 21:34:21 +01:00
2022-12-20 17:27:40 +01:00
r, err := a.cache.Get(ctx, key)
2022-12-21 00:49:59 +01:00
if err != nil && !errors.Is(err, context.Canceled) {
2022-12-20 17:27:40 +01:00
log.Error(ctx, "Error accessing image cache", "id", id, "size", size, err)
2022-12-19 21:34:21 +01:00
}
2022-12-20 17:27:40 +01:00
return r, err
}
2022-12-19 23:07:29 +01:00
type fromFunc func() (io.ReadCloser, string, error)
2022-12-19 23:07:29 +01:00
2022-12-23 00:06:29 +01:00
func (f fromFunc) String() string {
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
name = strings.TrimPrefix(name, "github.com/navidrome/navidrome/core.")
name = strings.TrimSuffix(name, ".func1")
return name
}
func (a *artwork) get(ctx context.Context, artID model.ArtworkID, size int) (reader io.ReadCloser, path string, err error) {
2022-12-24 22:21:49 +01:00
// If requested a resized image, get the original (possibly from cache)
if size > 0 {
r, err := a.Get(ctx, artID.String(), 0)
if err != nil {
return nil, "", err
}
defer r.Close()
resized, err := a.resizedFromOriginal(ctx, artID, r, size)
return io.NopCloser(resized), fmt.Sprintf("%s@%d", artID, size), err
}
2022-12-20 17:27:40 +01:00
switch artID.Kind {
2022-12-20 16:53:52 +01:00
case model.KindAlbumArtwork:
2022-12-20 17:27:40 +01:00
reader, path = a.extractAlbumImage(ctx, artID)
2022-12-20 16:53:52 +01:00
case model.KindMediaFileArtwork:
2022-12-20 17:27:40 +01:00
reader, path = a.extractMediaFileImage(ctx, artID)
2022-12-20 16:53:52 +01:00
default:
reader, path, _ = fromPlaceholder()()
2022-12-20 16:53:52 +01:00
}
2022-12-21 00:49:59 +01:00
return reader, path, ctx.Err()
2022-12-20 16:53:52 +01:00
}
2022-12-20 17:27:40 +01:00
func (a *artwork) extractAlbumImage(ctx context.Context, artID model.ArtworkID) (io.ReadCloser, string) {
al, err := a.ds.Album(ctx).Get(artID.ID)
2022-12-19 21:34:21 +01:00
if errors.Is(err, model.ErrNotFound) {
r, path, _ := fromPlaceholder()()
2022-12-20 16:53:52 +01:00
return r, path
2022-12-19 21:34:21 +01:00
}
if err != nil {
2022-12-20 17:27:40 +01:00
log.Error(ctx, "Could not retrieve album", "id", artID.ID, err)
2022-12-20 16:53:52 +01:00
return nil, ""
2022-12-19 21:34:21 +01:00
}
2022-12-22 19:53:49 +01:00
var ff = a.fromCoverArtPriority(ctx, conf.Server.CoverArtPriority, *al)
ff = append(ff, fromPlaceholder())
return extractImage(ctx, artID, ff...)
2022-12-20 16:53:52 +01:00
}
2022-12-20 17:27:40 +01:00
func (a *artwork) extractMediaFileImage(ctx context.Context, artID model.ArtworkID) (reader io.ReadCloser, path string) {
mf, err := a.ds.MediaFile(ctx).Get(artID.ID)
2022-12-20 16:53:52 +01:00
if errors.Is(err, model.ErrNotFound) {
r, path, _ := fromPlaceholder()()
2022-12-20 16:53:52 +01:00
return r, path
}
if err != nil {
2022-12-20 17:27:40 +01:00
log.Error(ctx, "Could not retrieve mediafile", "id", artID.ID, err)
2022-12-20 16:53:52 +01:00
return nil, ""
}
var ff []fromFunc
2022-12-22 17:47:18 +01:00
if mf.CoverArtID().Kind == model.KindMediaFileArtwork {
ff = []fromFunc{
fromTag(mf.Path),
fromFFmpegTag(ctx, a.ffmpeg, mf.Path),
}
}
ff = append(ff, a.fromAlbum(ctx, mf.AlbumCoverArtID()))
return extractImage(ctx, artID, ff...)
2022-12-20 16:53:52 +01:00
}
2022-12-23 00:06:29 +01:00
func (a *artwork) resizedFromOriginal(ctx context.Context, artID model.ArtworkID, original io.Reader, size int) (io.Reader, error) {
// Keep a copy of the original data. In case we can't resize it, send it as is
buf := new(bytes.Buffer)
r := io.TeeReader(original, buf)
2022-12-23 00:06:29 +01:00
resized, err := resizeImage(r, size)
2022-12-19 23:07:29 +01:00
if err != nil {
2022-12-23 00:06:29 +01:00
log.Warn(ctx, "Could not resize image. Will return image as is", "artID", artID, "size", size, err)
// Force finish reading any remaining data
_, _ = io.Copy(io.Discard, r)
return buf, nil
2022-12-19 23:07:29 +01:00
}
2022-12-23 00:06:29 +01:00
return resized, nil
2022-12-20 18:55:40 +01:00
}
func extractImage(ctx context.Context, artID model.ArtworkID, extractFuncs ...fromFunc) (io.ReadCloser, string) {
2022-12-19 21:34:21 +01:00
for _, f := range extractFuncs {
2022-12-21 00:49:59 +01:00
if ctx.Err() != nil {
return nil, ""
}
r, path, err := f()
2022-12-19 21:34:21 +01:00
if r != nil {
2022-12-20 18:55:40 +01:00
log.Trace(ctx, "Found artwork", "artID", artID, "path", path, "origin", f)
2022-12-19 21:34:21 +01:00
return r, path
}
log.Trace(ctx, "Tried to extract artwork", "artID", artID, "origin", f, err)
2022-12-19 21:34:21 +01:00
}
2022-12-20 17:27:40 +01:00
log.Error(ctx, "extractImage should never reach this point!", "artID", artID, "path")
2022-12-19 21:34:21 +01:00
return nil, ""
}
2022-12-20 18:55:40 +01:00
func (a *artwork) fromAlbum(ctx context.Context, id model.ArtworkID) fromFunc {
return func() (io.ReadCloser, string, error) {
2022-12-20 18:55:40 +01:00
r, path, err := a.get(ctx, id, 0)
if err != nil {
return nil, "", err
2022-12-20 18:55:40 +01:00
}
return r, path, nil
2022-12-20 18:55:40 +01:00
}
2022-12-20 18:25:47 +01:00
}
2022-12-22 19:53:49 +01:00
func (a *artwork) fromCoverArtPriority(ctx context.Context, priority string, al model.Album) []fromFunc {
var ff []fromFunc
for _, pattern := range strings.Split(strings.ToLower(priority), ",") {
pattern = strings.TrimSpace(pattern)
if pattern == "embedded" {
2022-12-22 19:53:49 +01:00
ff = append(ff, fromTag(al.EmbedArtPath), fromFFmpegTag(ctx, a.ffmpeg, al.EmbedArtPath))
continue
}
if al.ImageFiles != "" {
ff = append(ff, fromExternalFile(ctx, al.ImageFiles, pattern))
}
2022-12-22 19:53:49 +01:00
}
return ff
}
func fromExternalFile(ctx context.Context, files string, pattern string) fromFunc {
return func() (io.ReadCloser, string, error) {
2022-12-22 19:53:49 +01:00
for _, file := range filepath.SplitList(files) {
_, name := filepath.Split(file)
match, err := filepath.Match(pattern, strings.ToLower(name))
if err != nil {
log.Warn(ctx, "Error matching cover art file to pattern", "pattern", pattern, "file", file)
continue
}
if !match {
continue
}
f, err := os.Open(file)
if err != nil {
log.Warn(ctx, "Could not open cover art file", "file", file, err)
2022-12-22 19:53:49 +01:00
continue
2022-12-19 23:07:29 +01:00
}
return f, file, err
2022-12-19 23:07:29 +01:00
}
return nil, "", fmt.Errorf("pattern '%s' not matched by files %v", pattern, files)
2022-12-19 23:07:29 +01:00
}
}
2022-12-20 18:55:40 +01:00
func fromTag(path string) fromFunc {
return func() (io.ReadCloser, string, error) {
2022-12-19 23:07:29 +01:00
if path == "" {
return nil, "", nil
2022-12-19 23:07:29 +01:00
}
2022-12-19 21:34:21 +01:00
f, err := os.Open(path)
if err != nil {
return nil, "", err
2022-12-19 21:34:21 +01:00
}
defer f.Close()
m, err := tag.ReadFrom(f)
if err != nil {
return nil, "", err
2022-12-19 21:34:21 +01:00
}
picture := m.Picture()
if picture == nil {
return nil, "", fmt.Errorf("no embedded image found in %s", path)
2022-12-19 21:34:21 +01:00
}
return io.NopCloser(bytes.NewReader(picture.Data)), path, nil
2022-12-19 21:34:21 +01:00
}
}
2022-12-20 18:55:40 +01:00
func fromFFmpegTag(ctx context.Context, ffmpeg ffmpeg.FFmpeg, path string) fromFunc {
return func() (io.ReadCloser, string, error) {
2022-12-20 18:25:47 +01:00
if path == "" {
return nil, "", nil
2022-12-20 18:25:47 +01:00
}
r, err := ffmpeg.ExtractImage(ctx, path)
if err != nil {
return nil, "", err
2022-12-20 18:25:47 +01:00
}
defer r.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
if err != nil {
return nil, "", err
}
return io.NopCloser(buf), path, nil
2022-12-20 18:25:47 +01:00
}
}
2022-12-20 18:55:40 +01:00
func fromPlaceholder() fromFunc {
return func() (io.ReadCloser, string, error) {
2022-12-19 21:34:21 +01:00
r, _ := resources.FS().Open(consts.PlaceholderAlbumArt)
return r, consts.PlaceholderAlbumArt, nil
2022-12-19 21:34:21 +01:00
}
2022-12-19 19:59:24 +01:00
}
2022-12-19 23:07:29 +01:00
2022-12-23 00:06:29 +01:00
func asImageReader(r io.Reader) (io.Reader, string, error) {
br := bufio.NewReader(r)
buf, err := br.Peek(512)
if err != nil {
return nil, "", err
}
return br, http.DetectContentType(buf), nil
}
func resizeImage(reader io.Reader, size int) (io.Reader, error) {
r, format, err := asImageReader(reader)
if err != nil {
return nil, err
}
img, _, err := image.Decode(r)
2022-12-19 23:07:29 +01:00
if err != nil {
return nil, err
}
// Preserve the aspect ratio of the image.
var m *image.NRGBA
bounds := img.Bounds()
if bounds.Max.X > bounds.Max.Y {
m = imaging.Resize(img, size, 0, imaging.Lanczos)
} else {
m = imaging.Resize(img, 0, size, imaging.Lanczos)
}
buf := new(bytes.Buffer)
2022-12-23 00:06:29 +01:00
buf.Reset()
if format == "image/png" {
2022-12-19 23:07:29 +01:00
err = png.Encode(buf, m)
} else {
err = jpeg.Encode(buf, m, &jpeg.Options{Quality: conf.Server.CoverJpegQuality})
}
2022-12-23 00:06:29 +01:00
return buf, err
2022-12-19 23:07:29 +01:00
}
2022-12-20 17:27:40 +01:00
type ArtworkCache struct {
cache.FileCache
}
type artworkKey struct {
2022-12-20 18:55:40 +01:00
a *artwork
artID model.ArtworkID
size int
2022-12-20 17:27:40 +01:00
}
func (k *artworkKey) Key() string {
2022-12-22 20:13:01 +01:00
return fmt.Sprintf("%s.%d.%d", k.artID, k.size, conf.Server.CoverJpegQuality)
2022-12-20 17:27:40 +01:00
}
func GetImageCache() cache.FileCache {
return singleton.GetInstance(func() *ArtworkCache {
return &ArtworkCache{
FileCache: cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems,
func(ctx context.Context, arg cache.Item) (io.Reader, error) {
info := arg.(*artworkKey)
r, _, err := info.a.get(ctx, info.artID, info.size)
return r, err
}),
}
})
}