Skip to content

Commit ca7ef14

Browse files
authored
Merge pull request #10 from jmshrv/uuid-dir
Add option to create UUID directory for each file
2 parents d97d84e + 53f7489 commit ca7ef14

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ The default size is **1G**.
6969

7070
|**capath**
7171
|This is the Parameter where you can define the CA filename for the **notify_url**.
72+
73+
|**create_uuid_dir**
74+
|If set to `true`, each file will get a unique directory with a UUID as its name.
75+
7276
|===
7377

7478
=== JSON

upload.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
1313
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
1414
"github.com/dustin/go-humanize"
15+
"github.com/google/uuid"
1516
"go.uber.org/zap"
1617
)
1718

@@ -36,6 +37,7 @@ type Upload struct {
3637
ResponseTemplate string `json:"response_template,omitempty"`
3738
NotifyURL string `json:"notify_url,omitempty"`
3839
NotifyMethod string `json:"notify_method,omitempty"`
40+
CreateUuidDir bool `json:"create_uuid_dir,omitempty"`
3941

4042
MyTlsSetting struct {
4143
InsecureSkipVerify bool `json:"insecure,omitempty"`
@@ -214,9 +216,35 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
214216
}
215217
defer file.Close()
216218

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+
217245
// Create the file within the DestDir directory
218246

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)
220248

221249
if tmpf_err != nil {
222250
u.logger.Error("TempFile Error",
@@ -268,7 +296,8 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
268296
}
269297
}
270298

271-
return next.ServeHTTP(w, r)
299+
w.WriteHeader(http.StatusCreated)
300+
return nil
272301
}
273302

274303
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
@@ -347,6 +376,16 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
347376
if !d.Args(&u.MyTlsSetting.CAPath) {
348377
return d.ArgErr()
349378
}
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
350389
default:
351390
return d.Errf("unrecognized servers option '%s'", d.Val())
352391
}
@@ -358,12 +397,11 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
358397
// parseCaddyfile parses the upload directive. It enables the upload
359398
// of a file:
360399
//
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+
// }
367405
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
368406
var u Upload
369407
err := u.UnmarshalCaddyfile(h.Dispenser)

0 commit comments

Comments
 (0)