mirror of
https://github.com/navidrome/navidrome.git
synced 2026-02-23 18:38:34 -05:00
* refactor: move playlist business logic from repositories to core.Playlists service Move authorization, permission checks, and orchestration logic from playlist repositories to the core.Playlists service, following the existing pattern used by core.Share and core.Library. Changes: - Expand core.Playlists interface with read, mutation, track management, and REST adapter methods - Add playlistRepositoryWrapper for REST Save/Update/Delete with permission checks (follows Share/Library pattern) - Simplify persistence/playlist_repository.go: remove isWritable(), auth checks from Delete()/Put()/updatePlaylist() - Simplify persistence/playlist_track_repository.go: remove isTracksEditable() and permission checks from Add/Delete/Reorder - Update Subsonic API handlers to route through service - Update Native API handlers to accept core.Playlists instead of model.DataStore * test: add coverage for playlist service methods and REST wrapper Add 30 new tests covering the service methods added during the playlist refactoring: - Delete: owner, admin, denied, not found - Create: new playlist, replace tracks, admin bypass, denied, not found - AddTracks: owner, admin, denied, smart playlist, not found - RemoveTracks: owner, smart playlist denied, non-owner denied - ReorderTrack: owner, smart playlist denied - NewRepository wrapper: Save (owner assignment, ID clearing), Update (owner, admin, denied, ownership change, not found), Delete (delegation with permission checks) Expand mockedPlaylistRepo with Get, Delete, Tracks, GetWithTracks, and rest.Persistable methods. Add mockedPlaylistTrackRepo for track operation verification. * fix: add authorization check to playlist Update method Added ownership verification to the Subsonic Update endpoint in the playlist service layer. The authorization check was present in the old repository code but was not carried over during the refactoring to the service layer, allowing any authenticated user to modify playlists they don't own via the Subsonic API. Also added corresponding tests for the Update method's permission logic. * refactor: improve playlist permission checks and error handling, add e2e tests Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename core.Playlists to playlists package and update references Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename playlists_internal_test.go to parse_m3u_test.go and update tests; add new parse_nsp.go and rest_adapter.go files Signed-off-by: Deluan <deluan@navidrome.org> * fix: block track mutations on smart playlists in Create and Update Create now rejects replacing tracks on smart playlists (pre-existing gap). Update now uses checkTracksEditable instead of checkWritable when track changes are requested, restoring the protection that was removed from the repository layer during the refactoring. Metadata-only updates on smart playlists remain allowed. * test: add smart playlist protection tests to ensure readonly behavior and mutation restrictions * refactor: optimize track removal and renumbering in playlists Signed-off-by: Deluan <deluan@navidrome.org> * refactor: implement track reordering in playlists with SQL updates Signed-off-by: Deluan <deluan@navidrome.org> * refactor: wrap track deletion and reordering in transactions for consistency Signed-off-by: Deluan <deluan@navidrome.org> * refactor: remove unused getTracks method from playlistTrackRepository Signed-off-by: Deluan <deluan@navidrome.org> * refactor: optimize playlist track renumbering with CTE-based UPDATE Replace the DELETE + re-INSERT renumbering strategy with a two-step UPDATE approach using a materialized CTE and ROW_NUMBER() window function. The previous approach (SELECT all IDs, DELETE all tracks, re-INSERT in chunks of 200) required 13 SQL operations for a 2000-track playlist. The new approach uses just 2 UPDATEs: first negating all IDs to clear the positive space, then assigning sequential positions via UPDATE...FROM with a CTE. This avoids the UNIQUE constraint violations that affected the original correlated subquery while reducing per-delete request time from ~110ms to ~12ms on a 2000-track playlist. Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename New function to NewPlaylists for clarity Signed-off-by: Deluan <deluan@navidrome.org> * refactor: update mock playlist repository and tests for consistency Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
package scanner
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"maps"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/core/playlists"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/utils/chrono"
|
|
)
|
|
|
|
func newFolderEntry(job *scanJob, id, path string, updTime time.Time, hash string) *folderEntry {
|
|
f := &folderEntry{
|
|
id: id,
|
|
job: job,
|
|
path: path,
|
|
audioFiles: make(map[string]fs.DirEntry),
|
|
imageFiles: make(map[string]fs.DirEntry),
|
|
albumIDMap: make(map[string]string),
|
|
updTime: updTime,
|
|
prevHash: hash,
|
|
}
|
|
return f
|
|
}
|
|
|
|
type folderEntry struct {
|
|
job *scanJob
|
|
elapsed chrono.Meter
|
|
path string // Full path
|
|
id string // DB ID
|
|
modTime time.Time // From FS
|
|
updTime time.Time // from DB
|
|
audioFiles map[string]fs.DirEntry
|
|
imageFiles map[string]fs.DirEntry
|
|
numPlaylists int
|
|
numSubFolders int
|
|
imagesUpdatedAt time.Time
|
|
prevHash string // Previous hash from DB
|
|
tracks model.MediaFiles
|
|
albums model.Albums
|
|
albumIDMap map[string]string
|
|
artists model.Artists
|
|
tags model.TagList
|
|
missingTracks []*model.MediaFile
|
|
}
|
|
|
|
func (f *folderEntry) hasNoFiles() bool {
|
|
return len(f.audioFiles) == 0 && len(f.imageFiles) == 0 && f.numPlaylists == 0
|
|
}
|
|
|
|
func (f *folderEntry) isEmpty() bool {
|
|
return f.hasNoFiles() && f.numSubFolders == 0
|
|
}
|
|
|
|
func (f *folderEntry) isNew() bool {
|
|
return f.updTime.IsZero()
|
|
}
|
|
|
|
func (f *folderEntry) isOutdated() bool {
|
|
if f.job.lib.FullScanInProgress && f.updTime.Before(f.job.lib.LastScanStartedAt) {
|
|
return true
|
|
}
|
|
return f.prevHash != f.hash()
|
|
}
|
|
|
|
func (f *folderEntry) toFolder() *model.Folder {
|
|
folder := model.NewFolder(f.job.lib, f.path)
|
|
folder.NumAudioFiles = len(f.audioFiles)
|
|
if playlists.InPath(*folder) {
|
|
folder.NumPlaylists = f.numPlaylists
|
|
}
|
|
folder.ImageFiles = slices.Collect(maps.Keys(f.imageFiles))
|
|
folder.ImagesUpdatedAt = f.imagesUpdatedAt
|
|
folder.Hash = f.hash()
|
|
return folder
|
|
}
|
|
|
|
func (f *folderEntry) hash() string {
|
|
h := md5.New()
|
|
_, _ = fmt.Fprintf(
|
|
h,
|
|
"%s:%d:%d:%s",
|
|
f.modTime.UTC(),
|
|
f.numPlaylists,
|
|
f.numSubFolders,
|
|
f.imagesUpdatedAt.UTC(),
|
|
)
|
|
|
|
// Sort the keys of audio and image files to ensure consistent hashing
|
|
audioKeys := slices.Collect(maps.Keys(f.audioFiles))
|
|
slices.Sort(audioKeys)
|
|
imageKeys := slices.Collect(maps.Keys(f.imageFiles))
|
|
slices.Sort(imageKeys)
|
|
|
|
// Include audio files with their size and modtime
|
|
for _, key := range audioKeys {
|
|
_, _ = io.WriteString(h, key)
|
|
if info, err := f.audioFiles[key].Info(); err == nil {
|
|
_, _ = fmt.Fprintf(h, ":%d:%s", info.Size(), info.ModTime().UTC().String())
|
|
}
|
|
}
|
|
|
|
// Include image files with their size and modtime
|
|
for _, key := range imageKeys {
|
|
_, _ = io.WriteString(h, key)
|
|
if info, err := f.imageFiles[key].Info(); err == nil {
|
|
_, _ = fmt.Fprintf(h, ":%d:%s", info.Size(), info.ModTime().UTC().String())
|
|
}
|
|
}
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|