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:
@ -3,7 +3,6 @@ package utils
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"os"
|
||||
"path"
|
||||
@ -43,7 +42,7 @@ func CopyFile(src, dst string) error {
|
||||
// CopyDir Dir copies a whole directory recursively
|
||||
func CopyDir(src string, dst string) error {
|
||||
var err error
|
||||
var fds []os.FileInfo
|
||||
var fds []os.DirEntry
|
||||
var srcinfo os.FileInfo
|
||||
|
||||
if srcinfo, err = os.Stat(src); err != nil {
|
||||
@ -52,7 +51,7 @@ func CopyDir(src string, dst string) error {
|
||||
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
if fds, err = ioutil.ReadDir(src); err != nil {
|
||||
if fds, err = os.ReadDir(src); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, fd := range fds {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -5,3 +5,18 @@ import "testing"
|
||||
func TestEncodePath(t *testing.T) {
|
||||
t.Log(EncodePath("http://localhost:5244/d/123#.png"))
|
||||
}
|
||||
|
||||
func TestFixAndCleanPath(t *testing.T) {
|
||||
datas := map[string]string{
|
||||
"": "/",
|
||||
".././": "/",
|
||||
"../../.../": "/...",
|
||||
"x//\\y/": "/x/y",
|
||||
".././.x/.y/.//..x../..y..": "/.x/.y/..x../..y..",
|
||||
}
|
||||
for key, value := range datas {
|
||||
if FixAndCleanPath(key) != value {
|
||||
t.Logf("raw %s fix fail", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user