Cloudreve/pkg/util/path.go

59 lines
1.1 KiB
Go
Raw Normal View History

2019-11-18 19:32:06 +08:00
package util
2020-02-03 13:23:33 +08:00
import (
2020-03-11 10:32:35 +08:00
"os"
2020-02-03 13:23:33 +08:00
"path"
2020-03-11 10:32:35 +08:00
"path/filepath"
2020-02-03 13:23:33 +08:00
"strings"
)
2019-11-18 19:32:06 +08:00
// DotPathToStandardPath 将","分割的路径转换为标准路径
func DotPathToStandardPath(path string) string {
return "/" + strings.Replace(path, ",", "/", -1)
}
2019-12-01 18:31:29 +08:00
// FillSlash 给路径补全`/`
func FillSlash(path string) string {
if path == "/" {
return path
}
return path + "/"
}
2019-12-20 11:59:09 +08:00
// RemoveSlash 移除路径最后的`/`
func RemoveSlash(path string) string {
if len(path) > 1 {
return strings.TrimSuffix(path, "/")
}
return path
}
// SplitPath 分割路径为列表
func SplitPath(path string) []string {
if len(path) == 0 || path[0] != '/' {
return []string{}
}
if path == "/" {
return []string{"/"}
}
2019-12-01 18:31:29 +08:00
pathSplit := strings.Split(path, "/")
pathSplit[0] = "/"
return pathSplit
2019-12-01 18:31:29 +08:00
}
2020-02-03 13:23:33 +08:00
// FormSlash 将path中的反斜杠'\'替换为'/'
func FormSlash(old string) string {
return path.Clean(strings.ReplaceAll(old, "\\", "/"))
}
2020-03-11 10:32:35 +08:00
// RelativePath 获取相对可执行文件的路径
func RelativePath(name string) string {
if filepath.IsAbs(name) {
return name
}
e, _ := os.Executable()
return filepath.Join(filepath.Dir(e), name)
}