-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
86 lines (74 loc) · 2.75 KB
/
path.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package path
import (
stringsi "github.com/hopeio/cherry/utils/strings"
sdpath "path"
"strings"
)
// windows需要,由于linux的文件也要放到windows看,统一处理
func FileRewrite(filename string) string {
filename = stringsi.ReplaceRunesEmpty(filename, '/', '\\', '*', '|')
filename = strings.ReplaceAll(filename, "<", "《")
filename = strings.ReplaceAll(filename, ">", "》")
filename = strings.ReplaceAll(filename, "?", "?")
filename = strings.ReplaceAll(filename, ":", ":")
return filename
}
// 仅仅针对文件名,Removed unsupported characters
func FileCleanse(filename string) string {
filename = strings.Trim(filename, ".")
filename = strings.TrimPrefix(filename, "-")
filename = strings.TrimPrefix(filename, "+")
// windows
//filename = stringsi.ReplaceRunesEmpty(filename, '/', '\\', ':', '*', '?', '"', '<', '>', '|')
// linux
//filename = stringsi.ReplaceRunesEmpty(filename, '\'', '*','?', '@', '#', '$', '&', '(', ')', '|', ';', '/', '%', '^', ' ', '\t', '\n')
filename = stringsi.ReplaceRunesEmpty(filename, '/', '\\', ':', '*', '?', '"', '<', '>', '|', ';', '/', '%', '^', ' ', '\t', '\n', '$', '&')
// 中文符号
//filename = stringsi.ReplaceRunesEmpty(filename, ':', ',', '。', '!', '?', '、', '“', '”', '、')
return filename
}
// 仅仅针对目录名,Removed unsupported characters
func DirCleanse(dir string) string { // will be used when save the dir or the part
// remove special symbol
// :unix允许存在,windows需要
// windows path
if len(dir) > 2 && dir[1] == ':' && ((dir[0] >= 'A' && dir[0] <= 'Z') || (dir[0] >= 'a' && dir[0] <= 'z')) && (dir[2] == '/' || dir[2] == '\\') {
return dir[:3] + stringsi.ReplaceRunesEmpty(dir[3:], ':', '*', '?', '"', '<', '>', '|', ',', ' ', '\t', '\n')
}
return stringsi.ReplaceRunesEmpty(dir, ':', '*', '?', '"', '<', '>', '|', ',', ' ', '\t', '\n')
}
// 针对带目录的完整文件名,Removed unsupported characters
func Cleanse(path string) string { // will be used when save the dir or the part
dir, file := Split(path)
if dir == "" {
return DirCleanse(dir)
}
if file == "" {
return FileCleanse(file)
}
// remove special symbol
return DirCleanse(dir) + string(path[len(dir)-1-len(file)]) + FileCleanse(file)
}
func CleanDir(path string) string {
dir, _ := Split(path)
return sdpath.Clean(dir)
}
// 获取url的文件部分
func Base(path string) string {
_, file := Split(path)
return file
}
// 返回目录名和文件名
func Split(path string) (dir, file string) {
i := lastSlash(path)
return path[:i+1], path[i+1:]
}
// 获取文件名除去扩展名
func FileNoExt(filepath string) string {
base := sdpath.Base(filepath)
return base[:len(base)-len(sdpath.Ext(base))]
}
func Dir(path string) string {
i := lastSlash(path)
return path[:i]
}