| package utils |
|
|
| import ( |
| "net/url" |
| stdpath "path" |
| "strings" |
|
|
| "github.com/OpenListTeam/OpenList/v4/internal/errs" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func FixAndCleanPath(path string) string { |
| path = strings.ReplaceAll(path, "\\", "/") |
| if !strings.HasPrefix(path, "/") { |
| path = "/" + path |
| } |
| return stdpath.Clean(path) |
| } |
|
|
| |
| |
| func PathAddSeparatorSuffix(path string) string { |
| if !strings.HasSuffix(path, "/") { |
| path = path + "/" |
| } |
| return path |
| } |
|
|
| |
| func PathEqual(path1, path2 string) bool { |
| return FixAndCleanPath(path1) == FixAndCleanPath(path2) |
| } |
|
|
| func IsSubPath(path string, subPath string) bool { |
| path, subPath = FixAndCleanPath(path), FixAndCleanPath(subPath) |
| return path == subPath || strings.HasPrefix(subPath, PathAddSeparatorSuffix(path)) |
| } |
|
|
| func Ext(path string) string { |
| return strings.ToLower(SourceExt(path)) |
| } |
|
|
| func SourceExt(path string) string { |
| ext := stdpath.Ext(path) |
| if len(ext) > 0 && ext[0] == '.' { |
| ext = ext[1:] |
| } |
| return ext |
| } |
|
|
| func EncodePath(path string, all ...bool) string { |
| seg := strings.Split(path, "/") |
| toReplace := []struct { |
| Src string |
| Dst string |
| }{ |
| {Src: "%", Dst: "%25"}, |
| {"%", "%25"}, |
| {"?", "%3F"}, |
| {"#", "%23"}, |
| } |
| for i := range seg { |
| if len(all) > 0 && all[0] { |
| seg[i] = url.PathEscape(seg[i]) |
| } else { |
| for j := range toReplace { |
| seg[i] = strings.ReplaceAll(seg[i], toReplace[j].Src, toReplace[j].Dst) |
| } |
| } |
| } |
| return strings.Join(seg, "/") |
| } |
|
|
| func JoinBasePath(basePath, reqPath string) (string, error) { |
| isRelativePath := strings.Contains(reqPath, "..") |
| reqPath = FixAndCleanPath(reqPath) |
| if isRelativePath && !strings.Contains(reqPath, "..") { |
| return "", errs.RelativePath |
| } |
| return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil |
| } |
|
|
| func GetFullPath(mountPath, path string) string { |
| return stdpath.Join(GetActualMountPath(mountPath), path) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func GetPathHierarchy(path string) []string { |
| if path == "" || path == "/" { |
| return []string{"/"} |
| } |
|
|
| path = FixAndCleanPath(path) |
| if !strings.HasPrefix(path, "/") { |
| path = "/" + path |
| } |
|
|
| hierarchy := []string{"/"} |
|
|
| parts := strings.Split(path, "/") |
| currentPath := "" |
| for _, part := range parts { |
| if part == "" { |
| continue |
| } |
| currentPath += "/" + part |
| hierarchy = append(hierarchy, currentPath) |
| } |
|
|
| return hierarchy |
| } |
|
|