Skip to content

Commit

Permalink
Parse correctly playlist CoverArt ids
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Dec 28, 2022
1 parent 8f3387a commit 501386b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core/artwork/artwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a *artwork) getArtworkReader(ctx context.Context, artID model.ArtworkID, s
case model.KindMediaFileArtwork:
artReader, err = newMediafileArtworkReader(ctx, a, artID)
default:
artReader, err = newEmptyIDReader(ctx, artID)
artReader, err = newPlaceholderReader(ctx, artID)
}
}
return artReader, err
Expand Down
12 changes: 6 additions & 6 deletions core/artwork/reader_emptyid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import (
"github.com/navidrome/navidrome/model"
)

type emptyIDReader struct {
type placeholderReader struct {
artID model.ArtworkID
}

func newEmptyIDReader(_ context.Context, artID model.ArtworkID) (*emptyIDReader, error) {
a := &emptyIDReader{
func newPlaceholderReader(_ context.Context, artID model.ArtworkID) (*placeholderReader, error) {
a := &placeholderReader{
artID: artID,
}
return a, nil
}

func (a *emptyIDReader) LastUpdated() time.Time {
func (a *placeholderReader) LastUpdated() time.Time {
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)
}

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())
return r, source, nil
}
13 changes: 11 additions & 2 deletions model/artwork_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import (
"errors"
"fmt"
"strings"

"golang.org/x/exp/slices"
)

type Kind struct{ prefix string }

var (
KindMediaFileArtwork = Kind{"mf"}
KindAlbumArtwork = Kind{"al"}
KindPlaylistArtwork = Kind{"pl"}
)

var artworkKindList = []string{
KindAlbumArtwork.prefix,
KindMediaFileArtwork.prefix,
KindPlaylistArtwork.prefix,
}

type ArtworkID struct {
Kind Kind
ID string
Expand All @@ -26,11 +35,11 @@ func (id ArtworkID) String() string {
}

func ParseArtworkID(id string) (ArtworkID, error) {
parts := strings.Split(id, "-")
parts := strings.SplitN(id, "-", 2)
if len(parts) != 2 {
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{
Expand Down
6 changes: 6 additions & 0 deletions model/artwork_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var _ = Describe("ParseArtworkID()", func() {
Expect(id.Kind).To(Equal(model.KindMediaFileArtwork))
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() {
_, err := model.ParseArtworkID("a6f8d2b1")
Expect(err).To(MatchError("invalid artwork id"))
Expand Down

0 comments on commit 501386b

Please sign in to comment.