diff --git a/README.adoc b/README.adoc index 30fbe39..91728ff 100644 --- a/README.adoc +++ b/README.adoc @@ -37,6 +37,9 @@ directory be evaluated at runtime from this variable `http.vars.root`. |**file_field_name** |The field name for the multi-part form upload +|**fixed_file_name** +|When set, all uploaded files will be renamed to the provided file name. + |**response_template** |The response template after a upload was successfully diff --git a/caddyfile.go b/caddyfile.go index b6f462a..08d38e5 100644 --- a/caddyfile.go +++ b/caddyfile.go @@ -28,6 +28,10 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { if !d.Args(&u.FileFieldName) { return d.ArgErr() } + case "fixed_file_name": + if !d.Args(&u.FixedFileName) { + return d.ArgErr() + } case "max_form_buffer": var sizeStr string if !d.AllArgs(&sizeStr) { diff --git a/upload.go b/upload.go index 9232d7e..e1efbe1 100644 --- a/upload.go +++ b/upload.go @@ -30,6 +30,7 @@ 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"` @@ -83,6 +84,11 @@ func (u *Upload) Provision(ctx caddy.Context) error { 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"), @@ -154,6 +160,7 @@ func (u *Upload) Provision(ctx caddy.Context) error { 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), @@ -261,7 +268,12 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp // Create the file within the DestDir directory - tempFile, tmpf_err := os.OpenFile(caddyhttp.SanitizedPathJoin(concatDir, handler.Filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) + 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", @@ -287,14 +299,14 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp u.logger.Info("Successful Upload Info", zap.String("requuid", requuid), - zap.String("Uploaded File", handler.Filename), + 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", handler.Filename) + repl.Set("http.upload.filename", filename) repl.Set("http.upload.filesize", handler.Size) repl.Set("http.upload.directory", concatDir)