alist/internal/operations/path.go

40 lines
1.3 KiB
Go
Raw Normal View History

2022-06-10 09:18:27 +00:00
package operations
import (
2022-06-15 12:31:23 +00:00
stdpath "path"
"strings"
2022-06-10 09:18:27 +00:00
"github.com/alist-org/alist/v3/internal/driver"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/errs"
2022-06-10 09:18:27 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// ActualPath Get the actual path
// !!! maybe and \ in the path when use windows local
2022-07-10 06:45:39 +00:00
func ActualPath(storage driver.Additional, rawPath string) string {
if i, ok := storage.(driver.IRootFolderPath); ok {
2022-06-15 12:31:23 +00:00
rawPath = stdpath.Join(i.GetRootFolderPath(), rawPath)
2022-06-10 12:20:45 +00:00
}
2022-06-23 09:04:37 +00:00
return utils.StandardizePath(rawPath)
2022-06-10 12:20:45 +00:00
}
2022-07-10 06:45:39 +00:00
// GetStorageAndActualPath Get the corresponding storage and actual path
2022-06-15 12:31:23 +00:00
// for path: remove the virtual path prefix and join the actual root folder if exists
2022-07-10 06:45:39 +00:00
func GetStorageAndActualPath(rawPath string) (driver.Driver, string, error) {
2022-06-23 09:04:37 +00:00
rawPath = utils.StandardizePath(rawPath)
2022-06-23 08:09:22 +00:00
if strings.Contains(rawPath, "..") {
return nil, "", errors.WithStack(errs.RelativePath)
}
2022-07-10 06:45:39 +00:00
storage := GetBalancedStorage(rawPath)
if storage == nil {
return nil, "", errors.Errorf("can't find storage with rawPath: %s", rawPath)
2022-06-10 09:18:27 +00:00
}
log.Debugln("use storage: ", storage.GetStorage().MountPath)
virtualPath := utils.GetActualVirtualPath(storage.GetStorage().MountPath)
2022-06-10 12:20:45 +00:00
actualPath := strings.TrimPrefix(rawPath, virtualPath)
2022-07-10 06:45:39 +00:00
actualPath = ActualPath(storage.GetAddition(), actualPath)
return storage, actualPath, nil
2022-06-10 09:18:27 +00:00
}