mirror of
https://github.com/navidrome/navidrome.git
synced 2026-04-28 03:19:38 +00:00
Some checks failed
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Lint i18n files (push) Waiting to run
Pipeline: Test, Lint, Build / Check Docker configuration (push) Waiting to run
Pipeline: Test, Lint, Build / Build (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Test JS code (push) Waiting to run
Pipeline: Test, Lint, Build / Get version info (push) Waiting to run
Pipeline: Test, Lint, Build / Lint Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (push) Waiting to run
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Waiting to run
Pipeline: Test, Lint, Build / Build-1 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-2 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-3 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-4 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build Windows installers (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-5 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-6 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-7 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-8 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Package/Release (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-9 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Build-10 (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to GHCR (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Push to Docker Hub (push) Blocked by required conditions
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Blocked by required conditions
POEditor export / push-translations (push) Has been cancelled
* refactor: rename ImportFile to ImportFromFolder in playlists service * feat: add ImportFile method with library/folder resolution * feat: allow sync flag upgrade on re-import of non-synced playlists * feat: add pls export subcommand with bulk and single export Add `navidrome pls export` command that supports: - Single playlist export to stdout (-p flag only) - Single playlist export to directory (-p and -o flags) - Bulk export of all playlists to a directory (-o flag only) - Filtering by user (-u flag) - Automatic filename sanitization and collision detection Also extracts findPlaylist helper from runExporter for reuse. * feat: add pls import subcommand with sync flag support * fix: improve error message for export without output directory * test: add tests for ImportFile sync flag and sync upgrade behavior * refactor: streamline export and import logic by removing redundant comments and improving library path matching Signed-off-by: Deluan <deluan@navidrome.org> * feat: update ImportFile method to include sync flag for playlist imports Signed-off-by: Deluan <deluan@navidrome.org> * feat: implement fetchPlaylists function to streamline playlist retrieval Signed-off-by: Deluan <deluan@navidrome.org> * feat: replace inline filename sanitization with centralized utility function Signed-off-by: Deluan <deluan@navidrome.org> * feat: refactor playlist import logic to consolidate sync handling and improve method signatures Signed-off-by: Deluan <deluan@navidrome.org> * fix: address code review feedback on playlist import/export - Fix duplicate playlist creation on non-sync re-import: only reconcile sync flag when the playlist was actually persisted (has an ID) - Distinguish "not in any library" from real errors in resolveFolder using a sentinel error, so DB/folder errors propagate instead of falling back to ImportM3U - Use bufio.Scanner in countM3UTrackLines instead of reading entire file * feat: replace bufio.Scanner with UTF8Reader and LinesFrom utility for improved file reading Signed-off-by: Deluan <deluan@navidrome.org> * fix: record path for outside-library imports to prevent duplicates Files outside all libraries now go through updatePlaylist with the absolute path recorded, so re-importing the same file updates the existing playlist instead of creating a duplicate. * refactor: name guard condition in updatePlaylist for readability Extracted the compound boolean expression into a named local variable `alreadyImportedAndNotSynced` to make the intent of the early-return guard clearer at a glance. * add godocs Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
165 lines
4.7 KiB
Go
165 lines
4.7 KiB
Go
package scanner
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/core/artwork"
|
|
"github.com/navidrome/navidrome/core/playlists"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
var _ = Describe("phasePlaylists", func() {
|
|
var (
|
|
phase *phasePlaylists
|
|
ctx context.Context
|
|
state *scanState
|
|
folderRepo *mockFolderRepository
|
|
ds *tests.MockDataStore
|
|
pls *mockPlaylists
|
|
cw artwork.CacheWarmer
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
conf.Server.AutoImportPlaylists = true
|
|
ctx = context.Background()
|
|
ctx = request.WithUser(ctx, model.User{ID: "123", IsAdmin: true})
|
|
folderRepo = &mockFolderRepository{}
|
|
ds = &tests.MockDataStore{
|
|
MockedFolder: folderRepo,
|
|
}
|
|
pls = &mockPlaylists{}
|
|
cw = artwork.NoopCacheWarmer()
|
|
state = &scanState{}
|
|
phase = createPhasePlaylists(ctx, state, ds, pls, cw)
|
|
})
|
|
|
|
Describe("description", func() {
|
|
It("returns the correct description", func() {
|
|
Expect(phase.description()).To(Equal("Import/update playlists"))
|
|
})
|
|
})
|
|
|
|
Describe("producer", func() {
|
|
It("produces folders with playlists", func() {
|
|
folderRepo.SetData(map[*model.Folder]error{
|
|
{Path: "/path/to/folder1"}: nil,
|
|
{Path: "/path/to/folder2"}: nil,
|
|
})
|
|
|
|
var produced []*model.Folder
|
|
err := phase.produce(func(folder *model.Folder) {
|
|
produced = append(produced, folder)
|
|
})
|
|
|
|
sort.Slice(produced, func(i, j int) bool {
|
|
return produced[i].Path < produced[j].Path
|
|
})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(produced).To(HaveLen(2))
|
|
Expect(produced[0].Path).To(Equal("/path/to/folder1"))
|
|
Expect(produced[1].Path).To(Equal("/path/to/folder2"))
|
|
})
|
|
|
|
It("returns an error if there is an error loading folders", func() {
|
|
folderRepo.SetData(map[*model.Folder]error{
|
|
nil: errors.New("error loading folders"),
|
|
})
|
|
|
|
called := false
|
|
err := phase.produce(func(folder *model.Folder) { called = true })
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(called).To(BeFalse())
|
|
Expect(err).To(MatchError(ContainSubstring("error loading folders")))
|
|
})
|
|
})
|
|
|
|
Describe("processPlaylistsInFolder", func() {
|
|
It("processes playlists in a folder", func() {
|
|
libPath := GinkgoT().TempDir()
|
|
folder := &model.Folder{LibraryPath: libPath, Path: "path/to", Name: "folder"}
|
|
_ = os.MkdirAll(folder.AbsolutePath(), 0755)
|
|
|
|
file1 := filepath.Join(folder.AbsolutePath(), "playlist1.m3u")
|
|
file2 := filepath.Join(folder.AbsolutePath(), "playlist2.m3u")
|
|
_ = os.WriteFile(file1, []byte{}, 0600)
|
|
_ = os.WriteFile(file2, []byte{}, 0600)
|
|
|
|
pls.On("ImportFromFolder", mock.Anything, folder, "playlist1.m3u").
|
|
Return(&model.Playlist{}, nil)
|
|
pls.On("ImportFromFolder", mock.Anything, folder, "playlist2.m3u").
|
|
Return(&model.Playlist{}, nil)
|
|
|
|
_, err := phase.processPlaylistsInFolder(folder)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(pls.Calls).To(HaveLen(2))
|
|
Expect(pls.Calls[0].Arguments[2]).To(Equal("playlist1.m3u"))
|
|
Expect(pls.Calls[1].Arguments[2]).To(Equal("playlist2.m3u"))
|
|
Expect(phase.refreshed.Load()).To(Equal(uint32(2)))
|
|
})
|
|
|
|
It("reports an error if there is an error reading files", func() {
|
|
tests.SkipOnWindows("relies on Unix /etc filesystem")
|
|
progress := make(chan *ProgressInfo)
|
|
state.progress = progress
|
|
folder := &model.Folder{Path: "/invalid/path"}
|
|
go func() {
|
|
_, err := phase.processPlaylistsInFolder(folder)
|
|
// I/O errors are ignored
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}()
|
|
|
|
// But are reported
|
|
info := &ProgressInfo{}
|
|
Eventually(progress).Should(Receive(&info))
|
|
Expect(info.Warning).To(ContainSubstring("no such file or directory"))
|
|
})
|
|
})
|
|
})
|
|
|
|
type mockPlaylists struct {
|
|
mock.Mock
|
|
playlists.Playlists
|
|
}
|
|
|
|
func (p *mockPlaylists) ImportFromFolder(ctx context.Context, folder *model.Folder, filename string) (*model.Playlist, error) {
|
|
args := p.Called(ctx, folder, filename)
|
|
return args.Get(0).(*model.Playlist), args.Error(1)
|
|
}
|
|
|
|
type mockFolderRepository struct {
|
|
model.FolderRepository
|
|
data map[*model.Folder]error
|
|
}
|
|
|
|
func (f *mockFolderRepository) GetTouchedWithPlaylists() (model.FolderCursor, error) {
|
|
return func(yield func(model.Folder, error) bool) {
|
|
for folder, err := range f.data {
|
|
if err != nil {
|
|
if !yield(model.Folder{}, err) {
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
if !yield(*folder, err) {
|
|
return
|
|
}
|
|
}
|
|
}, nil
|
|
}
|
|
|
|
func (f *mockFolderRepository) SetData(m map[*model.Folder]error) {
|
|
f.data = m
|
|
}
|