alist/pkg/utils/path.go

29 lines
554 B
Go
Raw Normal View History

2022-06-09 15:05:27 +00:00
package utils
2022-06-23 09:04:37 +00:00
import (
"path/filepath"
"strings"
)
2022-06-09 15:05:27 +00:00
2022-06-23 09:04:37 +00:00
// StandardizePath convert path like '/' '/root' '/a/b'
func StandardizePath(path string) string {
2022-06-09 15:05:27 +00:00
path = strings.TrimSuffix(path, "/")
2022-06-23 09:04:37 +00:00
// windows abs path
2022-06-27 12:37:05 +00:00
if filepath.IsAbs(path) {
2022-06-23 09:04:37 +00:00
return path
}
// relative path with prefix '..'
2022-06-27 12:37:05 +00:00
if strings.HasPrefix(path, ".") {
2022-06-23 09:04:37 +00:00
return path
}
2022-06-09 15:05:27 +00:00
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return path
}
2022-06-11 06:43:03 +00:00
2022-06-13 06:53:44 +00:00
// PathEqual judge path is equal
2022-06-11 06:43:03 +00:00
func PathEqual(path1, path2 string) bool {
2022-06-23 09:04:37 +00:00
return StandardizePath(path1) == StandardizePath(path2)
2022-06-11 06:43:03 +00:00
}