mirror of https://github.com/Xhofe/alist
chore: optimize standardize path
parent
a65dcb48b4
commit
ffa0bc294a
|
@ -41,7 +41,7 @@ func List(ctx context.Context, path string) ([]model.Obj, error) {
|
|||
}
|
||||
|
||||
func Get(ctx context.Context, path string) (model.Obj, error) {
|
||||
path = utils.StandardizationPath(path)
|
||||
path = utils.StandardizePath(path)
|
||||
// maybe a virtual file
|
||||
if path != "/" {
|
||||
virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path))
|
||||
|
|
|
@ -32,7 +32,7 @@ func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) {
|
|||
// then instantiate corresponding driver and save it in memory
|
||||
func CreateAccount(ctx context.Context, account model.Account) error {
|
||||
account.Modified = time.Now()
|
||||
account.VirtualPath = utils.StandardizationPath(account.VirtualPath)
|
||||
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
||||
err := store.CreateAccount(&account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed create account in database")
|
||||
|
@ -61,7 +61,7 @@ func UpdateAccount(ctx context.Context, account model.Account) error {
|
|||
return errors.WithMessage(err, "failed get old account")
|
||||
}
|
||||
account.Modified = time.Now()
|
||||
account.VirtualPath = utils.StandardizationPath(account.VirtualPath)
|
||||
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
||||
err = store.UpdateAccount(&account)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed update account in database")
|
||||
|
@ -155,7 +155,7 @@ func GetAccountVirtualFilesByPath(prefix string) []model.Obj {
|
|||
}
|
||||
return accounts[i].GetAccount().Index < accounts[j].GetAccount().Index
|
||||
})
|
||||
prefix = utils.StandardizationPath(prefix)
|
||||
prefix = utils.StandardizePath(prefix)
|
||||
set := make(map[string]interface{})
|
||||
for _, v := range accounts {
|
||||
// TODO should save a balanced account
|
||||
|
@ -189,7 +189,7 @@ var balanceMap generic_sync.MapOf[string, int]
|
|||
|
||||
// GetBalancedAccount get account by path
|
||||
func GetBalancedAccount(path string) driver.Driver {
|
||||
path = utils.StandardizationPath(path)
|
||||
path = utils.StandardizePath(path)
|
||||
accounts := getAccountsByPath(path)
|
||||
accountNum := len(accounts)
|
||||
switch accountNum {
|
||||
|
|
|
@ -15,13 +15,13 @@ func ActualPath(account driver.Additional, rawPath string) string {
|
|||
if i, ok := account.(driver.IRootFolderPath); ok {
|
||||
rawPath = stdpath.Join(i.GetRootFolderPath(), rawPath)
|
||||
}
|
||||
return utils.StandardizationPath(rawPath)
|
||||
return utils.StandardizePath(rawPath)
|
||||
}
|
||||
|
||||
// GetAccountAndActualPath Get the corresponding account
|
||||
// for path: remove the virtual path prefix and join the actual root folder if exists
|
||||
func GetAccountAndActualPath(rawPath string) (driver.Driver, string, error) {
|
||||
rawPath = utils.StandardizationPath(rawPath)
|
||||
rawPath = utils.StandardizePath(rawPath)
|
||||
if strings.Contains(rawPath, "..") {
|
||||
return nil, "", errors.WithStack(errs.RelativePath)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2))
|
|||
var metaG singleflight.Group[*model.Meta]
|
||||
|
||||
func GetNearestMeta(path string) (*model.Meta, error) {
|
||||
path = utils.StandardizationPath(path)
|
||||
path = utils.StandardizePath(path)
|
||||
meta, err := GetMetaByPath(path)
|
||||
if err == nil {
|
||||
return meta, nil
|
||||
|
|
|
@ -1,10 +1,22 @@
|
|||
package utils
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StandardizationPath convert path like '/' '/root' '/a/b'
|
||||
func StandardizationPath(path string) string {
|
||||
// StandardizePath convert path like '/' '/root' '/a/b'
|
||||
func StandardizePath(path string) string {
|
||||
path = strings.TrimSuffix(path, "/")
|
||||
// windows abs path
|
||||
if filepath.IsAbs(path) && runtime.GOOS == "windows" {
|
||||
return path
|
||||
}
|
||||
// relative path with prefix '..'
|
||||
if strings.HasPrefix(path, "..") {
|
||||
return path
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
|
@ -13,5 +25,5 @@ func StandardizationPath(path string) string {
|
|||
|
||||
// PathEqual judge path is equal
|
||||
func PathEqual(path1, path2 string) bool {
|
||||
return StandardizationPath(path1) == StandardizationPath(path2)
|
||||
return StandardizePath(path1) == StandardizePath(path2)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue