Skip to content

Commit

Permalink
centralise special path prefix check
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed Dec 29, 2023
1 parent 552aa3a commit b5de0b3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions fileutil/fileutil.go
Expand Up @@ -54,3 +54,8 @@ func First(path ...string) (string, error) {
}
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))
}
3 changes: 2 additions & 1 deletion scanner/scanner.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/rainycape/unidecode"

"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/fileutil"
"go.senan.xyz/gonic/tags/tagcommon"
)

Expand Down Expand Up @@ -721,7 +722,7 @@ func parseMulti(parser tagcommon.Info, setting MultiValueSetting, getMulti func(

func musicDirRelative(musicDirs []string, absPath string) (musicDir, relPath string) {
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)
return musicDir, relPath
}
Expand Down
3 changes: 2 additions & 1 deletion server/ctrlsubsonic/specidpaths/specidpaths.go
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/fileutil"
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func Lookup(dbc *db.DB, musicPaths []string, podcastsPath string, path string) (

var musicPath string
for _, mp := range musicPaths {
if strings.HasPrefix(path, filepath.Clean(mp)+string(filepath.Separator) /* ensure trailing */) {
if fileutil.HasPrefix(path, mp) {
musicPath = mp
break
}
Expand Down

0 comments on commit b5de0b3

Please sign in to comment.