From c8e167dd100ad61899aa860c6899b4dba9b35624 Mon Sep 17 00:00:00 2001 From: James Harvey <44349936+jmshrv@users.noreply.github.com> Date: Fri, 21 Apr 2023 15:33:13 +0100 Subject: [PATCH 1/4] Initial UUID dir support --- upload.go | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/upload.go b/upload.go index fbd8e8d..7ce996d 100644 --- a/upload.go +++ b/upload.go @@ -12,6 +12,7 @@ import ( "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" ) @@ -36,6 +37,7 @@ type Upload struct { 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"` MyTlsSetting struct { InsecureSkipVerify bool `json:"insecure,omitempty"` @@ -214,9 +216,35 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp } defer file.Close() + concatDir := u.DestDir + + if u.CreateUuidDir { + uuidDir := uuid.New() + + // It's very unlikely that the uuidDir already exists, but just in case + for { + if _, err := os.Stat(concatDir + "/" + uuidDir.String()); os.IsNotExist(err) { + break + } else { + uuidDir = uuid.New() + } + } + + concatDir = concatDir + "/" + uuidDir.String() + + 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 - tempFile, tmpf_err := os.OpenFile(u.DestDir+"/"+handler.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) + tempFile, tmpf_err := os.OpenFile(concatDir+"/"+handler.Filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) if tmpf_err != nil { u.logger.Error("TempFile Error", @@ -358,12 +386,11 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { // parseCaddyfile parses the upload directive. It enables the upload // of a file: // -// upload { -// dest_dir -// max_filesize -// response_template [] -// } -// +// upload { +// dest_dir +// max_filesize +// response_template [] +// } func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { var u Upload err := u.UnmarshalCaddyfile(h.Dispenser) From 89aff09c90f453250f970b25392649b88d96b7a5 Mon Sep 17 00:00:00 2001 From: James Harvey <44349936+jmshrv@users.noreply.github.com> Date: Fri, 21 Apr 2023 16:03:25 +0100 Subject: [PATCH 2/4] Handle parsing from Caddyfile --- upload.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/upload.go b/upload.go index 7ce996d..5bf0e5a 100644 --- a/upload.go +++ b/upload.go @@ -375,6 +375,16 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { if !d.Args(&u.MyTlsSetting.CAPath) { return d.ArgErr() } + case "create_uuid_dir": + var uuidDirStr string + if !d.AllArgs(&uuidDirStr) { + return d.ArgErr() + } + uuidDirBool, err := strconv.ParseBool(uuidDirStr) + if err != nil { + return d.Errf("parsing create_uuid_dir: %v", err) + } + u.CreateUuidDir = uuidDirBool default: return d.Errf("unrecognized servers option '%s'", d.Val()) } From 9fe7bbbc76f10b234cefb6a9b88311475980730a Mon Sep 17 00:00:00 2001 From: James Harvey <44349936+jmshrv@users.noreply.github.com> Date: Fri, 21 Apr 2023 16:06:01 +0100 Subject: [PATCH 3/4] Add documentation --- README.adoc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.adoc b/README.adoc index 02b0435..db20468 100644 --- a/README.adoc +++ b/README.adoc @@ -69,6 +69,10 @@ The default size is **1G**. |**capath** |This is the Parameter where you can define the CA filename for the **notify_url**. + +|**create_uuid_dir** +|If set to `true`, each file will get a unique directory with a UUID as its name. + |=== === JSON From 53f74896d25c83239de90d71e91a571e8b78687e Mon Sep 17 00:00:00 2001 From: James Harvey <44349936+jmshrv@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:59:31 +0100 Subject: [PATCH 4/4] Move mkdir out of CreateUuidDir block --- upload.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/upload.go b/upload.go index 5bf0e5a..55b53fa 100644 --- a/upload.go +++ b/upload.go @@ -231,15 +231,15 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp } concatDir = concatDir + "/" + uuidDir.String() + } - 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) - } + 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 @@ -296,7 +296,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp } } - return next.ServeHTTP(w, r) + w.WriteHeader(http.StatusCreated) + return nil } // UnmarshalCaddyfile implements caddyfile.Unmarshaler.