forked from crawlab-team/crawlab-fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseaweedfs_manager.go
406 lines (361 loc) · 10.6 KB
/
seaweedfs_manager.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package fs
import (
"bytes"
"errors"
"fmt"
"github.com/crawlab-team/go-trace"
"github.com/crawlab-team/goseaweedfs"
"github.com/google/uuid"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
)
type SeaweedFsManager struct {
// settings variables
filerUrl string
timeout time.Duration
authKey string
// internals
f *goseaweedfs.Filer
}
func (m *SeaweedFsManager) Init() (err error) {
var filerOpts []goseaweedfs.FilerOption
if m.authKey != "" {
filerOpts = append(filerOpts, goseaweedfs.WithFilerAuthKey(m.authKey))
}
m.f, err = goseaweedfs.NewFiler(m.filerUrl, &http.Client{Timeout: m.timeout}, filerOpts...)
if err != nil {
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) Close() (err error) {
if err := m.f.Close(); err != nil {
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) ListDir(remotePath string, isRecursive bool, args ...interface{}) (files []goseaweedfs.FilerFileInfo, err error) {
if isRecursive {
files, err = m.f.ListDirRecursive(remotePath)
} else {
files, err = m.f.ListDir(remotePath)
}
if err != nil {
return files, trace.TraceError(err)
}
return files, nil
}
func (m *SeaweedFsManager) UploadFile(localPath, remotePath string, args ...interface{}) (err error) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return trace.TraceError(err)
}
collection, ttl := getCollectionAndTtlFromArgs(args...)
res, err := m.f.UploadFile(localPath, remotePath, collection, ttl)
if err != nil {
return trace.TraceError(err)
}
if res.Error != "" {
err = errors.New(res.Error)
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) UploadDir(localPath, remotePath string, args ...interface{}) (err error) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return trace.TraceError(err)
}
collection, ttl := getCollectionAndTtlFromArgs(args...)
results, err := m.f.UploadDir(localPath, remotePath, collection, ttl)
if err != nil {
return trace.TraceError(err)
}
for _, res := range results {
if res.Error != "" {
err = errors.New(res.Error)
return trace.TraceError(err)
}
}
return nil
}
func (m *SeaweedFsManager) DownloadFile(remotePath, localPath string, args ...interface{}) (err error) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return trace.TraceError(err)
}
urlValues := getUrlValuesFromArgs(args...)
err = m.f.Download(remotePath, urlValues, func(reader io.Reader) error {
data, err := ioutil.ReadAll(reader)
if err != nil {
return trace.TraceError(err)
}
dirPath := filepath.Dir(localPath)
_, err = os.Stat(dirPath)
if err != nil {
// if not exists, create a new directory
if err := os.MkdirAll(dirPath, DefaultDirMode); err != nil {
return trace.TraceError(err)
}
}
fileMode := DefaultFileMode
fileInfo, err := os.Stat(localPath)
if err == nil {
// if file already exists, save file mode and remove it
fileMode = fileInfo.Mode()
if err := os.Remove(localPath); err != nil {
return trace.TraceError(err)
}
}
if err := ioutil.WriteFile(localPath, data, fileMode); err != nil {
return trace.TraceError(err)
}
return nil
})
if err != nil {
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) DownloadDir(remotePath, localPath string, args ...interface{}) (err error) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return trace.TraceError(err)
}
files, err := m.ListDir(remotePath, true)
for _, file := range files {
if file.IsDir {
if err := m.DownloadDir(file.FullPath, path.Join(localPath, file.Name), args...); err != nil {
return trace.TraceError(err)
}
} else {
if err := m.DownloadFile(file.FullPath, path.Join(localPath, file.Name), args...); err != nil {
return trace.TraceError(err)
}
}
}
return nil
}
func (m *SeaweedFsManager) DeleteFile(remotePath string, args ...interface{}) (err error) {
if err := m.f.DeleteFile(remotePath); err != nil {
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) DeleteDir(remotePath string, args ...interface{}) (err error) {
if err := m.f.DeleteDir(remotePath); err != nil {
return trace.TraceError(err)
}
return nil
}
func (m *SeaweedFsManager) SyncLocalToRemote(localPath, remotePath string, args ...interface{}) (err error) {
localPath, err = filepath.Abs(localPath)
if err != nil {
return trace.TraceError(err)
}
// raise error if local path does not exist
if _, err := os.Stat(localPath); err != nil {
return trace.TraceError(err)
}
// get files and maps
localFiles, remoteFiles, localFilesMap, remoteFilesMap, err := getFilesAndFilesMaps(m.f, localPath, remotePath)
if err != nil {
return trace.TraceError(err)
}
// compare remote files with local files and delete files absent in local files
for _, remoteFile := range remoteFiles {
// skip directories
if remoteFile.IsDir {
continue
}
// attempt to get corresponding local file
_, ok := localFilesMap[remoteFile.FullPath]
if !ok {
// file does not exist on local, delete
if err := m.DeleteFile(remoteFile.FullPath); err != nil {
return trace.TraceError(err)
}
}
}
// compare local files with remote files and upload files with difference
for _, localFile := range localFiles {
// skip .git
if IsGitFile(localFile) {
continue
}
// corresponding remote file path
fileRemotePath := fmt.Sprintf("%s%s", remotePath, strings.Replace(localFile.Path, localPath, "", -1))
// attempt to get corresponding remote file
remoteFile, ok := remoteFilesMap[fileRemotePath]
if !ok {
// file does not exist on remote, upload
if err := m.UploadFile(localFile.Path, fileRemotePath, args...); err != nil {
return trace.TraceError(err)
}
} else {
// file exists on remote, upload if md5sum values are different
if remoteFile.Md5 != localFile.Md5 {
if err := m.UploadFile(localFile.Path, fileRemotePath, args...); err != nil {
return trace.TraceError(err)
}
}
}
}
return nil
}
func (m *SeaweedFsManager) SyncRemoteToLocal(remotePath, localPath string, args ...interface{}) (err error) {
// create directory if local path does not exist
if _, err := os.Stat(localPath); err != nil {
if err := os.MkdirAll(localPath, os.ModePerm); err != nil {
return trace.TraceError(err)
}
}
// get files and maps
localFiles, remoteFiles, localFilesMap, remoteFilesMap, err := getFilesAndFilesMaps(m.f, localPath, remotePath)
if err != nil {
return trace.TraceError(err)
}
// compare local files with remote files and delete files absent on remote
for _, localFile := range localFiles {
// skip .git
if IsGitFile(localFile) {
continue
}
// corresponding remote file path
fileRemotePath := fmt.Sprintf("%s%s", remotePath, strings.Replace(localFile.Path, localPath, "", -1))
// attempt to get corresponding remote file
_, ok := remoteFilesMap[fileRemotePath]
if !ok {
// file does not exist on remote, upload
if err := os.Remove(localFile.Path); err != nil {
return trace.TraceError(err)
}
}
}
// compare remote files with local files and download if files with difference
for _, remoteFile := range remoteFiles {
// directory
if remoteFile.IsDir {
localDirRelativePath := strings.Replace(remoteFile.FullPath, remotePath, "", 1)
localDirPath := fmt.Sprintf("%s%s", localPath, localDirRelativePath)
if err := m.SyncRemoteToLocal(remoteFile.FullPath, localDirPath); err != nil {
return err
}
continue
}
// local file path
localFileRelativePath := strings.Replace(remoteFile.FullPath, remotePath, "", 1)
localFilePath := fmt.Sprintf("%s%s", localPath, localFileRelativePath)
// attempt to get corresponding local file
localFile, ok := localFilesMap[remoteFile.FullPath]
if !ok {
// file does not exist on local, download
if err := m.DownloadFile(remoteFile.FullPath, localFilePath); err != nil {
return trace.TraceError(err)
}
} else {
// file exists on remote, download if md5sum values are different
if remoteFile.Md5 != localFile.Md5 {
if err := m.DownloadFile(remoteFile.FullPath, localFilePath, args...); err != nil {
return trace.TraceError(err)
}
}
}
}
return nil
}
func (m *SeaweedFsManager) GetFile(remotePath string, args ...interface{}) (data []byte, err error) {
urlValues := getUrlValuesFromArgs(args...)
var buf bytes.Buffer
err = m.f.Download(remotePath, urlValues, func(reader io.Reader) error {
_, err := io.Copy(&buf, reader)
if err != nil {
return trace.TraceError(err)
}
return nil
})
data = buf.Bytes()
return
}
func (m *SeaweedFsManager) GetFileInfo(remotePath string, args ...interface{}) (file *goseaweedfs.FilerFileInfo, err error) {
arr := strings.Split(remotePath, "/")
dirName := strings.Join(arr[:(len(arr)-1)], "/")
files, err := m.f.ListDir(dirName)
if err != nil {
return file, trace.TraceError(err)
}
for _, f := range files {
if f.FullPath == remotePath {
return &f, nil
}
}
return nil, trace.TraceError(ErrorFsNotExists)
}
func (m *SeaweedFsManager) UpdateFile(remotePath string, data []byte, args ...interface{}) (err error) {
tmpRootDir := os.TempDir()
tmpDirPath := path.Join(tmpRootDir, ".seaweedfs")
if _, err := os.Stat(tmpDirPath); err != nil {
if err := os.MkdirAll(tmpDirPath, os.ModePerm); err != nil {
return trace.TraceError(err)
}
}
tmpFilePath := path.Join(tmpDirPath, fmt.Sprintf(".%s", uuid.New().String()))
if _, err := os.Stat(tmpFilePath); err == nil {
if err := os.Remove(tmpFilePath); err != nil {
return trace.TraceError(err)
}
}
if err := ioutil.WriteFile(tmpFilePath, data, os.ModePerm); err != nil {
return trace.TraceError(err)
}
if err = m.UploadFile(tmpFilePath, remotePath, args...); err != nil {
return trace.TraceError(err)
}
if err := os.Remove(tmpFilePath); err != nil {
return trace.TraceError(err)
}
return
}
func (m *SeaweedFsManager) Exists(remotePath string, args ...interface{}) (ok bool, err error) {
_, err = m.GetFile(remotePath, args...)
if err == nil {
// exists
return true, nil
}
if strings.Contains(err.Error(), FilerStatusNotFoundErrorMessage) {
// not exists
return false, nil
}
return ok, trace.TraceError(err)
}
func (m *SeaweedFsManager) SetFilerUrl(url string) {
m.filerUrl = url
}
func (m *SeaweedFsManager) SetFilerAuthKey(authKey string) {
m.authKey = authKey
}
func (m *SeaweedFsManager) SetTimeout(timeout time.Duration) {
m.timeout = timeout
}
func NewSeaweedFsManager(opts ...Option) (m2 Manager, err error) {
// manager
m := &SeaweedFsManager{
filerUrl: "http://localhost:8888",
timeout: 5 * time.Minute,
}
// apply options
for _, opt := range opts {
opt(m)
}
// initialize
if err := m.Init(); err != nil {
return nil, err
}
return m, nil
}