Handle "naked" CoverArtIDs (IDs of album, mediafiles and playlists)

This commit is contained in:
Deluan 2022-12-28 12:32:46 -05:00 committed by Deluan Quintão
parent bc09de6640
commit 61e5523457
14 changed files with 82 additions and 25 deletions

View file

@ -92,7 +92,7 @@ func createScanner() scanner.Scanner {
// wire_injectors.go: // wire_injectors.go:
var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, listenbrainz.NewRouter, events.GetBroker, db.Db) var allProviders = wire.NewSet(core.Set, artwork.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, listenbrainz.NewRouter, events.GetBroker, db.Db)
// Scanner must be a Singleton // Scanner must be a Singleton
var ( var (

View file

@ -9,6 +9,7 @@ import (
"github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/agents/lastfm" "github.com/navidrome/navidrome/core/agents/lastfm"
"github.com/navidrome/navidrome/core/agents/listenbrainz" "github.com/navidrome/navidrome/core/agents/listenbrainz"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/persistence" "github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner" "github.com/navidrome/navidrome/scanner"
@ -20,6 +21,7 @@ import (
var allProviders = wire.NewSet( var allProviders = wire.NewSet(
core.Set, core.Set,
artwork.Set,
subsonic.New, subsonic.New,
nativeapi.New, nativeapi.New,
persistence.New, persistence.New,

View file

@ -38,12 +38,9 @@ func (a *artwork) Get(ctx context.Context, id string, size int) (reader io.ReadC
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel() defer cancel()
var artID model.ArtworkID artID, err := a.getArtworkId(ctx, id)
if id != "" {
artID, err = model.ParseArtworkID(id)
if err != nil { if err != nil {
return nil, time.Time{}, errors.New("invalid ID") return nil, time.Time{}, err
}
} }
artReader, err := a.getArtworkReader(ctx, artID, size) artReader, err := a.getArtworkReader(ctx, artID, size)
@ -61,6 +58,34 @@ func (a *artwork) Get(ctx context.Context, id string, size int) (reader io.ReadC
return r, artReader.LastUpdated(), nil return r, artReader.LastUpdated(), nil
} }
func (a *artwork) getArtworkId(ctx context.Context, id string) (model.ArtworkID, error) {
if id == "" {
return model.ArtworkID{}, nil
}
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
}
switch e := entity.(type) {
case *model.Album:
artID = model.NewArtworkID(model.KindAlbumArtwork, e.ID)
log.Trace(ctx, "ID is for an Album", "id", id, "name", e.Name, "artist", e.AlbumArtist)
case *model.MediaFile:
artID = model.NewArtworkID(model.KindMediaFileArtwork, e.ID)
log.Trace(ctx, "ID is for a MediaFile", "id", id, "title", e.Title, "album", e.Album)
case *model.Playlist:
artID = model.NewArtworkID(model.KindPlaylistArtwork, e.ID)
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) (artworkReader, error) { func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, size int) (artworkReader, error) {
var artReader artworkReader var artReader artworkReader
var err error var err error

View file

@ -13,6 +13,7 @@ import (
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils/slice"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )
@ -66,10 +67,9 @@ func (a *playlistArtworkReader) fromGeneratedTile(ctx context.Context, tracks mo
func compactIDs(tracks model.PlaylistTracks) []model.ArtworkID { func compactIDs(tracks model.PlaylistTracks) []model.ArtworkID {
slices.SortFunc(tracks, func(a, b model.PlaylistTrack) bool { return a.AlbumID < b.AlbumID }) slices.SortFunc(tracks, func(a, b model.PlaylistTrack) bool { return a.AlbumID < b.AlbumID })
tracks = slices.CompactFunc(tracks, func(a, b model.PlaylistTrack) bool { return a.AlbumID == b.AlbumID }) tracks = slices.CompactFunc(tracks, func(a, b model.PlaylistTrack) bool { return a.AlbumID == b.AlbumID })
ids := make([]model.ArtworkID, len(tracks)) ids := slice.Map(tracks, func(e model.PlaylistTrack) model.ArtworkID {
for i, t := range tracks { return e.AlbumCoverArtID()
ids[i] = t.AlbumCoverArtID() })
}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(ids), func(i, j int) { ids[i], ids[j] = ids[j], ids[i] }) rand.Shuffle(len(ids), func(i, j int) { ids[i], ids[j] = ids[j], ids[i] })
return ids return ids

View file

@ -0,0 +1,11 @@
package artwork
import (
"github.com/google/wire"
)
var Set = wire.NewSet(
NewArtwork,
GetImageCache,
NewCacheWarmer,
)

View file

@ -47,7 +47,7 @@ func NewExternalMetadata(ds model.DataStore, agents *agents.Agents) ExternalMeta
func (e *externalMetadata) getArtist(ctx context.Context, id string) (*auxArtist, error) { func (e *externalMetadata) getArtist(ctx context.Context, id string) (*auxArtist, error) {
var entity interface{} var entity interface{}
entity, err := GetEntityByID(ctx, e.ds, id) entity, err := model.GetEntityByID(ctx, e.ds, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,7 +3,6 @@ package core
import ( import (
"github.com/google/wire" "github.com/google/wire"
"github.com/navidrome/navidrome/core/agents" "github.com/navidrome/navidrome/core/agents"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/ffmpeg" "github.com/navidrome/navidrome/core/ffmpeg"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
) )
@ -19,7 +18,4 @@ var Set = wire.NewSet(
agents.New, agents.New,
ffmpeg.New, ffmpeg.New,
scrobbler.GetPlayTracker, scrobbler.GetPlayTracker,
artwork.NewArtwork,
artwork.GetImageCache,
artwork.NewCacheWarmer,
) )

View file

@ -34,6 +34,10 @@ func (id ArtworkID) String() string {
return fmt.Sprintf("%s-%s", id.Kind.prefix, id.ID) return fmt.Sprintf("%s-%s", id.Kind.prefix, id.ID)
} }
func NewArtworkID(kind Kind, id string) ArtworkID {
return ArtworkID{kind, id}
}
func ParseArtworkID(id string) (ArtworkID, error) { func ParseArtworkID(id string) (ArtworkID, error) {
parts := strings.SplitN(id, "-", 2) parts := strings.SplitN(id, "-", 2)
if len(parts) != 2 { if len(parts) != 2 {

View file

@ -1,13 +1,11 @@
package core package model
import ( import (
"context" "context"
"github.com/navidrome/navidrome/model"
) )
// TODO: Should the type be encoded in the ID? // TODO: Should the type be encoded in the ID?
func GetEntityByID(ctx context.Context, ds model.DataStore, id string) (interface{}, error) { func GetEntityByID(ctx context.Context, ds DataStore, id string) (interface{}, error) {
ar, err := ds.Artist(ctx).Get(id) ar, err := ds.Artist(ctx).Get(id)
if err == nil { if err == nil {
return ar, nil return ar, nil

View file

@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/subsonic/filter" "github.com/navidrome/navidrome/server/subsonic/filter"
@ -95,7 +94,7 @@ func (api *Router) GetMusicDirectory(r *http.Request) (*responses.Subsonic, erro
id := utils.ParamString(r, "id") id := utils.ParamString(r, "id")
ctx := r.Context() ctx := r.Context()
entity, err := core.GetEntityByID(ctx, api.ds, id) entity, err := model.GetEntityByID(ctx, api.ds, id)
if errors.Is(err, model.ErrNotFound) { if errors.Is(err, model.ErrNotFound) {
log.Error(r, "Requested ID not found ", "id", id) log.Error(r, "Requested ID not found ", "id", id)
return nil, newError(responses.ErrorDataNotFound, "Directory not found") return nil, newError(responses.ErrorDataNotFound, "Directory not found")

View file

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
@ -46,7 +45,7 @@ func (api *Router) setRating(ctx context.Context, id string, rating int) error {
var repo model.AnnotatedRepository var repo model.AnnotatedRepository
var resource string var resource string
entity, err := core.GetEntityByID(ctx, api.ds, id) entity, err := model.GetEntityByID(ctx, api.ds, id)
if err != nil { if err != nil {
return err return err
} }

View file

@ -91,7 +91,7 @@ func (api *Router) Download(w http.ResponseWriter, r *http.Request) (*responses.
return nil, newError(responses.ErrorAuthorizationFail, "downloads are disabled") return nil, newError(responses.ErrorAuthorizationFail, "downloads are disabled")
} }
entity, err := core.GetEntityByID(ctx, api.ds, id) entity, err := model.GetEntityByID(ctx, api.ds, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,5 +1,13 @@
package slice package slice
func Map[T any, R any](t []T, mapFunc func(T) R) []R {
r := make([]R, len(t))
for i, e := range t {
r[i] = mapFunc(e)
}
return r
}
func Group[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T { func Group[T any, K comparable](s []T, keyFunc func(T) K) map[K][]T {
m := map[K][]T{} m := map[K][]T{}
for _, item := range s { for _, item := range s {

View file

@ -1,6 +1,7 @@
package slice_test package slice_test
import ( import (
"strconv"
"testing" "testing"
"github.com/navidrome/navidrome/utils/slice" "github.com/navidrome/navidrome/utils/slice"
@ -13,6 +14,20 @@ func TestSlice(t *testing.T) {
RunSpecs(t, "Slice Suite") RunSpecs(t, "Slice Suite")
} }
var _ = Describe("Map", func() {
It("returns empty slice for an empty input", func() {
mapFunc := func(v int) string { return strconv.Itoa(v * 2) }
result := slice.Map([]int{}, mapFunc)
Expect(result).To(BeEmpty())
})
It("returns a new slice with elements mapped", func() {
mapFunc := func(v int) string { return strconv.Itoa(v * 2) }
result := slice.Map([]int{1, 2, 3, 4}, mapFunc)
Expect(result).To(ConsistOf("2", "4", "6", "8"))
})
})
var _ = Describe("Group", func() { var _ = Describe("Group", func() {
It("returns empty map for an empty input", func() { It("returns empty map for an empty input", func() {
keyFunc := func(v int) int { return v % 2 } keyFunc := func(v int) int { return v % 2 }