Files
OpenList/internal/op/path.go
Kuingsmile fdcc2f136e chore: change module name to OpenListTeam/OpenList (#2)
* Enable blank issue

* chore(README.md): update docs (temporally)

* Update FUNDING.yml

* chore: purge README.md

* chore: change module name to OpenListTeam/OpenList

* fix: fix link errors

* chore: remove v3 in module name

* fix: resolve some conficts

* fix: resolve conficts

* docs: update with latest file

---------

Co-authored-by: ShenLin <773933146@qq.com>
Co-authored-by: Hantong Chen <cxwdyx620@gmail.com>
Co-authored-by: joshua <i@joshua.su>
Co-authored-by: Hantong Chen <70561268+cxw620@users.noreply.github.com>
2025-06-12 22:02:46 +08:00

59 lines
1.9 KiB
Go

package op
import (
stdpath "path"
"strings"
"github.com/OpenListTeam/OpenList/internal/errs"
"github.com/OpenListTeam/OpenList/internal/driver"
"github.com/OpenListTeam/OpenList/pkg/utils"
log "github.com/sirupsen/logrus"
)
// GetStorageAndActualPath Get the corresponding storage and actual path
// for path: remove the mount path prefix and join the actual root folder if exists
func GetStorageAndActualPath(rawPath string) (storage driver.Driver, actualPath string, err error) {
rawPath = utils.FixAndCleanPath(rawPath)
storage = GetBalancedStorage(rawPath)
if storage == nil {
if rawPath == "/" {
err = errs.NewErr(errs.StorageNotFound, "please add a storage first")
return
}
err = errs.NewErr(errs.StorageNotFound, "rawPath: %s", rawPath)
return
}
log.Debugln("use storage: ", storage.GetStorage().MountPath)
mountPath := utils.GetActualMountPath(storage.GetStorage().MountPath)
actualPath = utils.FixAndCleanPath(strings.TrimPrefix(rawPath, mountPath))
return
}
// urlTreeSplitLineFormPath 分割path中分割真实路径和UrlTree定义字符串
func urlTreeSplitLineFormPath(path string) (pp string, file string) {
// url.PathUnescape 会移除 // ,手动加回去
path = strings.Replace(path, "https:/", "https://", 1)
path = strings.Replace(path, "http:/", "http://", 1)
if strings.Contains(path, ":https:/") || strings.Contains(path, ":http:/") {
// URL-Tree模式 /url_tree_drivr/file_name[:size[:time]]:https://example.com/file
fPath := strings.SplitN(path, ":", 2)[0]
pp, _ = stdpath.Split(fPath)
file = path[len(pp):]
} else if strings.Contains(path, "/https:/") || strings.Contains(path, "/http:/") {
// URL-Tree模式 /url_tree_drivr/https://example.com/file
index := strings.Index(path, "/http://")
if index == -1 {
index = strings.Index(path, "/https://")
}
pp = path[:index]
file = path[index+1:]
} else {
pp, file = stdpath.Split(path)
}
if pp == "" {
pp = "/"
}
return
}