mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-13 13:52:31 +03:00
fix(server): import playlists with absolute paths
Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
parent
5fa19f9cfa
commit
0954928b14
@ -188,20 +188,14 @@ func (s *playlists) parseM3U(ctx context.Context, pls *model.Playlist, folder *m
|
|||||||
if !model.IsAudioFile(line) {
|
if !model.IsAudioFile(line) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
line = filepath.Clean(line)
|
|
||||||
if folder != nil && !filepath.IsAbs(line) {
|
|
||||||
line = filepath.Join(folder.AbsolutePath(), line)
|
|
||||||
var err error
|
|
||||||
line, err = filepath.Rel(folder.LibraryPath, line)
|
|
||||||
if err != nil {
|
|
||||||
log.Trace(ctx, "Error getting relative path", "playlist", pls.Name, "path", line, "folder", folder, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
filteredLines = append(filteredLines, line)
|
filteredLines = append(filteredLines, line)
|
||||||
}
|
}
|
||||||
filteredLines = slice.Map(filteredLines, filepath.ToSlash)
|
paths, err := s.normalizePaths(ctx, pls, folder, filteredLines)
|
||||||
found, err := mediaFileRepository.FindByPaths(filteredLines)
|
if err != nil {
|
||||||
|
log.Warn(ctx, "Error normalizing paths in playlist", "playlist", pls.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found, err := mediaFileRepository.FindByPaths(paths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(ctx, "Error reading files from DB", "playlist", pls.Name, err)
|
log.Warn(ctx, "Error reading files from DB", "playlist", pls.Name, err)
|
||||||
continue
|
continue
|
||||||
@ -210,7 +204,7 @@ func (s *playlists) parseM3U(ctx context.Context, pls *model.Playlist, folder *m
|
|||||||
for idx := range found {
|
for idx := range found {
|
||||||
existing[strings.ToLower(found[idx].Path)] = idx
|
existing[strings.ToLower(found[idx].Path)] = idx
|
||||||
}
|
}
|
||||||
for _, path := range filteredLines {
|
for _, path := range paths {
|
||||||
idx, ok := existing[strings.ToLower(path)]
|
idx, ok := existing[strings.ToLower(path)]
|
||||||
if ok {
|
if ok {
|
||||||
mfs = append(mfs, found[idx])
|
mfs = append(mfs, found[idx])
|
||||||
@ -228,6 +222,44 @@ func (s *playlists) parseM3U(ctx context.Context, pls *model.Playlist, folder *m
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO This won't work for multiple libraries
|
||||||
|
func (s *playlists) normalizePaths(ctx context.Context, pls *model.Playlist, folder *model.Folder, lines []string) ([]string, error) {
|
||||||
|
libs, err := s.ds.Library(ctx).GetAll()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Normalize paths to be relative to the library root. Search for roots in any library
|
||||||
|
var res []string
|
||||||
|
for idx, line := range lines {
|
||||||
|
var libPath string
|
||||||
|
var filePath string
|
||||||
|
if folder != nil && !filepath.IsAbs(line) {
|
||||||
|
libPath = folder.LibraryPath
|
||||||
|
filePath = filepath.Join(folder.AbsolutePath(), line)
|
||||||
|
} else {
|
||||||
|
for _, lib := range libs {
|
||||||
|
if strings.HasPrefix(line, lib.Path) {
|
||||||
|
libPath = lib.Path
|
||||||
|
filePath = line
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if libPath != "" {
|
||||||
|
var err error
|
||||||
|
filePath, err = filepath.Rel(libPath, filePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Trace(ctx, "Error getting relative path", "playlist", pls.Name, "path", line, "folder", folder, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
res = append(res, filePath)
|
||||||
|
} else {
|
||||||
|
log.Warn(ctx, "Path in playlist not found in any library", "path", line, "line", idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slice.Map(res, filepath.ToSlash), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *playlists) updatePlaylist(ctx context.Context, newPls *model.Playlist) error {
|
func (s *playlists) updatePlaylist(ctx context.Context, newPls *model.Playlist) error {
|
||||||
owner, _ := request.UserFrom(ctx)
|
owner, _ := request.UserFrom(ctx)
|
||||||
|
|
||||||
|
@ -20,15 +20,20 @@ import (
|
|||||||
var _ = Describe("Playlists", func() {
|
var _ = Describe("Playlists", func() {
|
||||||
var ds *tests.MockDataStore
|
var ds *tests.MockDataStore
|
||||||
var ps Playlists
|
var ps Playlists
|
||||||
var mp mockedPlaylist
|
var mockPlsRepo mockedPlaylistRepo
|
||||||
|
var mockLibRepo *tests.MockLibraryRepo
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
mp = mockedPlaylist{}
|
mockPlsRepo = mockedPlaylistRepo{}
|
||||||
|
mockLibRepo = &tests.MockLibraryRepo{}
|
||||||
ds = &tests.MockDataStore{
|
ds = &tests.MockDataStore{
|
||||||
MockedPlaylist: &mp,
|
MockedPlaylist: &mockPlsRepo,
|
||||||
|
MockedLibrary: mockLibRepo,
|
||||||
}
|
}
|
||||||
ctx = request.WithUser(ctx, model.User{ID: "123"})
|
ctx = request.WithUser(ctx, model.User{ID: "123"})
|
||||||
|
// Path should be libPath, but we want to match the root folder referenced in the m3u, which is `/`
|
||||||
|
mockLibRepo.SetData([]model.Library{{ID: 1, Path: "/"}})
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("ImportFile", func() {
|
Describe("ImportFile", func() {
|
||||||
@ -48,15 +53,14 @@ var _ = Describe("Playlists", func() {
|
|||||||
|
|
||||||
Describe("M3U", func() {
|
Describe("M3U", func() {
|
||||||
It("parses well-formed playlists", func() {
|
It("parses well-formed playlists", func() {
|
||||||
// get absolute path for "tests/fixtures" folder
|
|
||||||
pls, err := ps.ImportFile(ctx, folder, "pls1.m3u")
|
pls, err := ps.ImportFile(ctx, folder, "pls1.m3u")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(pls.OwnerID).To(Equal("123"))
|
Expect(pls.OwnerID).To(Equal("123"))
|
||||||
Expect(pls.Tracks).To(HaveLen(3))
|
Expect(pls.Tracks).To(HaveLen(3))
|
||||||
Expect(pls.Tracks[0].Path).To(Equal("tests/fixtures/playlists/test.mp3"))
|
Expect(pls.Tracks[0].Path).To(Equal("tests/fixtures/playlists/test.mp3"))
|
||||||
Expect(pls.Tracks[1].Path).To(Equal("tests/fixtures/playlists/test.ogg"))
|
Expect(pls.Tracks[1].Path).To(Equal("tests/fixtures/playlists/test.ogg"))
|
||||||
Expect(pls.Tracks[2].Path).To(Equal("/tests/fixtures/01 Invisible (RED) Edit Version.mp3"))
|
Expect(pls.Tracks[2].Path).To(Equal("tests/fixtures/01 Invisible (RED) Edit Version.mp3"))
|
||||||
Expect(mp.last).To(Equal(pls))
|
Expect(mockPlsRepo.last).To(Equal(pls))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("parses playlists using LF ending", func() {
|
It("parses playlists using LF ending", func() {
|
||||||
@ -76,7 +80,7 @@ var _ = Describe("Playlists", func() {
|
|||||||
It("parses well-formed playlists", func() {
|
It("parses well-formed playlists", func() {
|
||||||
pls, err := ps.ImportFile(ctx, folder, "recently_played.nsp")
|
pls, err := ps.ImportFile(ctx, folder, "recently_played.nsp")
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(mp.last).To(Equal(pls))
|
Expect(mockPlsRepo.last).To(Equal(pls))
|
||||||
Expect(pls.OwnerID).To(Equal("123"))
|
Expect(pls.OwnerID).To(Equal("123"))
|
||||||
Expect(pls.Name).To(Equal("Recently Played"))
|
Expect(pls.Name).To(Equal("Recently Played"))
|
||||||
Expect(pls.Comment).To(Equal("Recently played tracks"))
|
Expect(pls.Comment).To(Equal("Recently played tracks"))
|
||||||
@ -98,79 +102,87 @@ var _ = Describe("Playlists", func() {
|
|||||||
repo = &mockedMediaFileFromListRepo{}
|
repo = &mockedMediaFileFromListRepo{}
|
||||||
ds.MockedMediaFile = repo
|
ds.MockedMediaFile = repo
|
||||||
ps = NewPlaylists(ds)
|
ps = NewPlaylists(ds)
|
||||||
|
mockLibRepo.SetData([]model.Library{{ID: 1, Path: "/music"}})
|
||||||
ctx = request.WithUser(ctx, model.User{ID: "123"})
|
ctx = request.WithUser(ctx, model.User{ID: "123"})
|
||||||
})
|
})
|
||||||
|
|
||||||
It("parses well-formed playlists", func() {
|
It("parses well-formed playlists", func() {
|
||||||
repo.data = []string{
|
repo.data = []string{
|
||||||
"tests/fixtures/test.mp3",
|
"tests/test.mp3",
|
||||||
"tests/fixtures/test.ogg",
|
"tests/test.ogg",
|
||||||
"/tests/fixtures/01 Invisible (RED) Edit Version.mp3",
|
"tests/01 Invisible (RED) Edit Version.mp3",
|
||||||
}
|
}
|
||||||
f, _ := os.Open("tests/fixtures/playlists/pls-with-name.m3u")
|
m3u := strings.Join([]string{
|
||||||
defer f.Close()
|
"#PLAYLIST:playlist 1",
|
||||||
|
"/music/tests/test.mp3",
|
||||||
|
"/music/tests/test.ogg",
|
||||||
|
"file:///music/tests/01%20Invisible%20(RED)%20Edit%20Version.mp3",
|
||||||
|
}, "\n")
|
||||||
|
f := strings.NewReader(m3u)
|
||||||
|
|
||||||
pls, err := ps.ImportM3U(ctx, f)
|
pls, err := ps.ImportM3U(ctx, f)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(pls.OwnerID).To(Equal("123"))
|
Expect(pls.OwnerID).To(Equal("123"))
|
||||||
Expect(pls.Name).To(Equal("playlist 1"))
|
Expect(pls.Name).To(Equal("playlist 1"))
|
||||||
Expect(pls.Sync).To(BeFalse())
|
Expect(pls.Sync).To(BeFalse())
|
||||||
Expect(pls.Tracks).To(HaveLen(3))
|
Expect(pls.Tracks).To(HaveLen(3))
|
||||||
Expect(pls.Tracks[0].Path).To(Equal("tests/fixtures/test.mp3"))
|
Expect(pls.Tracks[0].Path).To(Equal("tests/test.mp3"))
|
||||||
Expect(pls.Tracks[1].Path).To(Equal("tests/fixtures/test.ogg"))
|
Expect(pls.Tracks[1].Path).To(Equal("tests/test.ogg"))
|
||||||
Expect(pls.Tracks[2].Path).To(Equal("/tests/fixtures/01 Invisible (RED) Edit Version.mp3"))
|
Expect(pls.Tracks[2].Path).To(Equal("tests/01 Invisible (RED) Edit Version.mp3"))
|
||||||
Expect(mp.last).To(Equal(pls))
|
Expect(mockPlsRepo.last).To(Equal(pls))
|
||||||
f.Close()
|
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("sets the playlist name as a timestamp if the #PLAYLIST directive is not present", func() {
|
It("sets the playlist name as a timestamp if the #PLAYLIST directive is not present", func() {
|
||||||
repo.data = []string{
|
repo.data = []string{
|
||||||
"tests/fixtures/test.mp3",
|
"tests/test.mp3",
|
||||||
"tests/fixtures/test.ogg",
|
"tests/test.ogg",
|
||||||
"/tests/fixtures/01 Invisible (RED) Edit Version.mp3",
|
"/tests/01 Invisible (RED) Edit Version.mp3",
|
||||||
}
|
}
|
||||||
f, _ := os.Open("tests/fixtures/playlists/pls-without-name.m3u")
|
m3u := strings.Join([]string{
|
||||||
defer f.Close()
|
"/music/tests/test.mp3",
|
||||||
|
"/music/tests/test.ogg",
|
||||||
|
}, "\n")
|
||||||
|
f := strings.NewReader(m3u)
|
||||||
pls, err := ps.ImportM3U(ctx, f)
|
pls, err := ps.ImportM3U(ctx, f)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
_, err = time.Parse(time.RFC3339, pls.Name)
|
_, err = time.Parse(time.RFC3339, pls.Name)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(pls.Tracks).To(HaveLen(3))
|
Expect(pls.Tracks).To(HaveLen(2))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns only tracks that exist in the database and in the same other as the m3u", func() {
|
It("returns only tracks that exist in the database and in the same other as the m3u", func() {
|
||||||
repo.data = []string{
|
repo.data = []string{
|
||||||
"test1.mp3",
|
"album1/test1.mp3",
|
||||||
"test2.mp3",
|
"album2/test2.mp3",
|
||||||
"test3.mp3",
|
"album3/test3.mp3",
|
||||||
}
|
}
|
||||||
m3u := strings.Join([]string{
|
m3u := strings.Join([]string{
|
||||||
"test3.mp3",
|
"/music/album3/test3.mp3",
|
||||||
"test1.mp3",
|
"/music/album1/test1.mp3",
|
||||||
"test4.mp3",
|
"/music/album4/test4.mp3",
|
||||||
"test2.mp3",
|
"/music/album2/test2.mp3",
|
||||||
}, "\n")
|
}, "\n")
|
||||||
f := strings.NewReader(m3u)
|
f := strings.NewReader(m3u)
|
||||||
pls, err := ps.ImportM3U(ctx, f)
|
pls, err := ps.ImportM3U(ctx, f)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(pls.Tracks).To(HaveLen(3))
|
Expect(pls.Tracks).To(HaveLen(3))
|
||||||
Expect(pls.Tracks[0].Path).To(Equal("test3.mp3"))
|
Expect(pls.Tracks[0].Path).To(Equal("album3/test3.mp3"))
|
||||||
Expect(pls.Tracks[1].Path).To(Equal("test1.mp3"))
|
Expect(pls.Tracks[1].Path).To(Equal("album1/test1.mp3"))
|
||||||
Expect(pls.Tracks[2].Path).To(Equal("test2.mp3"))
|
Expect(pls.Tracks[2].Path).To(Equal("album2/test2.mp3"))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("is case-insensitive when comparing paths", func() {
|
It("is case-insensitive when comparing paths", func() {
|
||||||
repo.data = []string{
|
repo.data = []string{
|
||||||
"tEsT1.Mp3",
|
"abc/tEsT1.Mp3",
|
||||||
}
|
}
|
||||||
m3u := strings.Join([]string{
|
m3u := strings.Join([]string{
|
||||||
"TeSt1.mP3",
|
"/music/ABC/TeSt1.mP3",
|
||||||
}, "\n")
|
}, "\n")
|
||||||
f := strings.NewReader(m3u)
|
f := strings.NewReader(m3u)
|
||||||
pls, err := ps.ImportM3U(ctx, f)
|
pls, err := ps.ImportM3U(ctx, f)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(pls.Tracks).To(HaveLen(1))
|
Expect(pls.Tracks).To(HaveLen(1))
|
||||||
Expect(pls.Tracks[0].Path).To(Equal("tEsT1.Mp3"))
|
Expect(pls.Tracks[0].Path).To(Equal("abc/tEsT1.Mp3"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -254,16 +266,16 @@ func (r *mockedMediaFileFromListRepo) FindByPaths([]string) (model.MediaFiles, e
|
|||||||
return mfs, nil
|
return mfs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockedPlaylist struct {
|
type mockedPlaylistRepo struct {
|
||||||
last *model.Playlist
|
last *model.Playlist
|
||||||
model.PlaylistRepository
|
model.PlaylistRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *mockedPlaylist) FindByPath(string) (*model.Playlist, error) {
|
func (r *mockedPlaylistRepo) FindByPath(string) (*model.Playlist, error) {
|
||||||
return nil, model.ErrNotFound
|
return nil, model.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *mockedPlaylist) Put(pls *model.Playlist) error {
|
func (r *mockedPlaylistRepo) Put(pls *model.Playlist) error {
|
||||||
r.last = pls
|
r.last = pls
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
6
tests/fixtures/playlists/subfolder2/pls2.m3u
vendored
6
tests/fixtures/playlists/subfolder2/pls2.m3u
vendored
@ -1,2 +1,4 @@
|
|||||||
test.mp3
|
../test.mp3
|
||||||
test.ogg
|
../test.ogg
|
||||||
|
/tests/fixtures/01%20Invisible%20(RED)%20Edit%20Version.mp3
|
||||||
|
/invalid/path/xyz.mp3
|
Loading…
x
Reference in New Issue
Block a user