mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* fix(playlist): preserve smart playlist counters on re-import (#5907) * perf(playlist): skip re-importing unchanged NSP files (#5907) * feat(playlist): also store content hash for M3U imports (unused for now) * fix(playlist): return stored record when skipping unchanged NSP import Skipping before copying the stored identity broke the ImportFile(sync=false) contract: callers received an ID-less playlist and the requested Sync change was silently dropped. * refactor(playlist): hash imports once at the caller; protect smart counters in Put Move content hashing out of both parsers into the code that owns the file (parsePlaylist and ImportFile), removing the NSP double-buffer and the duplicated hashing idiom. Put now drops song_count/duration/size for smart playlists (PostMapArgs), disarming the counter-zeroing trap for all callers. * fix(playlist): invalidate imported hash when rules are edited via API Without this, a rules edit through the REST API kept the stored file hash, so every scan skipped the unchanged file and never restored the file-backed rules while sync was on. * test(playlist): verify smart counters survive a re-import, end to end The existing Put test seeds the stored counters with a raw SQL update, so it pins the guard in PostMapArgs but not the pipeline around it. This test drives the counters through a real evaluation instead: it saves a smart playlist, reads it with GetWithTracks to populate song_count/duration/size, then saves the playlist the way the scanner rebuilds it after parsing the .nsp file, with the counters back at zero. Both routes fail without the guard, and the new one covers the exact sequence reported in #5907. Test taken from #5970, which diagnosed the same root cause independently. Co-authored-by: Junker der Provinz <133605895+junkerderprovinz@users.noreply.github.com> * test(playlist): build the service with artwork.NewUploader The artwork pipeline in #5847 replaced core.NewImageUploadService() with artwork.NewUploader(ds) and updated every call site it could see. The five call sites this branch adds were written against the old constructor, so the merge applied cleanly but left the package uncompilable. * fix(db): re-stamp the imported_hash migration after the master merge Master gained three migrations while this branch was open, the newest being 20260816180040. The original 20260808200333 stamp now sorts before them, so any database already upgraded past that point would skip this migration entirely and never get the imported_hash column. Same SQL, current timestamp. * refactor(playlist): hash imported playlists with xxh3 and the id encoding ImportedHash is a change detector, not a security boundary, so it does not need a cryptographic digest. xxh3 is already a direct dependency and is used the same way to fingerprint files in the artwork image store. Encoding the 128-bit digest with id.Encode stores it in the same 22-char base62 form as every other id in the schema, down from 64 hex chars. No migration is needed: the imported_hash column has not shipped in a release, so no database holds a value in the old format. * refactor(playlist): extract the imported-playlist fingerprint helper Both import paths encoded the hash inline, so how a playlist file is fingerprinted lived in two places. A third import path that encoded it differently would silently never match the stored value, turning the unchanged-file skip into a no-op. --------- Co-authored-by: Junker der Provinz <133605895+junkerderprovinz@users.noreply.github.com>
190 lines
5.7 KiB
Go
190 lines
5.7 KiB
Go
package model
|
|
|
|
import (
|
|
"iter"
|
|
"slices"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/model/criteria"
|
|
)
|
|
|
|
type Playlist struct {
|
|
Annotations `structs:"-"`
|
|
ItemImage `structs:"-"`
|
|
|
|
ID string `structs:"id" json:"id"`
|
|
Name string `structs:"name" json:"name"`
|
|
Comment string `structs:"comment" json:"comment"`
|
|
Duration float32 `structs:"duration" json:"duration"`
|
|
Size int64 `structs:"size" json:"size"`
|
|
SongCount int `structs:"song_count" json:"songCount"`
|
|
OwnerName string `structs:"-" json:"ownerName"`
|
|
OwnerID string `structs:"owner_id" json:"ownerId"`
|
|
Public bool `structs:"public" json:"public"`
|
|
Tracks PlaylistTracks `structs:"-" json:"tracks,omitempty"`
|
|
Path string `structs:"path" json:"path"`
|
|
Sync bool `structs:"sync" json:"sync"`
|
|
UploadedImage string `structs:"uploaded_image" json:"uploadedImage"`
|
|
ExternalImageURL string `structs:"external_image_url" json:"externalImageUrl,omitempty"`
|
|
CreatedAt time.Time `structs:"created_at" json:"createdAt"`
|
|
UpdatedAt time.Time `structs:"updated_at" json:"updatedAt"`
|
|
ImportedHash string `structs:"imported_hash" json:"-"`
|
|
|
|
// SmartPlaylist attributes
|
|
Rules *criteria.Criteria `structs:"rules" json:"rules"`
|
|
EvaluatedAt *time.Time `structs:"evaluated_at" json:"evaluatedAt"`
|
|
}
|
|
|
|
func (pls Playlist) IsSmartPlaylist() bool {
|
|
return pls.Rules != nil && pls.Rules.Expression != nil
|
|
}
|
|
|
|
// RefreshDelay returns the playlist's own refresh window when set, falling
|
|
// back to the global SmartPlaylistRefreshDelay.
|
|
func (pls Playlist) RefreshDelay() time.Duration {
|
|
if pls.IsSmartPlaylist() && pls.Rules.RefreshDelay > 0 {
|
|
return pls.Rules.RefreshDelay
|
|
}
|
|
return conf.Server.SmartPlaylistRefreshDelay
|
|
}
|
|
|
|
func (pls Playlist) MediaFiles() MediaFiles {
|
|
if len(pls.Tracks) == 0 {
|
|
return nil
|
|
}
|
|
return pls.Tracks.MediaFiles()
|
|
}
|
|
|
|
func (pls *Playlist) refreshStats() {
|
|
pls.SongCount = len(pls.Tracks)
|
|
pls.Duration = 0
|
|
pls.Size = 0
|
|
for _, t := range pls.Tracks {
|
|
pls.Duration += t.MediaFile.Duration
|
|
pls.Size += t.MediaFile.Size
|
|
}
|
|
}
|
|
|
|
func (pls *Playlist) SetTracks(tracks PlaylistTracks) {
|
|
pls.Tracks = tracks
|
|
pls.refreshStats()
|
|
}
|
|
|
|
func (pls *Playlist) RemoveTracks(idxToRemove []int) {
|
|
var newTracks PlaylistTracks
|
|
for i, t := range pls.Tracks {
|
|
if slices.Contains(idxToRemove, i) {
|
|
continue
|
|
}
|
|
newTracks = append(newTracks, t)
|
|
}
|
|
pls.Tracks = newTracks
|
|
pls.refreshStats()
|
|
}
|
|
|
|
// ToM3U8 exports the playlist to the Extended M3U8 format
|
|
func (pls *Playlist) ToM3U8() string {
|
|
return pls.MediaFiles().ToM3U8(pls.Name, true)
|
|
}
|
|
|
|
func (pls *Playlist) AddMediaFilesByID(mediaFileIds []string) {
|
|
pos := len(pls.Tracks)
|
|
for _, mfId := range mediaFileIds {
|
|
pos++
|
|
t := PlaylistTrack{
|
|
ID: strconv.Itoa(pos),
|
|
MediaFileID: mfId,
|
|
MediaFile: MediaFile{ID: mfId},
|
|
PlaylistID: pls.ID,
|
|
}
|
|
pls.Tracks = append(pls.Tracks, t)
|
|
}
|
|
pls.refreshStats()
|
|
}
|
|
|
|
func (pls *Playlist) AddMediaFiles(mfs MediaFiles) {
|
|
pos := len(pls.Tracks)
|
|
for _, mf := range mfs {
|
|
pos++
|
|
t := PlaylistTrack{
|
|
ID: strconv.Itoa(pos),
|
|
MediaFileID: mf.ID,
|
|
MediaFile: mf,
|
|
PlaylistID: pls.ID,
|
|
}
|
|
pls.Tracks = append(pls.Tracks, t)
|
|
}
|
|
pls.refreshStats()
|
|
}
|
|
|
|
func (pls Playlist) CoverArtID() ArtworkID {
|
|
return artworkIDFromPlaylist(pls)
|
|
}
|
|
|
|
// UploadedImagePath returns the absolute filesystem path for a manually uploaded
|
|
// playlist cover image. Returns empty string if no image has been uploaded.
|
|
// This does NOT cover sidecar images or external URLs — those are resolved
|
|
// by the artwork reader's fallback chain.
|
|
func (pls Playlist) UploadedImagePath() string {
|
|
return UploadedImagePath(consts.EntityPlaylist, pls.UploadedImage)
|
|
}
|
|
|
|
type Playlists []Playlist
|
|
|
|
type PlaylistCursor iter.Seq2[Playlist, error]
|
|
|
|
type PlaylistRepository interface {
|
|
ResourceRepository
|
|
AnnotatedRepository
|
|
CountAll(options ...QueryOptions) (int64, error)
|
|
Exists(id string) (bool, error)
|
|
Put(pls *Playlist, cols ...string) error
|
|
Get(id string) (*Playlist, error)
|
|
GetWithTracks(id string, refreshSmartPlaylist, includeMissing bool) (*Playlist, error)
|
|
GetAll(options ...QueryOptions) (Playlists, error)
|
|
GetAllIDs(options ...QueryOptions) ([]string, error)
|
|
GetCursor(options ...QueryOptions) (PlaylistCursor, error)
|
|
FindByPath(path string) (*Playlist, error)
|
|
Delete(id string) error
|
|
Tracks(playlistId string, refreshSmartPlaylist bool) PlaylistTrackRepository
|
|
GetPlaylists(mediaFileId string) (Playlists, error)
|
|
}
|
|
|
|
type PlaylistTrack struct {
|
|
ID string `json:"id"`
|
|
MediaFileID string `json:"mediaFileId"`
|
|
PlaylistID string `json:"playlistId"`
|
|
MediaFile
|
|
}
|
|
|
|
type PlaylistTracks []PlaylistTrack
|
|
|
|
func (plt PlaylistTracks) MediaFiles() MediaFiles {
|
|
mfs := make(MediaFiles, len(plt))
|
|
for i, t := range plt {
|
|
mfs[i] = t.MediaFile
|
|
}
|
|
return mfs
|
|
}
|
|
|
|
type PlaylistTrackCursor iter.Seq2[PlaylistTrack, error]
|
|
|
|
type PlaylistTrackRepository interface {
|
|
ResourceRepository
|
|
CountAll(options ...QueryOptions) (int64, error)
|
|
GetAll(options ...QueryOptions) (PlaylistTracks, error)
|
|
GetCursor(options ...QueryOptions) (PlaylistTrackCursor, error)
|
|
GetAlbumIDs(options ...QueryOptions) ([]string, error)
|
|
GetMediaFileIDs(options ...QueryOptions) ([]string, error)
|
|
Add(mediaFileIds []string) (int, error)
|
|
AddAlbums(albumIds []string) (int, error)
|
|
AddArtists(artistIds []string) (int, error)
|
|
AddDiscs(discs []DiscID) (int, error)
|
|
Delete(id ...string) error
|
|
DeleteAll() error
|
|
Reorder(pos int, newPos int) error
|
|
}
|