Add lastUpdated to coverArt ids. Helps with invalidating art cache client-side.

This commit is contained in:
Deluan 2023-02-08 13:49:05 -05:00 committed by Deluan Quintão
parent a3b8682d44
commit 806713719f
6 changed files with 105 additions and 52 deletions

View file

@ -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,
}
}