/
a_train.go
124 lines (98 loc) · 2.58 KB
/
a_train.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
package a_train
import (
"encoding/json"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog/hlog"
"github.com/cloudbox/autoscan"
)
type Drive struct {
ID string `yaml:"id"`
Rewrite []autoscan.Rewrite `yaml:"rewrite"`
}
type Config struct {
Drives []Drive `yaml:"drives"`
Priority int `yaml:"priority"`
Rewrite []autoscan.Rewrite `yaml:"rewrite"`
Verbosity string `yaml:"verbosity"`
}
type ATrainRewriter = func(drive string, input string) string
// // New creates an autoscan-compatible HTTP Trigger for A-Train webhooks.
func New(c Config) (autoscan.HTTPTrigger, error) {
rewrites := make(map[string]autoscan.Rewriter)
for _, drive := range c.Drives {
rewriter, err := autoscan.NewRewriter(append(drive.Rewrite, c.Rewrite...))
if err != nil {
return nil, err
}
rewrites[drive.ID] = rewriter
}
globalRewriter, err := autoscan.NewRewriter(c.Rewrite)
if err != nil {
return nil, err
}
rewriter := func(drive string, input string) string {
driveRewriter, ok := rewrites[drive]
if !ok {
return globalRewriter(input)
}
return driveRewriter(input)
}
trigger := func(callback autoscan.ProcessorFunc) http.Handler {
return handler{
callback: callback,
priority: c.Priority,
rewrite: rewriter,
}
}
return trigger, nil
}
type handler struct {
priority int
rewrite ATrainRewriter
callback autoscan.ProcessorFunc
}
type atrainEvent struct {
Created []string
Deleted []string
}
func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
var err error
rlog := hlog.FromRequest(r)
drive := chi.URLParam(r, "drive")
event := new(atrainEvent)
err = json.NewDecoder(r.Body).Decode(event)
if err != nil {
rlog.Error().Err(err).Msg("Failed decoding request")
rw.WriteHeader(http.StatusBadRequest)
return
}
rlog.Trace().Interface("event", event).Msg("Received JSON body")
scans := make([]autoscan.Scan, 0)
for _, path := range event.Created {
scans = append(scans, autoscan.Scan{
Folder: h.rewrite(drive, path),
Priority: h.priority,
Time: now(),
})
}
for _, path := range event.Deleted {
scans = append(scans, autoscan.Scan{
Folder: h.rewrite(drive, path),
Priority: h.priority,
Time: now(),
})
}
err = h.callback(scans...)
if err != nil {
rlog.Error().Err(err).Msg("Processor could not process scans")
rw.WriteHeader(http.StatusInternalServerError)
return
}
for _, scan := range scans {
rlog.Info().Str("path", scan.Folder).Msg("Scan moved to processor")
}
rw.WriteHeader(http.StatusOK)
}
var now = time.Now