-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.go
62 lines (51 loc) · 1.09 KB
/
name.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
package gfx
import (
"fmt"
"path/filepath"
"strings"
)
var (
_ = Name
_ = NewName
)
// Name 获得文件名称
func Name(path string, opts ...nameOption) (filename string) {
_options := defaultNameOptions()
for _, opt := range opts {
opt.applyName(_options)
}
switch _options.typ {
case TypeDir:
filename = dir(path)
case TypeFile:
fallthrough
default:
filename = name(path, _options.ext)
}
return
}
// NewName 新文件名,在避免文件名冲突的情况下
func NewName(original string) (new string) {
for {
index := 1
new = filepath.Join(filepath.Dir(original), name(original, fmt.Sprintf(`%d.%s`, index, filepath.Ext(original))))
/*if !existsWithPath(new) {
break
}*/
}
return
}
func name(path string, ext string) (filename string) {
base := filepath.Base(path)
_ext := filepath.Ext(path)
_name := base[:len(base)-len(_ext)]
if `` == strings.TrimSpace(ext) {
filename = _name
} else {
filename = fmt.Sprintf(`%s.%s`, _name, ext)
}
return
}
func dir(path string) string {
return filepath.Join(filepath.Dir(path), name(filepath.Base(path), ``))
}