-
Notifications
You must be signed in to change notification settings - Fork 190
/
define.go
109 lines (90 loc) · 2.26 KB
/
define.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package fsutil
import (
"io/fs"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/strutil"
)
const (
// MimeSniffLen sniff Length, use for detect file mime type
MimeSniffLen = 512
)
// NameMatchFunc name match func, alias of comdef.StringMatchFunc
type NameMatchFunc = comdef.StringMatchFunc
// PathMatchFunc path match func. alias of comdef.StringMatchFunc
type PathMatchFunc = comdef.StringMatchFunc
// Entry extends fs.DirEntry, add some useful methods
type Entry interface {
fs.DirEntry
// Path get file/dir full path. eg: "/path/to/file.go"
Path() string
// Info get file info. like fs.DirEntry.Info(), but will cache result.
Info() (fs.FileInfo, error)
}
type entry struct {
fs.DirEntry
path string
stat fs.FileInfo
sErr error
}
// NewEntry create a new Entry instance
func NewEntry(fPath string, ent fs.DirEntry) Entry {
return &entry{
path: fPath,
DirEntry: ent,
}
}
// Path get full file/dir path. eg: "/path/to/file.go"
func (e *entry) Path() string {
return e.path
}
// Info get file info, will cache result
func (e *entry) Info() (fs.FileInfo, error) {
if e.stat == nil {
e.stat, e.sErr = e.DirEntry.Info()
}
return e.stat, e.sErr
}
// String get string representation
func (e *entry) String() string {
return strutil.OrCond(e.IsDir(), "dir: ", "file: ") + e.Path()
}
// FileInfo extends fs.FileInfo, add some useful methods
type FileInfo interface {
fs.FileInfo
// Path get file full path. eg: "/path/to/file.go"
Path() string
}
type fileInfo struct {
fs.FileInfo
fullPath string
}
// NewFileInfo create a new FileInfo instance
func NewFileInfo(fPath string, info fs.FileInfo) FileInfo {
return &fileInfo{
fullPath: fPath,
FileInfo: info,
}
}
// Path get file full path. eg: "/path/to/file.go"
func (fi *fileInfo) Path() string {
return fi.fullPath
}
// FileInfos type for FileInfo slice
//
// implements sort.Interface:
//
// sorts by oldest time modified in the file info.
// eg: [old_220211, old_220212, old_220213]
type FileInfos []FileInfo
// Len get length
func (fis FileInfos) Len() int {
return len(fis)
}
// Swap swap values
func (fis FileInfos) Swap(i, j int) {
fis[i], fis[j] = fis[j], fis[i]
}
// Less check by mod time
func (fis FileInfos) Less(i, j int) bool {
return fis[j].ModTime().After(fis[i].ModTime())
}