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

@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"strings"
"golang.org/x/exp/slices"
)
type Kind struct{ prefix string }
@ -11,8 +13,15 @@ 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
@ -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{

View file

@ -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"))