refactor: obj name mapping and internal path processing (#2733)

* refactor:Prepare to remove the get interface

* feat:add obj Unwarp interface

* refactor:obj name mapping and program internal path processing

* chore: fix typo

* feat: unwrap get

* fix: no use op.Get to get parent id

* fix: set the path uniformly

Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
foxxorcat
2022-12-17 19:49:05 +08:00
committed by GitHub
parent 3d336b328a
commit fb64f00640
50 changed files with 297 additions and 381 deletions

View File

@ -3,33 +3,37 @@ package utils
import (
"net/url"
stdpath "path"
"path/filepath"
"runtime"
"strings"
"github.com/alist-org/alist/v3/internal/errs"
)
// StandardizePath convert path like '/' '/root' '/a/b'
func StandardizePath(path string) string {
path = strings.TrimSuffix(path, "/")
// abs path
if filepath.IsAbs(path) && runtime.GOOS == "windows" {
return path
}
// relative path with prefix '..'
if strings.HasPrefix(path, ".") {
return path
}
// FixAndCleanPath
// The upper layer of the root directory is still the root directory.
// So ".." And "." will be cleared
// for example
// 1. ".." or "." => "/"
// 2. "../..." or "./..." => "/..."
// 3. "../.x." or "./.x." => "/.x."
// 4. "x//\\y" = > "/z/x"
func FixAndCleanPath(path string) string {
path = strings.ReplaceAll(path, "\\", "/")
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return stdpath.Clean(path)
}
// PathAddSeparatorSuffix Add path '/' suffix
// for example /root => /root/
func PathAddSeparatorSuffix(path string) string {
if !strings.HasSuffix(path, "/") {
path = path + "/"
}
return path
}
// PathEqual judge path is equal
func PathEqual(path1, path2 string) bool {
return StandardizePath(path1) == StandardizePath(path2)
return FixAndCleanPath(path1) == FixAndCleanPath(path2)
}
func Ext(path string) string {
@ -64,8 +68,5 @@ func EncodePath(path string, all ...bool) string {
}
func JoinBasePath(basePath, reqPath string) (string, error) {
if strings.HasSuffix(reqPath, "..") || strings.Contains(reqPath, "../") {
return "", errs.RelativePath
}
return stdpath.Join(basePath, reqPath), nil
return stdpath.Join(FixAndCleanPath(basePath), FixAndCleanPath(reqPath)), nil
}