-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
103 lines (91 loc) · 2.56 KB
/
utils.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
package fs
import (
"fmt"
"github.com/crawlab-team/goseaweedfs"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
func IsGitFile(file goseaweedfs.FileInfo) (res bool) {
// skip .git
res, err := regexp.MatchString("/?\\.git/", file.Path)
if err != nil {
return false
}
return res
}
func IsIgnoreFile(file goseaweedfs.FileInfo) (res bool) {
// skip .git
filerStr := os.Getenv("CRAWLAB_IS_IGNORE_FILE_FILER")
if filerStr == "" {
return false
}
res, err := regexp.MatchString(filerStr, file.Path)
if err != nil {
return false
}
return res
}
func getCollectionAndTtlFromArgs(args ...interface{}) (collection, ttl string) {
if len(args) > 0 {
collection = args[0].(string)
}
if len(args) > 1 {
ttl = args[1].(string)
}
return
}
func getUrlValuesFromArgs(args ...interface{}) (values url.Values) {
if len(args) > 0 {
values = args[0].(url.Values)
}
return values
}
func getFilesAndFilesMaps(f *goseaweedfs.Filer, localPath, remotePath string) (localFiles []goseaweedfs.FileInfo, remoteFiles []goseaweedfs.FilerFileInfo, localFilesMap map[string]goseaweedfs.FileInfo, remoteFilesMap map[string]goseaweedfs.FilerFileInfo, err error) {
// declare maps
localFilesMap = map[string]goseaweedfs.FileInfo{}
remoteFilesMap = map[string]goseaweedfs.FilerFileInfo{}
// cache local files info
localFiles, err = goseaweedfs.ListFilesRecursive(localPath)
if err != nil {
return localFiles, remoteFiles, localFilesMap, remoteFilesMap, err
}
for _, file := range localFiles {
fileRemotePath := fmt.Sprintf("%s%s", remotePath, strings.Replace(file.Path, localPath, "", -1))
localFilesMap[fileRemotePath] = file
// directory
dirRemotePath := filepath.Dir(fileRemotePath)
_, ok := localFilesMap[dirRemotePath]
if !ok {
localFilesMap[dirRemotePath] = goseaweedfs.FileInfo{
Name: filepath.Base(dirRemotePath),
Path: dirRemotePath,
}
}
}
// cache remote files info
remoteFiles, err = f.ListDirRecursive(remotePath)
if err != nil {
if err.Error() != FilerResponseNotFoundErrorMessage {
return localFiles, remoteFiles, localFilesMap, remoteFilesMap, err
}
err = nil
}
remoteFiles = getFlattenRemoteFiles(remoteFiles)
for _, file := range remoteFiles {
remoteFilesMap[file.FullPath] = file
}
return
}
func getFlattenRemoteFiles(files []goseaweedfs.FilerFileInfo) (flattenFiles []goseaweedfs.FilerFileInfo) {
flattenFiles = []goseaweedfs.FilerFileInfo{}
for _, file := range files {
flattenFiles = append(flattenFiles, file)
if file.IsDir {
flattenFiles = append(flattenFiles, getFlattenRemoteFiles(file.Children)...)
}
}
return
}