Skip to content

Added support of fixed filename #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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",
Expand All @@ -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)

Expand Down