mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* fix(playlist): block track edits on synced playlists across all APIs A synced playlist's tracks come from its source file, so any track edit made through the UI or an API was silently reverted on the next scan. Track mutations funnel through two service guards, checkTracksEditable (incremental edits) and Create (wholesale replace, used by Subsonic createPlaylist and Jellyfin's replace path), which each duplicated the smart-playlist check. Both now consult a shared model.Playlist.TracksEditable() predicate, so the native, Subsonic, and Jellyfin paths are all locked: track edits return ErrNotAuthorized (403, or Subsonic error 50) instead of being accepted and lost. Metadata-only edits (name, comment, public, the sync flag itself) still go through checkWritable and are unaffected. In the UI, a synced playlist's track list becomes read-only, mirroring how smart playlists already behave. * fix(playlist): return 409 Conflict for non-editable playlist track edits The previous commit rejected track edits on smart and synced playlists with ErrNotAuthorized (403). That conflates two different things: a 403 says the caller lacks permission, but a synced or smart playlist's tracks are immutable for everyone, including the owner and admins. It is a property of the resource, not the caller. Introduce ErrPlaylistNotEditable and return it from both track-edit guards. The Native and Jellyfin APIs now map it to 409 Conflict; Subsonic maps it to error 50, the closest code it has (it has no read-only concept). The Native track handlers previously mapped this rejection inconsistently (400 on add, 500 on remove, 403 on reorder) through a new shared writePlaylistError helper. Genuine authorization failures (non-owner, non-admin) still return ErrNotAuthorized. * fix(playlist): surface synced read-only state in picker, Jellyfin, and OpenSubsonic Follow-up to the track-edit lock: the read-only state was enforced but not advertised consistently, so clients still offered edits that the server rejects. - UI: the Add to Playlist picker filtered targets by isWritable only, offering synced playlists that then 409 on add. It now filters with canChangeTracks. - Jellyfin: addToPlaylist/removeFromPlaylist hard-coded every error to 404, so a locked playlist reported "not found" instead of 409. They now return 409 for ErrPlaylistNotEditable while keeping the deliberate anti-probing 404 for every other error (a non-owner never reaches ErrPlaylistNotEditable, so 409 leaks nothing). - OpenSubsonic: buildOSPlaylist marked only smart playlists readonly; owned synced playlists advertised readonly=false. Readonly now also covers !TracksEditable(), matching the existing smart-playlist treatment. * fix(jellyfin): report CanEdit from playlist editability in permission probes getPlaylistUsers and getPlaylistUser returned CanEdit: true unconditionally, so Finamp (which probes this before showing edit controls) offered track editing on synced/smart playlists whose add/remove requests now return 409. Both handlers now fetch the playlist and set CanEdit from TracksEditable(), keeping the deliberate non-owner looseness (CanEdit stays true for a normal playlist a non-owner views) and mapping any lookup error to 404 like the sibling probes. * fix(playlist): check ownership before editability when replacing tracks Create checked TracksEditable() before ownership, so a non-owner replacing another user's public smart/synced playlist (Jellyfin updatePlaylist with a non-empty Ids list) received a 409 read-only conflict instead of a 403 authorization failure. The incremental guards check ownership first via checkWritable; Create now matches that order. Subsonic is unaffected (both errors map to code 50). Owners of their own smart/synced playlists still get the read-only conflict. * fix(jellyfin): return 403 for locked playlists, matching Jellyfin Jellyfin itself refuses edits on its file-backed playlists with Forbid() (403): PlaylistsController gates every mutation on OwnerUserId == caller or a share with CanEdit, and playlists imported from .m3u files satisfy neither. Its CanEdit is an ACL field, not a read-only marker, and Jellyfin core has no server-managed playlist type at all. Our Jellyfin routes exist to imitate that API, so ErrPlaylistNotEditable now maps to 403 there instead of 409. The native API keeps 409 (a resource-state conflict is the accurate REST answer where we define the contract) and Subsonic keeps error 50, its closest code. * chore(playlist): trim comments added by this branch Several comments ran to three or four lines and carried rationale that belongs in the commit history rather than the code: what Jellyfin does with its own file-backed playlists, and restatements of the expressions directly below them. Each block is now one or two lines covering only the non-obvious why.
349 lines
11 KiB
Go
349 lines
11 KiB
Go
package playlists
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/bmatcuk/doublestar/v4"
|
|
"github.com/deluan/rest"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
)
|
|
|
|
type Playlists interface {
|
|
// Reads
|
|
GetAll(ctx context.Context, options ...model.QueryOptions) (model.Playlists, error)
|
|
Get(ctx context.Context, id string) (*model.Playlist, error)
|
|
GetWithTracks(ctx context.Context, id string) (*model.Playlist, error)
|
|
Tracks(ctx context.Context, id string) (model.PlaylistTrackRepository, error)
|
|
GetPlaylists(ctx context.Context, mediaFileId string) (model.Playlists, error)
|
|
|
|
// Mutations
|
|
Create(ctx context.Context, playlistId string, name string, ids []string) (string, error)
|
|
Delete(ctx context.Context, id string) error
|
|
Update(ctx context.Context, playlistID string, name *string, comment *string, public *bool, idsToAdd []string, idxToRemove []int) error
|
|
|
|
// Track management
|
|
AddTracks(ctx context.Context, playlistID string, ids []string) (int, error)
|
|
AddAlbums(ctx context.Context, playlistID string, albumIds []string) (int, error)
|
|
AddArtists(ctx context.Context, playlistID string, artistIds []string) (int, error)
|
|
AddDiscs(ctx context.Context, playlistID string, discs []model.DiscID) (int, error)
|
|
RemoveTracks(ctx context.Context, playlistID string, trackIds []string) error
|
|
ReorderTrack(ctx context.Context, playlistID string, pos int, newPos int) error
|
|
|
|
// Cover art
|
|
SetImage(ctx context.Context, playlistID string, reader io.Reader, ext string) error
|
|
RemoveImage(ctx context.Context, playlistID string) error
|
|
|
|
// Import
|
|
ImportFile(ctx context.Context, absolutePath string, sync bool) (*model.Playlist, error)
|
|
ImportFromFolder(ctx context.Context, folder *model.Folder, filename string) (*model.Playlist, error)
|
|
ImportM3U(ctx context.Context, reader io.Reader) (*model.Playlist, error)
|
|
|
|
// REST adapters
|
|
NewRepository(ctx context.Context) rest.Repository
|
|
TracksRepository(ctx context.Context, playlistId string, refreshSmartPlaylist bool) rest.Repository
|
|
}
|
|
|
|
// ImageUploadService is a local interface satisfied by artwork.Uploader.
|
|
// Defined here to avoid an import cycle between core/artwork and core/playlists.
|
|
type ImageUploadService interface {
|
|
SetImage(ctx context.Context, entityType string, entityID string, name string, oldPath string, reader io.Reader, ext string) (filename string, err error)
|
|
RemoveImage(ctx context.Context, path string) error
|
|
EnqueueArtwork(ctx context.Context, entityType, entityID string)
|
|
}
|
|
|
|
type playlists struct {
|
|
ds model.DataStore
|
|
imgUpload ImageUploadService
|
|
}
|
|
|
|
func NewPlaylists(ds model.DataStore, imgUpload ImageUploadService) Playlists {
|
|
return &playlists{ds: ds, imgUpload: imgUpload}
|
|
}
|
|
|
|
func InPath(folder model.Folder) bool {
|
|
if conf.Server.PlaylistsPath == "" {
|
|
return true
|
|
}
|
|
rel, _ := filepath.Rel(folder.LibraryPath, folder.AbsolutePath())
|
|
for path := range strings.SplitSeq(conf.Server.PlaylistsPath, string(filepath.ListSeparator)) {
|
|
if match, _ := doublestar.Match(path, rel); match {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// --- Read operations ---
|
|
|
|
func (s *playlists) GetAll(ctx context.Context, options ...model.QueryOptions) (model.Playlists, error) {
|
|
return s.ds.Playlist(ctx).GetAll(options...)
|
|
}
|
|
|
|
func (s *playlists) Get(ctx context.Context, id string) (*model.Playlist, error) {
|
|
return s.ds.Playlist(ctx).Get(id)
|
|
}
|
|
|
|
func (s *playlists) GetWithTracks(ctx context.Context, id string) (*model.Playlist, error) {
|
|
return s.ds.Playlist(ctx).GetWithTracks(id, true, false)
|
|
}
|
|
|
|
func (s *playlists) GetPlaylists(ctx context.Context, mediaFileId string) (model.Playlists, error) {
|
|
return s.ds.Playlist(ctx).GetPlaylists(mediaFileId)
|
|
}
|
|
|
|
// Tracks scopes a repository to one playlist's tracks, for callers that page or stream them rather
|
|
// than loading every one like GetWithTracks. Gets first because PlaylistRepository.Tracks discards
|
|
// its error behind a nil (and warns), and this is probed with ids that are usually not playlists.
|
|
func (s *playlists) Tracks(ctx context.Context, id string) (model.PlaylistTrackRepository, error) {
|
|
repo := s.ds.Playlist(ctx)
|
|
if _, err := repo.Get(id); err != nil {
|
|
return nil, err
|
|
}
|
|
tracks := repo.Tracks(id, true)
|
|
if tracks == nil {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
return tracks, nil
|
|
}
|
|
|
|
// --- Mutation operations ---
|
|
|
|
// Create creates a new playlist (when name is provided) or replaces tracks on an existing
|
|
// playlist (when playlistId is provided). This matches the Subsonic createPlaylist semantics.
|
|
func (s *playlists) Create(ctx context.Context, playlistId string, name string, ids []string) (string, error) {
|
|
usr, _ := request.UserFrom(ctx)
|
|
err := s.ds.WithTxImmediate(func(tx model.DataStore) error {
|
|
var pls *model.Playlist
|
|
var err error
|
|
|
|
if playlistId != "" {
|
|
pls, err = tx.Playlist(ctx).Get(playlistId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Ownership first: a non-owner must get ErrNotAuthorized, not a read-only conflict.
|
|
if !usr.IsAdmin && pls.OwnerID != usr.ID {
|
|
return model.ErrNotAuthorized
|
|
}
|
|
if !pls.TracksEditable() {
|
|
return model.ErrPlaylistNotEditable
|
|
}
|
|
} else {
|
|
pls = &model.Playlist{Name: name}
|
|
pls.OwnerID = usr.ID
|
|
}
|
|
pls.Tracks = nil
|
|
pls.AddMediaFilesByID(ids)
|
|
|
|
err = tx.Playlist(ctx).Put(pls)
|
|
playlistId = pls.ID
|
|
return err
|
|
})
|
|
return playlistId, err
|
|
}
|
|
|
|
func (s *playlists) Delete(ctx context.Context, id string) error {
|
|
pls, err := s.checkWritable(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Clean up custom cover image file if one exists
|
|
if path := pls.UploadedImagePath(); path != "" {
|
|
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
|
log.Warn(ctx, "Failed to remove playlist image on delete", "path", path, err)
|
|
}
|
|
}
|
|
|
|
return s.ds.Playlist(ctx).Delete(id)
|
|
}
|
|
|
|
func (s *playlists) Update(ctx context.Context, playlistID string,
|
|
name *string, comment *string, public *bool,
|
|
idsToAdd []string, idxToRemove []int) error {
|
|
var pls *model.Playlist
|
|
var err error
|
|
hasTrackChanges := len(idsToAdd) > 0 || len(idxToRemove) > 0
|
|
if hasTrackChanges {
|
|
pls, err = s.checkTracksEditable(ctx, playlistID)
|
|
} else {
|
|
pls, err = s.checkWritable(ctx, playlistID)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.ds.WithTxImmediate(func(tx model.DataStore) error {
|
|
repo := tx.Playlist(ctx)
|
|
|
|
if len(idxToRemove) > 0 {
|
|
tracksRepo := repo.Tracks(playlistID, false)
|
|
// Convert 0-based indices to 1-based position IDs and delete them directly,
|
|
// avoiding the need to load all tracks into memory.
|
|
positions := make([]string, len(idxToRemove))
|
|
for i, idx := range idxToRemove {
|
|
positions[i] = strconv.Itoa(idx + 1)
|
|
}
|
|
if err := tracksRepo.Delete(positions...); err != nil {
|
|
return err
|
|
}
|
|
if len(idsToAdd) > 0 {
|
|
if _, err := tracksRepo.Add(idsToAdd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.updateMetadata(ctx, tx, pls, name, comment, public)
|
|
}
|
|
|
|
if len(idsToAdd) > 0 {
|
|
if _, err := repo.Tracks(playlistID, false).Add(idsToAdd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if name == nil && comment == nil && public == nil {
|
|
return nil
|
|
}
|
|
// Reuse the playlist from checkWritable (no tracks loaded, so Put only refreshes counters)
|
|
return s.updateMetadata(ctx, tx, pls, name, comment, public)
|
|
})
|
|
}
|
|
|
|
// --- Permission helpers ---
|
|
|
|
// checkWritable fetches the playlist and verifies the current user can modify it.
|
|
func (s *playlists) checkWritable(ctx context.Context, id string) (*model.Playlist, error) {
|
|
pls, err := s.ds.Playlist(ctx).Get(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
usr, _ := request.UserFrom(ctx)
|
|
if !usr.IsAdmin && pls.OwnerID != usr.ID {
|
|
return nil, model.ErrNotAuthorized
|
|
}
|
|
return pls, nil
|
|
}
|
|
|
|
// checkTracksEditable verifies the user owns the playlist and its tracks are editable.
|
|
func (s *playlists) checkTracksEditable(ctx context.Context, playlistID string) (*model.Playlist, error) {
|
|
pls, err := s.checkWritable(ctx, playlistID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !pls.TracksEditable() {
|
|
return nil, model.ErrPlaylistNotEditable
|
|
}
|
|
return pls, nil
|
|
}
|
|
|
|
// updateMetadata applies optional metadata changes to a playlist and persists it.
|
|
// Accepts a DataStore parameter so it can be used inside transactions.
|
|
// The caller is responsible for permission checks.
|
|
func (s *playlists) updateMetadata(ctx context.Context, ds model.DataStore, pls *model.Playlist, name *string, comment *string, public *bool) error {
|
|
if name != nil {
|
|
pls.Name = *name
|
|
}
|
|
if comment != nil {
|
|
pls.Comment = *comment
|
|
}
|
|
if public != nil {
|
|
pls.Public = *public
|
|
}
|
|
return ds.Playlist(ctx).Put(pls)
|
|
}
|
|
|
|
// --- Track management operations ---
|
|
|
|
func (s *playlists) AddTracks(ctx context.Context, playlistID string, ids []string) (int, error) {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return 0, err
|
|
}
|
|
return s.ds.Playlist(ctx).Tracks(playlistID, false).Add(ids)
|
|
}
|
|
|
|
func (s *playlists) AddAlbums(ctx context.Context, playlistID string, albumIds []string) (int, error) {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return 0, err
|
|
}
|
|
return s.ds.Playlist(ctx).Tracks(playlistID, false).AddAlbums(albumIds)
|
|
}
|
|
|
|
func (s *playlists) AddArtists(ctx context.Context, playlistID string, artistIds []string) (int, error) {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return 0, err
|
|
}
|
|
return s.ds.Playlist(ctx).Tracks(playlistID, false).AddArtists(artistIds)
|
|
}
|
|
|
|
func (s *playlists) AddDiscs(ctx context.Context, playlistID string, discs []model.DiscID) (int, error) {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return 0, err
|
|
}
|
|
return s.ds.Playlist(ctx).Tracks(playlistID, false).AddDiscs(discs)
|
|
}
|
|
|
|
func (s *playlists) RemoveTracks(ctx context.Context, playlistID string, trackIds []string) error {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return err
|
|
}
|
|
return s.ds.WithTx(func(tx model.DataStore) error {
|
|
return tx.Playlist(ctx).Tracks(playlistID, false).Delete(trackIds...)
|
|
})
|
|
}
|
|
|
|
func (s *playlists) ReorderTrack(ctx context.Context, playlistID string, pos int, newPos int) error {
|
|
if _, err := s.checkTracksEditable(ctx, playlistID); err != nil {
|
|
return err
|
|
}
|
|
return s.ds.WithTx(func(tx model.DataStore) error {
|
|
return tx.Playlist(ctx).Tracks(playlistID, false).Reorder(pos, newPos)
|
|
})
|
|
}
|
|
|
|
// --- Cover art operations ---
|
|
|
|
func (s *playlists) SetImage(ctx context.Context, playlistID string, reader io.Reader, ext string) error {
|
|
pls, err := s.checkWritable(ctx, playlistID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
oldPath := pls.UploadedImagePath()
|
|
filename, err := s.imgUpload.SetImage(ctx, consts.EntityPlaylist, pls.ID, pls.Name, oldPath, reader, ext)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pls.UploadedImage = filename
|
|
if err := s.ds.Playlist(ctx).Put(pls); err != nil {
|
|
return err
|
|
}
|
|
s.imgUpload.EnqueueArtwork(ctx, consts.EntityPlaylist, pls.ID)
|
|
return nil
|
|
}
|
|
|
|
func (s *playlists) RemoveImage(ctx context.Context, playlistID string) error {
|
|
pls, err := s.checkWritable(ctx, playlistID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.imgUpload.RemoveImage(ctx, pls.UploadedImagePath()); err != nil {
|
|
return err
|
|
}
|
|
|
|
pls.UploadedImage = ""
|
|
if err := s.ds.Playlist(ctx).Put(pls); err != nil {
|
|
return err
|
|
}
|
|
s.imgUpload.EnqueueArtwork(ctx, consts.EntityPlaylist, pls.ID)
|
|
return nil
|
|
}
|