-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
57 lines (52 loc) · 1.4 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package bilinetdrive
import (
"path/filepath"
"strings"
)
func PathSwitchDir(orginalPath string, relPath string) (string, error) { //绝对路径转相对路径 orginalPath:原有绝对路径
pathList := strings.Split(relPath, "/")
nowPathList := strings.Split(orginalPath, "/")
nowPathList = nowPathList[1:]
for i := 0; i < len(pathList); i++ {
if pathList[i] == "." {
continue
} else if pathList[i] == "" && i == 0 {
nowPathList = []string{nowPathList[0]}
if len(pathList) == 2 {
if pathList[1] == "" {
break
}
}
} else if pathList[i] == ".." {
if len(nowPathList) > 0 && nowPathList[0] != "" {
nowPathList = nowPathList[0 : len(nowPathList)-1]
} else {
return "", PathDoesNotExist()
}
} else {
if nowPathList[0] == "" {
nowPathList[0] = pathList[i]
} else {
nowPathList = append(nowPathList, pathList[i])
}
}
}
return "/" + strings.Join(nowPathList, "/"), nil
}
func JoinPath(first string, second string) string {
path := filepath.Join(first, second)
pathList := strings.Split(path, "\\")
return strings.Join(pathList, "/")
}
func GetPathFileName(path string) string {
pathList := strings.Split(path, "/")
return pathList[len(pathList)-1]
}
func GetPathFolder(path string) string {
pathList := strings.Split(path, "/")
pathList = pathList[0 : len(pathList)-1]
if len(pathList) == 1 {
return "/"
}
return strings.Join(pathList, "/")
}