/
watch.go
342 lines (283 loc) · 7.48 KB
/
watch.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
package transloadit
import (
"encoding/json"
"errors"
"fmt"
"github.com/mitchellh/go-homedir"
"gopkg.in/fsnotify.v1"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
var sanitizeRe = regexp.MustCompile(`[^a-zA-Z0-9\-\_\.]`)
type WatchOptions struct {
// Directory to watch files in (only if Watch is true)
Input string
// Directoy to put result files in
Output string
// Watch the input directory or just compile files in input directory
Watch bool
// Template id to convert files with
TemplateId string
// Optional notify url for each assembly.
NotifyUrl string
// Instead of using templates you can define steps
Steps map[string]map[string]interface{}
// If true the original files will be copied in the output directoy with `-original_0_` prefix.
// If false input files will be deleted.
Preserve bool
// Read template from JSON file. See exmaples for information about the format.
TemplateFile string
// If true file which are already in the directory won't be compiled.
DontProcessDir bool
}
type Watcher struct {
client *Client
// Original options passed to Client.Watch.
// If Input or Output are relative to the home directory
// they will be extended (~/input -> /home/user/input)
Options *WatchOptions
stopped bool
// Listen for errors
Error chan error
// Listen for completed assemblies
Done chan *AssemblyInfo
// Listen for file changes (only if Watch == true)
Change chan string
stop chan bool
recentWrites map[string]time.Time
blacklist map[string]bool
}
// Watch a directory for changes and convert all changes files and download the result.
// It will create a new assembly for each file.
// If the directory already contains some they are all converted.
// See WatchOptions for possible configuration.
func (client *Client) Watch(options *WatchOptions) *Watcher {
options.Input = expandPath(options.Input)
options.Output = expandPath(options.Output)
if options.TemplateFile != "" {
options.TemplateFile = expandPath(options.TemplateFile)
options.Steps = readJson(options.TemplateFile)
}
if _, err := os.Stat(options.Input); os.IsNotExist(err) {
panic(fmt.Errorf("Input directory does not exist: %s", options.Input))
}
if _, err := os.Stat(options.Output); os.IsNotExist(err) {
panic(fmt.Errorf("Output directory does not exist: %s", options.Output))
}
if options.Input == options.Output {
panic(fmt.Errorf("Input and output directory are both: %s", options.Output))
}
watcher := &Watcher{
client: client,
Options: options,
Error: make(chan error, 1),
Done: make(chan *AssemblyInfo),
Change: make(chan string),
stop: make(chan bool, 1),
recentWrites: make(map[string]time.Time),
blacklist: make(map[string]bool),
}
watcher.start()
return watcher
}
func (watcher *Watcher) start() {
if watcher.Options.Watch {
go watcher.startWatcher()
}
if !watcher.Options.DontProcessDir {
go watcher.processDir()
}
// If we have nothing to do (neither processing the input directory nor
// watching it for changes) stop everything again
if !watcher.Options.Watch && watcher.Options.DontProcessDir {
watcher.Stop()
}
}
// Stop the watcher.
func (watcher *Watcher) Stop() {
if watcher.stopped {
return
}
watcher.stopped = true
close(watcher.Done)
close(watcher.Error)
close(watcher.Change)
close(watcher.stop)
}
func (watcher *Watcher) processDir() {
files, err := ioutil.ReadDir(watcher.Options.Input)
if err != nil {
watcher.error(err)
return
}
input := watcher.Options.Input
var wg sync.WaitGroup
for _, file := range files {
if !file.IsDir() {
wg.Add(1)
go func(file os.FileInfo) {
watcher.processFile(path.Join(input, file.Name()))
wg.Done()
}(file)
}
}
wg.Wait()
// If watching is not enabled, stop and close all channels to avoid a
// deadlock since we are done with everything.
if !watcher.Options.Watch {
watcher.Stop()
}
}
func (watcher *Watcher) processFile(name string) {
file, err := os.Open(name)
if err != nil {
watcher.error(err)
return
}
// Add file to blacklist
watcher.blacklist[name] = true
assembly := watcher.client.CreateAssembly()
if watcher.Options.TemplateId != "" {
assembly.TemplateId = watcher.Options.TemplateId
}
if watcher.Options.NotifyUrl != "" {
assembly.NotifyUrl = watcher.Options.NotifyUrl
}
for name, step := range watcher.Options.Steps {
assembly.AddStep(name, step)
}
assembly.Blocking = true
assembly.AddReader("file", path.Base(name), file)
info, err := assembly.Upload()
if err != nil {
watcher.error(err)
return
}
if info.Error != "" {
watcher.error(errors.New(info.Error))
return
}
var wg sync.WaitGroup
for stepName, results := range info.Results {
for index, result := range results {
wg.Add(1)
go func() {
watcher.downloadResult(stepName, index, result)
watcher.handleOriginalFile(name)
delete(watcher.blacklist, name)
watcher.Done <- info
wg.Done()
}()
}
}
wg.Wait()
if !watcher.Options.Watch {
watcher.Stop()
}
}
func (watcher *Watcher) downloadResult(stepName string, index int, result *FileInfo) {
fileName := sanitizeRe.ReplaceAllString(fmt.Sprintf("%s_%d_%s", stepName, index, result.Name), "-")
resp, err := http.Get(result.Url)
if err != nil {
watcher.error(err)
return
}
defer resp.Body.Close()
out, err := os.Create(path.Join(watcher.Options.Output, fileName))
if err != nil {
watcher.error(err)
return
}
defer out.Close()
io.Copy(out, resp.Body)
}
func (watcher *Watcher) startWatcher() {
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
watcher.error(err)
return
}
defer fsWatcher.Close()
if err = fsWatcher.Add(watcher.Options.Input); err != nil {
watcher.error(err)
return
}
for {
select {
case _, more := <-watcher.stop:
if !more {
return
}
case err := <-fsWatcher.Errors:
watcher.error(err)
case evt := <-fsWatcher.Events:
// Ignore the event if the file is currently processed
if _, ok := watcher.blacklist[evt.Name]; ok == true {
continue
}
if evt.Op&fsnotify.Create == fsnotify.Create || evt.Op&fsnotify.Write == fsnotify.Write {
watcher.recentWrites[evt.Name] = time.Now()
}
case <-time.Tick(1 * time.Second):
now := time.Now()
for name, lastEvent := range watcher.recentWrites {
diff := now.Sub(lastEvent)
if diff > (time.Millisecond * 500) {
delete(watcher.recentWrites, name)
watcher.Change <- name
go watcher.processFile(name)
}
}
}
}
}
func (watcher *Watcher) handleOriginalFile(name string) {
var err error
if watcher.Options.Preserve {
_, file := path.Split(name)
err = os.Rename(name, watcher.Options.Output+"/-original_0_"+basename(file))
} else {
err = os.Remove(name)
}
if err != nil {
watcher.error(err)
}
}
func (watcher *Watcher) error(err error) {
watcher.Error <- err
}
func basename(name string) string {
i := strings.LastIndex(name, string(os.PathSeparator))
return name[i+1:]
}
func expandPath(str string) string {
expanded, err := homedir.Expand(str)
if err != nil {
panic(err)
}
expanded, err = filepath.Abs(expanded)
if err != nil {
panic(err)
}
return expanded
}
func readJson(file string) map[string]map[string]interface{} {
content, err := ioutil.ReadFile(file)
if err != nil {
panic(fmt.Errorf("Error reading template file: %s", err))
}
steps := make(map[string]map[string]interface{})
err = json.Unmarshal(content, &steps)
if err != nil {
panic(fmt.Errorf("Error parsing template file: %s", err))
}
return steps
}