diff --git a/server/check.go b/server/check.go index a1024807..399211a0 100644 --- a/server/check.go +++ b/server/check.go @@ -8,7 +8,6 @@ import ( "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "gorm.io/gorm" - "path/filepath" ) func Auth(c *gin.Context) { @@ -52,7 +51,7 @@ func CheckParent(path string, password string) bool { if path == "/" || path == "\\" { return true } - return CheckParent(filepath.Dir(path), password) + return CheckParent(utils.Dir(path), password) } } @@ -75,6 +74,6 @@ func CheckDownLink(path string, passwordMd5 string) bool { if path == "/" || path == "\\" { return true } - return CheckDownLink(filepath.Dir(path), passwordMd5) + return CheckDownLink(utils.Dir(path), passwordMd5) } } diff --git a/server/down.go b/server/down.go index bb27396e..bf46e201 100644 --- a/server/down.go +++ b/server/down.go @@ -18,7 +18,7 @@ func Down(c *gin.Context) { rawPath = utils.ParsePath(rawPath) log.Debugf("down: %s", rawPath) pw := c.Query("pw") - if !CheckDownLink(filepath.Dir(rawPath), pw) { + if !CheckDownLink(utils.Dir(rawPath), pw) { ErrorResp(c, fmt.Errorf("wrong password"), 401) return } @@ -50,7 +50,7 @@ func Proxy(c *gin.Context) { rawPath = utils.ParsePath(rawPath) log.Debugf("proxy: %s", rawPath) pw := c.Query("pw") - if !CheckDownLink(filepath.Dir(rawPath), pw) { + if !CheckDownLink(utils.Dir(rawPath), pw) { ErrorResp(c, fmt.Errorf("wrong password"), 401) return } diff --git a/server/path.go b/server/path.go index 1fb2f295..f68a0ce3 100644 --- a/server/path.go +++ b/server/path.go @@ -7,7 +7,6 @@ import ( "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" - "path/filepath" "strings" ) @@ -32,7 +31,7 @@ func Path(c *gin.Context) { } // TODO hide or ignore? } else if conf.CheckParent { - if !CheckParent(filepath.Dir(req.Path), req.Password) { + if !CheckParent(utils.Dir(req.Path), req.Password) { ErrorResp(c, fmt.Errorf("wrong password"), 401) return } diff --git a/server/webdav/file.go b/server/webdav/file.go index 230b388f..efee9226 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -97,7 +97,7 @@ func GetPW(path string) string { if path == "/" || path == "\\" { return "" } - return GetPW(filepath.Dir(path)) + return GetPW(utils.Dir(path)) } } @@ -119,7 +119,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) { if driver.Config().OnlyProxy || account.WebdavProxy { link = fmt.Sprintf("%s://%s/p%s", protocol, r.Host, rawPath) if conf.CheckDown { - pw := GetPW(filepath.Dir(rawPath)) + pw := GetPW(utils.Dir(rawPath)) link += "?pw" + pw } } else { diff --git a/utils/file.go b/utils/file.go index ef531ff6..170b1974 100644 --- a/utils/file.go +++ b/utils/file.go @@ -94,4 +94,10 @@ func RemoveLastSlash(path string) string { return strings.TrimSuffix(path, "/") } return path +} + +func Dir(path string) string { + path = ParsePath(path) + idx := strings.LastIndex(path, "/") + return path[:idx] } \ No newline at end of file