Artwork reader for Artist

This commit is contained in:
Deluan
2022-12-31 16:58:07 -05:00
committed by Deluan Quintão
parent bf461473ef
commit 918fee3ea3
11 changed files with 99 additions and 24 deletions
+5 -1
View File
@@ -37,9 +37,13 @@ func (p *localAgent) GetImages(_ context.Context, id, name, mbid string) ([]Arti
}, nil
}
func (p *localAgent) GetTopSongs(ctx context.Context, id, artistName, mbid string, count int) ([]Song, error) {
return nil, nil // TODO return 5-stars and liked songs sorted by playCount
}
func (p *localAgent) artistImage(id string, size int) ArtistImage {
return ArtistImage{
filepath.Join(consts.URLPathPublicImages, artwork.Public(model.NewArtworkID(model.KindArtistArtwork, id), size)),
filepath.Join(consts.URLPathPublicImages, artwork.PublicLink(model.NewArtworkID(model.KindArtistArtwork, id), size)),
size,
}
}
+1 -4
View File
@@ -36,9 +36,6 @@ type artworkReader interface {
}
func (a *artwork) Get(ctx context.Context, id string, size int) (reader io.ReadCloser, lastUpdate time.Time, err error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
artID, err := a.getArtworkId(ctx, id)
if err != nil {
return nil, time.Time{}, err
@@ -112,7 +109,7 @@ func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, s
return artReader, err
}
func Public(artID model.ArtworkID, size int) string {
func PublicLink(artID model.ArtworkID, size int) string {
token, _ := auth.CreatePublicToken(map[string]any{
"id": artID.String(),
"size": size,
+3
View File
@@ -103,6 +103,9 @@ func (a *cacheWarmer) processBatch(ctx context.Context, batch []string) {
}
func (a *cacheWarmer) doCacheImage(ctx context.Context, id string) error {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
r, _, err := a.artwork.Get(ctx, id, 0)
if err != nil {
return fmt.Errorf("error cacheing id='%s': %w", id, err)
+53 -12
View File
@@ -4,32 +4,73 @@ import (
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/model"
)
type artistReader struct {
artID model.ArtworkID
cacheKey
a *artwork
artist model.Artist
files []string
}
func newArtistReader(_ context.Context, _ *artwork, artID model.ArtworkID) (*artistReader, error) {
a := &artistReader{
artID: artID,
func newArtistReader(ctx context.Context, artwork *artwork, artID model.ArtworkID) (*artistReader, error) {
ar, err := artwork.ds.Artist(ctx).Get(artID.ID)
if err != nil {
return nil, err
}
als, err := artwork.ds.Album(ctx).GetAll(model.QueryOptions{Filters: squirrel.Eq{"album_artist_id": artID.ID}})
if err != nil {
return nil, err
}
a := &artistReader{
a: artwork,
artist: *ar,
}
a.cacheKey.lastUpdate = ar.ExternalInfoUpdatedAt
for _, al := range als {
a.files = append(a.files, al.ImageFiles)
if a.cacheKey.lastUpdate.Before(al.UpdatedAt) {
a.cacheKey.lastUpdate = al.UpdatedAt
}
}
a.cacheKey.artID = artID
return a, nil
}
func (a *artistReader) LastUpdated() time.Time {
return consts.ServerStart // Invalidate cached placeholder every server start
}
func (a *artistReader) Key() string {
return fmt.Sprintf("placeholder.%d.0.%d", a.LastUpdated().UnixMilli(), conf.Server.CoverJpegQuality)
return a.lastUpdate
}
func (a *artistReader) Reader(ctx context.Context) (io.ReadCloser, string, error) {
return selectImageReader(ctx, a.artID, fromArtistPlaceholder())
return selectImageReader(ctx, a.artID,
//fromExternalFile()
fromExternalSource(ctx, a.artist),
fromArtistPlaceholder(),
)
}
func fromExternalSource(ctx context.Context, ar model.Artist) sourceFunc {
return func() (io.ReadCloser, string, error) {
imageUrl := ar.ArtistImageUrl()
if !strings.HasPrefix(imageUrl, "http") {
return nil, "", nil
}
hc := http.Client{Timeout: 5 * time.Second}
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, imageUrl, nil)
resp, err := hc.Do(req)
if err != nil {
return nil, "", err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, "", fmt.Errorf("error retrieveing cover from %s: %s", imageUrl, resp.Status)
}
return resp.Body, imageUrl, nil
}
}
+4
View File
@@ -2,6 +2,7 @@ package core
import (
"context"
"errors"
"sort"
"strings"
"sync"
@@ -224,6 +225,9 @@ func (e *externalMetadata) TopSongs(ctx context.Context, artistName string, coun
func (e *externalMetadata) getMatchingTopSongs(ctx context.Context, agent agents.ArtistTopSongsRetriever, artist *auxArtist, count int) (model.MediaFiles, error) {
songs, err := agent.GetTopSongs(ctx, artist.ID, artist.Name, artist.MbzArtistID, count)
if errors.Is(err, agents.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}