forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
209 lines (179 loc) · 3.9 KB
/
fs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package utils
import (
"bufio"
"chat/globals"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func CreateFolder(path string) bool {
if err := os.MkdirAll(path, os.ModePerm); err != nil && !os.IsExist(err) {
return false
}
return true
}
func Exists(path string) bool {
err := os.Mkdir(path, os.ModePerm)
return err != nil && os.IsExist(err)
}
func DirSafe(path string) string {
CreateFolder(path)
return path
}
func FileDirSafe(file string) string {
if strings.LastIndex(file, "/") == -1 {
return file
}
return DirSafe(file[:strings.LastIndex(file, "/")])
}
func FileSafe(file string) string {
FileDirSafe(file)
return file
}
func WriteFile(path string, data string, folderSafe bool) error {
if folderSafe {
FileDirSafe(path)
}
file, err := os.Create(path)
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), path))
}
}(file)
if _, err := file.WriteString(data); err != nil {
globals.Warn(fmt.Sprintf("[utils] write file error: %s (path: %s, bytes len: %d)", err.Error(), path, len(data)))
return err
}
return nil
}
func ReadFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), path))
}
}(file)
data, err := io.ReadAll(file)
if err != nil {
return "", err
}
return string(data), nil
}
func Walk(path string) []string {
var files []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if !info.IsDir() {
files = append(files, handlePath(path))
}
return nil
})
if err != nil {
return nil
}
return files
}
func GetFileSize(path string) int64 {
file, err := os.Open(path)
if err != nil {
return 0
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), path))
}
}(file)
stat, err := file.Stat()
if err != nil {
return 0
}
return stat.Size()
}
func GetFileCreated(path string) string {
file, err := os.Open(path)
if err != nil {
return ""
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), path))
}
}(file)
stat, err := file.Stat()
if err != nil {
return ""
}
return stat.ModTime().String()
}
func IsFileExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func CopyFile(src string, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer func(in *os.File) {
err := in.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), src))
}
}(in)
FileDirSafe(dst)
out, err := os.Create(dst)
if err != nil {
return err
}
defer func(out *os.File) {
err := out.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), dst))
}
}(out)
_, err = io.Copy(out, in)
return err
}
func DeleteFile(path string) error {
return os.Remove(path)
}
func ReadFileLatestLines(path string, length int) (string, error) {
if length <= 0 {
return "", errors.New("length must be greater than 0")
}
file, err := os.Open(path)
if err != nil {
return "", err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
globals.Warn(fmt.Sprintf("[utils] close file error: %s (path: %s)", err.Error(), path))
}
}(file)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if len(lines) < length {
length = len(lines)
}
return strings.Join(lines[len(lines)-length:], "\n"), nil
}