diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go index 8b46fb29d..5b6ff5485 100644 --- a/persistence/mediafile_repository.go +++ b/persistence/mediafile_repository.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path/filepath" - "strings" . "github.com/Masterminds/squirrel" "github.com/astaxie/beego/orm" @@ -95,11 +94,9 @@ func (r mediaFileRepository) FindByPath(path string) (model.MediaFiles, error) { } func pathStartsWith(path string) Sqlizer { - escapeChar := string(os.PathListSeparator) - escapedPath := strings.ReplaceAll(path, escapeChar, escapeChar+escapeChar) - escapedPath = strings.ReplaceAll(escapedPath, "_", escapeChar+"_") - escapedPath = strings.ReplaceAll(escapedPath, "%", escapeChar+"%") - return ConcatExpr(Like{"path": filepath.Join(escapedPath, "%")}, " escape '"+escapeChar+"'") + cleanPath := filepath.Clean(path) + substr := fmt.Sprintf("substr(path, 1, %d)", len(cleanPath)) + return Eq{substr: cleanPath} } // FindPathsRecursively returns a list of all subfolders of basePath, recursively diff --git a/persistence/mediafile_repository_test.go b/persistence/mediafile_repository_test.go index 4a492dc18..c5da4649e 100644 --- a/persistence/mediafile_repository_test.go +++ b/persistence/mediafile_repository_test.go @@ -51,7 +51,7 @@ var _ = Describe("MediaRepository", func() { Expect(mr.FindByAlbum("67")).To(Equal(model.MediaFiles{})) }) - It("finds tracks by path", func() { + It("finds tracks by path when using wildcards chars", func() { Expect(mr.Put(&model.MediaFile{ID: "7001", Path: P("/Find:By'Path/_/123.mp3")})).To(BeNil()) Expect(mr.Put(&model.MediaFile{ID: "7002", Path: P("/Find:By'Path/1/123.mp3")})).To(BeNil()) @@ -61,6 +61,21 @@ var _ = Describe("MediaRepository", func() { Expect(found[0].ID).To(Equal("7001")) }) + It("finds tracks by path case sensitively", func() { + Expect(mr.Put(&model.MediaFile{ID: "7003", Path: P("/Casesensitive/file1.mp3")})).To(BeNil()) + Expect(mr.Put(&model.MediaFile{ID: "7004", Path: P("/casesensitive/file2.mp3")})).To(BeNil()) + + found, err := mr.FindByPath(P("/Casesensitive")) + Expect(err).To(BeNil()) + Expect(found).To(HaveLen(1)) + Expect(found[0].ID).To(Equal("7003")) + + found, err = mr.FindByPath(P("/casesensitive/")) + Expect(err).To(BeNil()) + Expect(found).To(HaveLen(1)) + Expect(found[0].ID).To(Equal("7004")) + }) + It("returns starred tracks", func() { Expect(mr.GetStarred()).To(Equal(model.MediaFiles{ songComeTogether,