-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncer.go
160 lines (119 loc) · 2.89 KB
/
syncer.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
package sync
import (
"bytes"
"fmt"
"io/ioutil"
"mime"
"os"
"path"
"strconv"
"strings"
)
// Syncer synchronises files from a directory to a storage service.
type Syncer struct {
Store ObjectStore
Log func(string, ...interface{})
}
// Run begins a sync using the specified directory.
func (s *Syncer) Run(dir string, exts string) (string, error) {
files, err := scanDirectory(dir, exts)
if err != nil {
return "", err
}
localIndex := &Index{}
for _, file := range files {
filepath := path.Join(dir, file)
key, err := CreateCASKey(filepath)
if err != nil {
return "", err
}
tags := strings.Join(parseTags(file), " ")
localIndex.Add(key, tags, filepath, false)
}
if len(localIndex.Objects) == 0 {
return "", fmt.Errorf("no files matching (%v) found in: %v", exts, dir)
}
remoteIndex, _ := s.fetchRemoteIndex("index.json")
diff := remoteIndex.Diff(localIndex)
uploaded := 0
for _, object := range diff.Objects {
if !object.IsNew {
continue
}
s.Log("uploading: %v", object.Key)
file, err := os.Open(object.Filepath)
if err != nil {
return "", err
}
contentType := mime.TypeByExtension(path.Ext(object.Filepath))
if err := s.Store.Put(object.Key, contentType, file); err != nil {
return "", err
}
file.Close()
uploaded++
}
s.Log("processed %d objects", len(localIndex.Objects))
s.Log("uploaded %d objects", uploaded)
return s.saveRemoteIndex(localIndex, "index.json")
}
func (s *Syncer) fetchRemoteIndex(key string) (*Index, error) {
index := &Index{}
bytes, err := s.Store.Get(key)
if err != nil {
return index, err
}
if err := index.LoadJSON(bytes); err != nil {
return index, err
}
return index, nil
}
func (s *Syncer) saveRemoteIndex(index *Index, key string) (string, error) {
data, err := index.SaveJSON()
if err != nil {
return "", err
}
if err := s.Store.Put(key, "application/json", bytes.NewReader(data)); err != nil {
return "", err
}
return string(data), nil
}
func scanDirectory(dir string, exts string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
matches := make([]string, 0)
for _, file := range files {
if file.IsDir() {
continue
}
if !hasValidExtension(file.Name(), exts) {
continue
}
matches = append(matches, file.Name())
}
return matches, nil
}
func hasValidExtension(filename string, exts string) bool {
if exts == "" {
return true
}
extension := strings.TrimPrefix(path.Ext(filename), ".")
for _, ext := range strings.Split(exts, ",") {
if extension == strings.TrimSpace(ext) {
return true
}
}
return false
}
func parseTags(filename string) []string {
basename := strings.TrimSuffix(filename, path.Ext(filename))
tags := make([]string, 0)
for _, part := range strings.Split(basename, " ") {
if _, err := strconv.Atoi(part); err == nil {
continue
}
tags = append(tags, strings.TrimSpace(part))
}
return tags
}