Add lastUpdated to coverArt ids. Helps with invalidating art cache client-side.
This commit is contained in:
+43
-17
@@ -3,7 +3,9 @@ package model
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Kind struct {
|
||||
@@ -30,19 +32,28 @@ var artworkKindMap = map[string]Kind{
|
||||
}
|
||||
|
||||
type ArtworkID struct {
|
||||
Kind Kind
|
||||
ID string
|
||||
Kind Kind
|
||||
ID string
|
||||
LastUpdate time.Time
|
||||
}
|
||||
|
||||
func (id ArtworkID) String() string {
|
||||
if id.ID == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%s-%s", id.Kind.prefix, id.ID)
|
||||
s := fmt.Sprintf("%s-%s", id.Kind.prefix, id.ID)
|
||||
if lu := id.LastUpdate.Unix(); lu > 0 {
|
||||
return fmt.Sprintf("%s_%x", s, lu)
|
||||
}
|
||||
return s + "_0"
|
||||
}
|
||||
|
||||
func NewArtworkID(kind Kind, id string) ArtworkID {
|
||||
return ArtworkID{kind, id}
|
||||
func NewArtworkID(kind Kind, id string, lastUpdate *time.Time) ArtworkID {
|
||||
artID := ArtworkID{kind, id, time.Time{}}
|
||||
if lastUpdate != nil {
|
||||
artID.LastUpdate = *lastUpdate
|
||||
}
|
||||
return artID
|
||||
}
|
||||
|
||||
func ParseArtworkID(id string) (ArtworkID, error) {
|
||||
@@ -50,14 +61,26 @@ func ParseArtworkID(id string) (ArtworkID, error) {
|
||||
if len(parts) != 2 {
|
||||
return ArtworkID{}, errors.New("invalid artwork id")
|
||||
}
|
||||
if kind, ok := artworkKindMap[parts[0]]; !ok {
|
||||
kind, ok := artworkKindMap[parts[0]]
|
||||
if !ok {
|
||||
return ArtworkID{}, errors.New("invalid artwork kind")
|
||||
} else {
|
||||
return ArtworkID{
|
||||
Kind: kind,
|
||||
ID: parts[1],
|
||||
}, nil
|
||||
}
|
||||
parsedID := ArtworkID{
|
||||
Kind: kind,
|
||||
ID: parts[1],
|
||||
}
|
||||
parts = strings.SplitN(parts[1], "_", 2)
|
||||
if len(parts) == 2 {
|
||||
if parts[1] != "0" {
|
||||
lastUpdate, err := strconv.ParseInt(parts[1], 16, 64)
|
||||
if err != nil {
|
||||
return ArtworkID{}, err
|
||||
}
|
||||
parsedID.LastUpdate = time.Unix(lastUpdate, 0)
|
||||
}
|
||||
parsedID.ID = parts[0]
|
||||
}
|
||||
return parsedID, nil
|
||||
}
|
||||
|
||||
func MustParseArtworkID(id string) ArtworkID {
|
||||
@@ -70,22 +93,25 @@ func MustParseArtworkID(id string) ArtworkID {
|
||||
|
||||
func artworkIDFromAlbum(al Album) ArtworkID {
|
||||
return ArtworkID{
|
||||
Kind: KindAlbumArtwork,
|
||||
ID: al.ID,
|
||||
Kind: KindAlbumArtwork,
|
||||
ID: al.ID,
|
||||
LastUpdate: al.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func artworkIDFromMediaFile(mf MediaFile) ArtworkID {
|
||||
return ArtworkID{
|
||||
Kind: KindMediaFileArtwork,
|
||||
ID: mf.ID,
|
||||
Kind: KindMediaFileArtwork,
|
||||
ID: mf.ID,
|
||||
LastUpdate: mf.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func artworkIDFromPlaylist(pls Playlist) ArtworkID {
|
||||
return ArtworkID{
|
||||
Kind: KindPlaylistArtwork,
|
||||
ID: pls.ID,
|
||||
Kind: KindPlaylistArtwork,
|
||||
ID: pls.ID,
|
||||
LastUpdate: pls.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+48
-25
@@ -1,36 +1,59 @@
|
||||
package model_test
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/model"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("ParseArtworkID()", func() {
|
||||
It("parses album artwork ids", func() {
|
||||
id, err := model.ParseArtworkID("al-1234")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(id.Kind).To(Equal(model.KindAlbumArtwork))
|
||||
Expect(id.ID).To(Equal("1234"))
|
||||
var _ = Describe("ArtworkID", func() {
|
||||
Describe("NewArtworkID()", func() {
|
||||
It("creates a valid parseable ArtworkID", func() {
|
||||
now := time.Now()
|
||||
id := model.NewArtworkID(model.KindAlbumArtwork, "1234", &now)
|
||||
parsedId, err := model.ParseArtworkID(id.String())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(parsedId.Kind).To(Equal(id.Kind))
|
||||
Expect(parsedId.ID).To(Equal(id.ID))
|
||||
Expect(parsedId.LastUpdate.Unix()).To(Equal(id.LastUpdate.Unix()))
|
||||
})
|
||||
It("creates a valid ArtworkID without lastUpdate info", func() {
|
||||
id := model.NewArtworkID(model.KindPlaylistArtwork, "1234", nil)
|
||||
parsedId, err := model.ParseArtworkID(id.String())
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(parsedId.Kind).To(Equal(id.Kind))
|
||||
Expect(parsedId.ID).To(Equal(id.ID))
|
||||
Expect(parsedId.LastUpdate.Unix()).To(Equal(id.LastUpdate.Unix()))
|
||||
})
|
||||
})
|
||||
It("parses media file artwork ids", func() {
|
||||
id, err := model.ParseArtworkID("mf-a6f8d2b1")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
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"))
|
||||
})
|
||||
It("fails to parse ids with invalid kind", func() {
|
||||
_, err := model.ParseArtworkID("xx-a6f8d2b1")
|
||||
Expect(err).To(MatchError("invalid artwork kind"))
|
||||
Describe("ParseArtworkID()", func() {
|
||||
It("parses album artwork ids", func() {
|
||||
id, err := model.ParseArtworkID("al-1234")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(id.Kind).To(Equal(model.KindAlbumArtwork))
|
||||
Expect(id.ID).To(Equal("1234"))
|
||||
})
|
||||
It("parses media file artwork ids", func() {
|
||||
id, err := model.ParseArtworkID("mf-a6f8d2b1")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
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"))
|
||||
})
|
||||
It("fails to parse ids with invalid kind", func() {
|
||||
_, err := model.ParseArtworkID("xx-a6f8d2b1")
|
||||
Expect(err).To(MatchError("invalid artwork kind"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user