centralise special path prefix check
This commit is contained in:
@@ -54,3 +54,8 @@ func First(path ...string) (string, error) {
|
|||||||
}
|
}
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasPrefix checks a path has a prefix, making sure to respect path boundaries. So that /aa & /a does not match, but /a/a & /a does.
|
||||||
|
func HasPrefix(p, prefix string) bool {
|
||||||
|
return p == prefix || strings.HasPrefix(p, filepath.Clean(prefix)+string(filepath.Separator))
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/rainycape/unidecode"
|
"github.com/rainycape/unidecode"
|
||||||
|
|
||||||
"go.senan.xyz/gonic/db"
|
"go.senan.xyz/gonic/db"
|
||||||
|
"go.senan.xyz/gonic/fileutil"
|
||||||
"go.senan.xyz/gonic/tags/tagcommon"
|
"go.senan.xyz/gonic/tags/tagcommon"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -721,7 +722,7 @@ func parseMulti(parser tagcommon.Info, setting MultiValueSetting, getMulti func(
|
|||||||
|
|
||||||
func musicDirRelative(musicDirs []string, absPath string) (musicDir, relPath string) {
|
func musicDirRelative(musicDirs []string, absPath string) (musicDir, relPath string) {
|
||||||
for _, musicDir := range musicDirs {
|
for _, musicDir := range musicDirs {
|
||||||
if absPath == musicDir || strings.HasPrefix(absPath, filepath.Clean(musicDir)+string(filepath.Separator)) { // ensure trailing slash for substring check
|
if fileutil.HasPrefix(absPath, musicDir) {
|
||||||
relPath, _ = filepath.Rel(musicDir, absPath)
|
relPath, _ = filepath.Rel(musicDir, absPath)
|
||||||
return musicDir, relPath
|
return musicDir, relPath
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.senan.xyz/gonic/db"
|
"go.senan.xyz/gonic/db"
|
||||||
|
"go.senan.xyz/gonic/fileutil"
|
||||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,7 +55,7 @@ func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (
|
|||||||
|
|
||||||
var musicPath string
|
var musicPath string
|
||||||
for _, mp := range musicPaths {
|
for _, mp := range musicPaths {
|
||||||
if strings.HasPrefix(path, filepath.Clean(mp)+string(filepath.Separator) /* ensure trailing */) {
|
if fileutil.HasPrefix(path, mp) {
|
||||||
musicPath = mp
|
musicPath = mp
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user