Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/op/path.go
1560 views
1
package op
2
3
import (
4
"github.com/alist-org/alist/v3/internal/errs"
5
stdpath "path"
6
"strings"
7
8
"github.com/alist-org/alist/v3/internal/driver"
9
"github.com/alist-org/alist/v3/pkg/utils"
10
log "github.com/sirupsen/logrus"
11
)
12
13
// GetStorageAndActualPath Get the corresponding storage and actual path
14
// for path: remove the mount path prefix and join the actual root folder if exists
15
func GetStorageAndActualPath(rawPath string) (storage driver.Driver, actualPath string, err error) {
16
rawPath = utils.FixAndCleanPath(rawPath)
17
storage = GetBalancedStorage(rawPath)
18
if storage == nil {
19
if rawPath == "/" {
20
err = errs.NewErr(errs.StorageNotFound, "please add a storage first")
21
return
22
}
23
err = errs.NewErr(errs.StorageNotFound, "rawPath: %s", rawPath)
24
return
25
}
26
log.Debugln("use storage: ", storage.GetStorage().MountPath)
27
mountPath := utils.GetActualMountPath(storage.GetStorage().MountPath)
28
actualPath = utils.FixAndCleanPath(strings.TrimPrefix(rawPath, mountPath))
29
return
30
}
31
32
// urlTreeSplitLineFormPath 分割path中分割真实路径和UrlTree定义字符串
33
func urlTreeSplitLineFormPath(path string) (pp string, file string) {
34
// url.PathUnescape 会移除 // ,手动加回去
35
path = strings.Replace(path, "https:/", "https://", 1)
36
path = strings.Replace(path, "http:/", "http://", 1)
37
if strings.Contains(path, ":https:/") || strings.Contains(path, ":http:/") {
38
// URL-Tree模式 /url_tree_drivr/file_name[:size[:time]]:https://example.com/file
39
fPath := strings.SplitN(path, ":", 2)[0]
40
pp, _ = stdpath.Split(fPath)
41
file = path[len(pp):]
42
} else if strings.Contains(path, "/https:/") || strings.Contains(path, "/http:/") {
43
// URL-Tree模式 /url_tree_drivr/https://example.com/file
44
index := strings.Index(path, "/http://")
45
if index == -1 {
46
index = strings.Index(path, "/https://")
47
}
48
pp = path[:index]
49
file = path[index+1:]
50
} else {
51
pp, file = stdpath.Split(path)
52
}
53
if pp == "" {
54
pp = "/"
55
}
56
return
57
}
58
59