-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
46 lines (40 loc) · 947 Bytes
/
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
package utils
import (
"github.com/WingGao/errors"
"github.com/thoas/go-funk"
"path/filepath"
"strings"
)
func GetFileBaseName(s string) string {
out := ""
for i := len(s) - 1; i >= 0 && s[i] != '/'; i-- {
if s[i] == '.' {
out = s[:i]
break
}
}
return filepath.Base(out)
}
//ext需要带上'.', eg: '.doc'
func FileRenameExt(s, ext string) string {
for i := len(s) - 1; i >= 0 && s[i] != '/'; i-- {
if s[i] == '.' {
return s[:i] + ext
}
}
return s + ext
}
func ExtIsImage(fp string) bool {
ext := strings.ToLower(filepath.Ext(fp))
return funk.ContainsString(IMAGE_EXT_LIST, ext)
}
// 保证路径拼接的结果在scope里
func JoinInScope(scope string, dirs ...string) (string, error) {
scope, _ = filepath.Abs(scope)
dst := filepath.Join(append([]string{scope}, dirs...)...)
//TODO scope+"/" 这个问题
if strings.HasPrefix(dst, scope) {
return dst, nil
}
return scope, errors.New("out of scope")
}