Parse correctly playlist CoverArt ids

This commit is contained in:
Deluan 2022-12-27 17:05:40 -05:00 committed by Deluan Quintão
parent 8f3387a894
commit 501386b11f
4 changed files with 24 additions and 9 deletions

View file

@ -70,7 +70,7 @@ func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, s
case model.KindMediaFileArtwork: case model.KindMediaFileArtwork:
artReader, err = newMediafileArtworkReader(ctx, a, artID) artReader, err = newMediafileArtworkReader(ctx, a, artID)
default: default:
artReader, err = newEmptyIDReader(ctx, artID) artReader, err = newPlaceholderReader(ctx, artID)
} }
} }
return artReader, err return artReader, err

View file

@ -10,26 +10,26 @@ import (
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
) )
type emptyIDReader struct { type placeholderReader struct {
artID model.ArtworkID artID model.ArtworkID
} }
func newEmptyIDReader(_ context.Context, artID model.ArtworkID) (*emptyIDReader, error) { func newPlaceholderReader(_ context.Context, artID model.ArtworkID) (*placeholderReader, error) {
a := &emptyIDReader{ a := &placeholderReader{
artID: artID, artID: artID,
} }
return a, nil return a, nil
} }
func (a *emptyIDReader) LastUpdated() time.Time { func (a *placeholderReader) LastUpdated() time.Time {
return time.Now() // Basically make it non-cacheable return time.Now() // Basically make it non-cacheable
} }
func (a *emptyIDReader) Key() string { func (a *placeholderReader) Key() string {
return fmt.Sprintf("0.%d.0.%d", a.LastUpdated().UnixMilli(), conf.Server.CoverJpegQuality) return fmt.Sprintf("0.%d.0.%d", a.LastUpdated().UnixMilli(), conf.Server.CoverJpegQuality)
} }
func (a *emptyIDReader) Reader(ctx context.Context) (io.ReadCloser, string, error) { func (a *placeholderReader) Reader(ctx context.Context) (io.ReadCloser, string, error) {
r, source := extractImage(ctx, a.artID, fromPlaceholder()) r, source := extractImage(ctx, a.artID, fromPlaceholder())
return r, source, nil return r, source, nil
} }

View file

@ -4,6 +4,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"golang.org/x/exp/slices"
) )
type Kind struct{ prefix string } type Kind struct{ prefix string }
@ -11,8 +13,15 @@ type Kind struct{ prefix string }
var ( var (
KindMediaFileArtwork = Kind{"mf"} KindMediaFileArtwork = Kind{"mf"}
KindAlbumArtwork = Kind{"al"} KindAlbumArtwork = Kind{"al"}
KindPlaylistArtwork = Kind{"pl"}
) )
var artworkKindList = []string{
KindAlbumArtwork.prefix,
KindMediaFileArtwork.prefix,
KindPlaylistArtwork.prefix,
}
type ArtworkID struct { type ArtworkID struct {
Kind Kind Kind Kind
ID string ID string
@ -26,11 +35,11 @@ func (id ArtworkID) String() string {
} }
func ParseArtworkID(id string) (ArtworkID, error) { func ParseArtworkID(id string) (ArtworkID, error) {
parts := strings.Split(id, "-") parts := strings.SplitN(id, "-", 2)
if len(parts) != 2 { if len(parts) != 2 {
return ArtworkID{}, errors.New("invalid artwork id") return ArtworkID{}, errors.New("invalid artwork id")
} }
if parts[0] != KindAlbumArtwork.prefix && parts[0] != KindMediaFileArtwork.prefix { if !slices.Contains(artworkKindList, parts[0]) {
return ArtworkID{}, errors.New("invalid artwork kind") return ArtworkID{}, errors.New("invalid artwork kind")
} }
return ArtworkID{ return ArtworkID{

View file

@ -19,6 +19,12 @@ var _ = Describe("ParseArtworkID()", func() {
Expect(id.Kind).To(Equal(model.KindMediaFileArtwork)) Expect(id.Kind).To(Equal(model.KindMediaFileArtwork))
Expect(id.ID).To(Equal("a6f8d2b1")) Expect(id.ID).To(Equal("a6f8d2b1"))
}) })
It("parses playlists artwork ids", func() {
id, err := model.ParseArtworkID("pl-18690de0-151b-4d86-81cb-f418a907315a")
Expect(err).ToNot(HaveOccurred())
Expect(id.Kind).To(Equal(model.KindPlaylistArtwork))
Expect(id.ID).To(Equal("18690de0-151b-4d86-81cb-f418a907315a"))
})
It("fails to parse malformed ids", func() { It("fails to parse malformed ids", func() {
_, err := model.ParseArtworkID("a6f8d2b1") _, err := model.ParseArtworkID("a6f8d2b1")
Expect(err).To(MatchError("invalid artwork id")) Expect(err).To(MatchError("invalid artwork id"))