mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-12 18:27:18 +03:00
fix: relative path was not working for rootFolder started with '.'
This commit is contained in:
parent
694b5d1d39
commit
4d06d250e6
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,3 +18,4 @@ navidrome.db
|
||||
*.swp
|
||||
*_gen.go
|
||||
dist
|
||||
music
|
||||
|
@ -3,7 +3,6 @@ package scanner
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/deluan/navidrome/log"
|
||||
@ -73,8 +72,8 @@ func (s *ChangeDetector) loadDir(dirPath string) (children []string, lastUpdated
|
||||
return
|
||||
}
|
||||
|
||||
func (s *ChangeDetector) loadMap(dirMap dirInfoMap, rootPath string, since time.Time, maybe bool) error {
|
||||
children, lastUpdated, err := s.loadDir(rootPath)
|
||||
func (s *ChangeDetector) loadMap(dirMap dirInfoMap, path string, since time.Time, maybe bool) error {
|
||||
children, lastUpdated, err := s.loadDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -86,14 +85,14 @@ func (s *ChangeDetector) loadMap(dirMap dirInfoMap, rootPath string, since time.
|
||||
}
|
||||
}
|
||||
|
||||
dir := s.getRelativePath(rootPath)
|
||||
dir := s.getRelativePath(path)
|
||||
dirMap[dir] = dirInfo{mdate: lastUpdated, maybe: maybe}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ChangeDetector) getRelativePath(subfolder string) string {
|
||||
dir := strings.TrimPrefix(subfolder, s.rootFolder)
|
||||
func (s *ChangeDetector) getRelativePath(subFolder string) string {
|
||||
dir, _ := filepath.Rel(s.rootFolder, subFolder)
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
@ -111,6 +110,7 @@ func (s *ChangeDetector) checkForUpdates(lastModifiedSince time.Time, newMap dir
|
||||
oldLastUpdated = time.Time{}
|
||||
}
|
||||
}
|
||||
|
||||
if lastUpdated.After(oldLastUpdated) {
|
||||
changed = append(changed, dir)
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ var _ = Describe("ChangeDetector", func() {
|
||||
Expect(changed).To(ConsistOf("."))
|
||||
|
||||
// Add one subfolder
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
err = os.MkdirAll(filepath.Join(testFolder, "a"), 0777)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -41,10 +41,10 @@ var _ = Describe("ChangeDetector", func() {
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(ConsistOf(".", P("/a")))
|
||||
Expect(changed).To(ConsistOf(".", P("a")))
|
||||
|
||||
// Add more subfolders
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
err = os.MkdirAll(filepath.Join(testFolder, "a", "b", "c"), 0777)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -52,17 +52,17 @@ var _ = Describe("ChangeDetector", func() {
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(ConsistOf(P("/a"), P("/a/b"), P("/a/b/c")))
|
||||
Expect(changed).To(ConsistOf(P("a"), P("a/b"), P("a/b/c")))
|
||||
|
||||
// Scan with no changes
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(BeEmpty())
|
||||
|
||||
// New file in subfolder
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
_, err = os.Create(filepath.Join(testFolder, "a", "b", "empty.txt"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -70,10 +70,10 @@ var _ = Describe("ChangeDetector", func() {
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(ConsistOf(P("/a/b")))
|
||||
Expect(changed).To(ConsistOf(P("a/b")))
|
||||
|
||||
// Delete file in subfolder
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
err = os.Remove(filepath.Join(testFolder, "a", "b", "empty.txt"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -81,21 +81,21 @@ var _ = Describe("ChangeDetector", func() {
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(ConsistOf(P("/a/b")))
|
||||
Expect(changed).To(ConsistOf(P("a/b")))
|
||||
|
||||
// Delete subfolder
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
err = os.Remove(filepath.Join(testFolder, "a", "b", "c"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
changed, deleted, err = scanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(ConsistOf(P("/a/b/c")))
|
||||
Expect(changed).To(ConsistOf(P("/a/b")))
|
||||
Expect(deleted).To(ConsistOf(P("a/b/c")))
|
||||
Expect(changed).To(ConsistOf(P("a/b")))
|
||||
|
||||
// Only returns changes after lastModifiedSince
|
||||
lastModifiedSince = time.Now()
|
||||
lastModifiedSince = nowWithDelay()
|
||||
newScanner := NewChangeDetector(testFolder)
|
||||
changed, deleted, err = newScanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
@ -108,6 +108,13 @@ var _ = Describe("ChangeDetector", func() {
|
||||
changed, deleted, err = newScanner.Scan(lastModifiedSince)
|
||||
Expect(err).To(BeNil())
|
||||
Expect(deleted).To(BeEmpty())
|
||||
Expect(changed).To(ConsistOf(P("/a/b")))
|
||||
Expect(changed).To(ConsistOf(P("a/b")))
|
||||
})
|
||||
})
|
||||
|
||||
// I hate time-based tests....
|
||||
func nowWithDelay() time.Time {
|
||||
now := time.Now()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
return now
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user