forked from linuxpham/fasthttpsession
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
98 lines (88 loc) · 1.97 KB
/
file.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
package file
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type file struct{}
// create file
func (f *file) createFile(filename string) error {
newFile, err := os.Create(filename)
defer newFile.Close()
return err
}
// file or path is exists
func (f *file) pathIsExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
// get file content
func (f *file) getContent(filename string) (data []byte, err error) {
fi, err := os.Open(filename)
if err != nil {
return
}
defer fi.Close()
fd, err := ioutil.ReadAll(fi)
return fd, nil
}
// get file update time
func (f *file) getModifyTime(filename string) int64 {
fileInfo, _ := os.Stat(filename)
modTime := fileInfo.ModTime()
return modTime.Unix()
}
// Gets all files in the specified directory and all subdirectories, and can match the suffix filter.
func (f *file) walkDir(dirPth, suffix string) (files []string, err error) {
files = make([]string, 0, 30)
if suffix != "" {
suffix = strings.ToUpper(suffix)
}
err = filepath.Walk(dirPth, func(filename string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
if suffix != "" {
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
files = append(files, filename)
}
} else {
files = append(files, filename)
}
return nil
})
return files, err
}
// Gets all files count in the specified directory and all subdirectories, and can match the suffix filter.
func (f *file) count(dirPth, suffix string) (total int, err error) {
if suffix != "" {
suffix = strings.ToUpper(suffix)
}
err = filepath.Walk(dirPth, func(filename string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
if suffix != "" {
if strings.HasSuffix(strings.ToUpper(fi.Name()), suffix) {
total += 1
}
} else {
total += 1
}
return nil
})
return total, err
}