@@ -12,6 +12,7 @@ import (
12
12
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
13
13
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
14
14
"github.com/dustin/go-humanize"
15
+ "github.com/google/uuid"
15
16
"go.uber.org/zap"
16
17
)
17
18
@@ -36,6 +37,7 @@ type Upload struct {
36
37
ResponseTemplate string `json:"response_template,omitempty"`
37
38
NotifyURL string `json:"notify_url,omitempty"`
38
39
NotifyMethod string `json:"notify_method,omitempty"`
40
+ CreateUuidDir bool `json:"create_uuid_dir,omitempty"`
39
41
40
42
MyTlsSetting struct {
41
43
InsecureSkipVerify bool `json:"insecure,omitempty"`
@@ -214,9 +216,35 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
214
216
}
215
217
defer file .Close ()
216
218
219
+ concatDir := u .DestDir
220
+
221
+ if u .CreateUuidDir {
222
+ uuidDir := uuid .New ()
223
+
224
+ // It's very unlikely that the uuidDir already exists, but just in case
225
+ for {
226
+ if _ , err := os .Stat (concatDir + "/" + uuidDir .String ()); os .IsNotExist (err ) {
227
+ break
228
+ } else {
229
+ uuidDir = uuid .New ()
230
+ }
231
+ }
232
+
233
+ concatDir = concatDir + "/" + uuidDir .String ()
234
+ }
235
+
236
+ if err := os .MkdirAll (concatDir , 0755 ); err != nil {
237
+ u .logger .Error ("UUID directory creation error" ,
238
+ zap .String ("requuid" , requuid ),
239
+ zap .String ("message" , "Failed to create " + concatDir ),
240
+ zap .Error (err ),
241
+ zap .Object ("request" , caddyhttp.LoggableHTTPRequest {Request : r }))
242
+ return caddyhttp .Error (http .StatusInternalServerError , err )
243
+ }
244
+
217
245
// Create the file within the DestDir directory
218
246
219
- tempFile , tmpf_err := os .OpenFile (u . DestDir + "/" + handler .Filename , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0755 )
247
+ tempFile , tmpf_err := os .OpenFile (concatDir + "/" + handler .Filename , os .O_RDWR | os .O_CREATE | os .O_TRUNC , 0755 )
220
248
221
249
if tmpf_err != nil {
222
250
u .logger .Error ("TempFile Error" ,
@@ -268,7 +296,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
268
296
}
269
297
}
270
298
271
- return next .ServeHTTP (w , r )
299
+ w .WriteHeader (http .StatusCreated )
300
+ return nil
272
301
}
273
302
274
303
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
@@ -347,6 +376,16 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
347
376
if ! d .Args (& u .MyTlsSetting .CAPath ) {
348
377
return d .ArgErr ()
349
378
}
379
+ case "create_uuid_dir" :
380
+ var uuidDirStr string
381
+ if ! d .AllArgs (& uuidDirStr ) {
382
+ return d .ArgErr ()
383
+ }
384
+ uuidDirBool , err := strconv .ParseBool (uuidDirStr )
385
+ if err != nil {
386
+ return d .Errf ("parsing create_uuid_dir: %v" , err )
387
+ }
388
+ u .CreateUuidDir = uuidDirBool
350
389
default :
351
390
return d .Errf ("unrecognized servers option '%s'" , d .Val ())
352
391
}
@@ -358,12 +397,11 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
358
397
// parseCaddyfile parses the upload directive. It enables the upload
359
398
// of a file:
360
399
//
361
- // upload {
362
- // dest_dir <destination directory>
363
- // max_filesize <size>
364
- // response_template [<path to a response template>]
365
- // }
366
- //
400
+ // upload {
401
+ // dest_dir <destination directory>
402
+ // max_filesize <size>
403
+ // response_template [<path to a response template>]
404
+ // }
367
405
func parseCaddyfile (h httpcaddyfile.Helper ) (caddyhttp.MiddlewareHandler , error ) {
368
406
var u Upload
369
407
err := u .UnmarshalCaddyfile (h .Dispenser )
0 commit comments