Make "ByPath" queries case-sensitive

This commit is contained in:
Deluan 2020-07-14 15:27:27 -04:00
parent 1ef0869a54
commit 8b20c26e04
2 changed files with 19 additions and 7 deletions

View File

@ -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

View File

@ -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,