-
Notifications
You must be signed in to change notification settings - Fork 6
/
upload.go
368 lines (312 loc) · 11.3 KB
/
upload.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
package upload
import (
"fmt"
"io"
"net/http"
"os"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/dustin/go-humanize"
"github.com/google/uuid"
"go.uber.org/zap"
)
const (
Version = "0.17"
)
func init() {
caddy.RegisterModule(Upload{})
httpcaddyfile.RegisterHandlerDirective("upload", parseCaddyfile)
}
// Middleware implements an HTTP handler that writes the
// uploaded file to a file on the disk.
type Upload struct {
DestDir string `json:"dest_dir,omitempty"`
RootDir string `json:"root_dir,omitempty"`
FileFieldName string `json:"file_field_name,omitempty"`
FixedFileName string `json:"fixed_file_name,omitempty"`
MaxFilesize int64 `json:"max_filesize_int,omitempty"`
MaxFilesizeH string `json:"max_filesize,omitempty"`
MaxFormBuffer int64 `json:"max_form_buffer_int,omitempty"`
MaxFormBufferH string `json:"max_form_buffer,omitempty"`
ResponseTemplate string `json:"response_template,omitempty"`
NotifyURL string `json:"notify_url,omitempty"`
NotifyMethod string `json:"notify_method,omitempty"`
CreateUuidDir bool `json:"create_uuid_dir,omitempty"`
DestDirFieldName string `json:"dest_dir_field_name,omitempty"`
MyTlsSetting struct {
InsecureSkipVerify bool `json:"insecure,omitempty"`
CAPath string `json:"capath,omitempty"`
}
// TODO: Handle notify Body
ctx caddy.Context
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (Upload) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.upload",
New: func() caddy.Module { return new(Upload) },
}
}
// Provision implements caddy.Provisioner.
func (u *Upload) Provision(ctx caddy.Context) error {
u.ctx = ctx
u.logger = ctx.Logger(u)
repl := caddy.NewReplacer()
if u.DestDir == "" && u.DestDirFieldName == "" {
u.logger.Error("Provision",
zap.String("msg", "no Destination Directory specified (dest_dir or dest_dir_field_name)"))
return fmt.Errorf("no Destination Directory specified (dest_dir or dest_dir_field_name)")
}
if u.RootDir == "" {
u.logger.Info("Provision",
zap.String("msg", "no Root Directory specified (root_dir), will use {http.vars.root}"))
}
if u.FileFieldName == "" {
u.logger.Warn("Provision",
zap.String("msg", "no FileFieldName specified (file_field_name), using the default one 'myFile'"),
)
u.FileFieldName = "myFile"
}
if u.FixedFileName == "" {
u.logger.Info("Provision",
zap.String("msg", "no fixed file name specified (fixed_file_name), will use file name from header"))
}
if u.ResponseTemplate == "" {
u.logger.Warn("Provision",
zap.String("msg", "no ResponseTemplate specified (response_template), using the default one"),
)
u.ResponseTemplate = "upload-resp-template.txt"
}
if u.NotifyURL != "" && u.NotifyMethod == "" {
u.NotifyMethod = "GET"
}
if u.MaxFilesize == 0 && u.MaxFilesizeH != "" {
MaxFilesizeH := repl.ReplaceAll(u.MaxFilesizeH, "1GB")
u.MaxFilesizeH = MaxFilesizeH
size, err := humanize.ParseBytes(u.MaxFilesizeH)
if err != nil {
u.logger.Error("Provision ReplaceAll",
zap.String("msg", "MaxFilesizeH: Error parsing max_filesize"),
zap.String("MaxFilesizeH", u.MaxFilesizeH),
zap.Error(err))
return err
}
u.MaxFilesize = int64(size)
} else {
if u.MaxFilesize == 0 {
size, err := humanize.ParseBytes("1GB")
if err != nil {
u.logger.Error("Provision int",
zap.String("msg", "MaxFilesize: Error parsing max_filesize_int"),
zap.Int64("MaxFilesize", u.MaxFilesize),
zap.Error(err))
return err
}
u.MaxFilesize = int64(size)
}
}
if u.MaxFormBuffer == 0 && u.MaxFormBufferH != "" {
MaxFormBufferH := repl.ReplaceAll(u.MaxFormBufferH, "1GB")
u.MaxFormBufferH = MaxFormBufferH
size, err := humanize.ParseBytes(u.MaxFormBufferH)
if err != nil {
u.logger.Error("Provision ReplaceAll",
zap.String("msg", "MaxFormBufferH: Error parsing max_form_buffer"),
zap.String("MaxFormBufferH", u.MaxFormBufferH),
zap.Error(err))
return err
}
u.MaxFormBuffer = int64(size)
} else {
if u.MaxFormBuffer == 0 {
size, err := humanize.ParseBytes("1GB")
if err != nil {
u.logger.Error("Provision int",
zap.String("msg", "MaxFormBufferH: Error parsing max_form_buffer_int"),
zap.Int64("MaxFormBuffer", u.MaxFormBuffer),
zap.Error(err))
return err
}
u.MaxFormBuffer = int64(size)
}
}
u.logger.Info("Current Config",
zap.String("Version", Version),
zap.String("dest_dir", u.DestDir),
zap.String("root_dir", u.RootDir),
zap.String("fixed_file_name", u.FixedFileName),
zap.Int64("max_filesize_int", u.MaxFilesize),
zap.String("max_filesize", u.MaxFilesizeH),
zap.Int64("max_form_buffer_int", u.MaxFormBuffer),
zap.String("max_form_buffer", u.MaxFormBufferH),
zap.String("response_template", u.ResponseTemplate),
zap.String("notify_method", u.NotifyMethod),
zap.String("notify_url", u.NotifyURL),
zap.Bool("CreateUuidDir", u.CreateUuidDir),
zap.String("capath", u.MyTlsSetting.CAPath),
zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify),
zap.String("dest_dir_field_name", u.DestDirFieldName),
)
return nil
}
// Validate implements caddy.Validator.
func (u *Upload) Validate() error {
// TODO: Do I need this func
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
requuid, requuiderr := repl.GetString("http.request.uuid")
if !requuiderr {
requuid = "0"
u.logger.Error("http.request.uuid",
zap.Bool("requuiderr", requuiderr),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
}
repl.Set("http.upload.max_filesize", u.MaxFilesize)
r.Body = http.MaxBytesReader(w, r.Body, u.MaxFilesize)
if max_size_err := r.ParseMultipartForm(u.MaxFormBuffer); max_size_err != nil {
u.logger.Error("ServeHTTP",
zap.String("requuid", requuid),
zap.String("message", "The uploaded file is too big. Please choose an file that's less than MaxFilesize."),
zap.String("MaxFilesize", humanize.Bytes(uint64(u.MaxFilesize))),
zap.Error(max_size_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusRequestEntityTooLarge, max_size_err)
}
// cleanup uploaded files see
// https://github.com/git001/caddyv2-upload/issues/13
defer r.MultipartForm.RemoveAll()
// FormFile returns the first file for the given file field key
// it also returns the FileHeader so we can get the Filename,
// the Header and the size of the file
file, handler, ff_err := r.FormFile(u.FileFieldName)
if ff_err != nil {
u.logger.Error("FormFile Error",
zap.String("requuid", requuid),
zap.String("message", "Error Retrieving the File"),
zap.Error(ff_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, ff_err)
}
defer file.Close()
if u.RootDir == "" {
var rootDirErr bool
u.RootDir, rootDirErr = repl.GetString("http.vars.root")
if !rootDirErr {
u.logger.Error("Provision",
zap.Bool("rootDirErr", rootDirErr),
zap.String("rootDir", u.RootDir),
zap.String("message", "Variable {http.vars.root} not defined"))
return caddyhttp.Error(http.StatusInternalServerError, fmt.Errorf("can't find root dir"))
}
}
if u.DestDirFieldName != "" {
u.DestDir = r.FormValue(u.DestDirFieldName)
}
concatDir := caddyhttp.SanitizedPathJoin(u.RootDir, u.DestDir)
if u.CreateUuidDir {
uuidDir := uuid.New().String()
// It's very unlikely that the uuidDir already exists, but just in case
for {
if _, err := os.Stat(caddyhttp.SanitizedPathJoin(concatDir, uuidDir)); os.IsNotExist(err) {
break
} else {
uuidDir = uuid.New().String()
}
}
concatDir = caddyhttp.SanitizedPathJoin(concatDir, uuidDir)
repl.Set("http.upload.uuiddir", uuidDir)
}
if err := os.MkdirAll(concatDir, 0755); err != nil {
u.logger.Error("UUID directory creation error",
zap.String("requuid", requuid),
zap.String("message", "Failed to create "+concatDir),
zap.Error(err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, err)
}
// Create the file within the DestDir directory
var filename string = handler.Filename
if u.FixedFileName != "" {
filename = u.FixedFileName
}
tempFile, tmpf_err := os.OpenFile(caddyhttp.SanitizedPathJoin(concatDir, filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if tmpf_err != nil {
u.logger.Error("TempFile Error",
zap.String("requuid", requuid),
zap.String("message", "Error at TempFile"),
zap.Error(tmpf_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, tmpf_err)
}
defer tempFile.Close()
// read all of the contents of our uploaded file into a
// byte array
fileBytes, io_err := io.Copy(tempFile, file)
if io_err != nil {
u.logger.Error("Copy Error",
zap.String("requuid", requuid),
zap.String("message", "Error at io.Copy"),
zap.Error(io_err),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, io_err)
}
u.logger.Info("Successful Upload Info",
zap.String("requuid", requuid),
zap.String("Uploaded File", filename),
zap.Int64("File Size", handler.Size),
zap.Int64("written-bytes", fileBytes),
zap.String("UploadDir", concatDir),
zap.Any("MIME Header", handler.Header),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
repl.Set("http.upload.filename", filename)
repl.Set("http.upload.filesize", handler.Size)
repl.Set("http.upload.directory", concatDir)
if u.NotifyURL != "" {
errNotify := u.SendNotify(requuid)
if errNotify != nil {
u.logger.Error("Notify Error",
zap.Error(errNotify),
)
}
}
if u.ResponseTemplate != "" {
fileRespTemplate, fRTErr := os.Open(caddyhttp.SanitizedPathJoin(u.RootDir, u.ResponseTemplate))
if fRTErr != nil {
u.logger.Error("File Response Template open Error",
zap.String("requuid", requuid),
zap.Any("fileRespTemplate", fileRespTemplate),
zap.String("message", "Error at os.Open"),
zap.Error(fRTErr),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, fRTErr)
}
defer fileRespTemplate.Close()
// get information about the file
info, fRTSTErr := fileRespTemplate.Stat()
if fRTSTErr != nil {
u.logger.Error("File Response Template Stat Error",
zap.String("requuid", requuid),
zap.Any("fileRespTemplate", fileRespTemplate),
zap.String("message", "Error at fileRespTemplate.Stat"),
zap.Error(fRTSTErr),
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
return caddyhttp.Error(http.StatusInternalServerError, fRTSTErr)
}
http.ServeContent(w, r, info.Name(), info.ModTime(), fileRespTemplate)
return nil
}
return nil
}
// Interface guards
var (
_ caddy.Provisioner = (*Upload)(nil)
_ caddy.Validator = (*Upload)(nil)
_ caddyhttp.MiddlewareHandler = (*Upload)(nil)
_ caddyfile.Unmarshaler = (*Upload)(nil)
)