Skip to content

Commit a5fd408

Browse files
committed
Add parameter file_field_name
This commit add the possibility to define another upload field name instead of the predefined myFile
1 parent cca551d commit a5fd408

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

README.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ In this handler are the following modules in use.
2727
|**dest_dir**
2828
|The directory in which the files will be uploaded
2929

30+
|**file_field_name**
31+
|The field name for the multi-part form upload
32+
3033
|**response_template**
3134
|The response template after a upload was successfully
3235

@@ -50,7 +53,7 @@ In this handler are the following modules in use.
5053
Default is false which implies that a valid CA must be used
5154

5255
|**capath**
53-
|This is the Parameter where you can define the CA for the **notify_url**.
56+
|This is the Parameter where you can define the CA filename for the **notify_url**.
5457
|===
5558

5659
=== JSON

upload.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
Version = "0.4"
18+
Version = "0.5"
1919
)
2020

2121
func init() {
@@ -27,6 +27,7 @@ func init() {
2727
// uploaded file to a file on the disk.
2828
type Upload struct {
2929
DestDir string `json:"dest_dir,omitempty"`
30+
FileFieldName string `json:"file_field_name,omitempty"`
3031
MaxFilesize int64 `json:"max_filesize,omitempty"`
3132
MaxFormBuffer int64 `json:"max_form_buffer,omitempty"`
3233
ResponseTemplate string `json:"response_template,omitempty"`
@@ -71,6 +72,13 @@ func (u *Upload) Provision(ctx caddy.Context) error {
7172
return mdall_err
7273
}
7374

75+
if u.FileFieldName == "" {
76+
u.logger.Warn("Provision",
77+
zap.String("msg", "no FileFieldName specified (file_field_name), using the default one 'myFile'"),
78+
)
79+
u.FileFieldName = "myFile"
80+
}
81+
7482
if u.ResponseTemplate == "" {
7583
u.logger.Warn("Provision",
7684
zap.String("msg", "no ResponseTemplate specified (response_template), using the default one"),
@@ -133,10 +141,10 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
133141
return caddyhttp.Error(http.StatusRequestEntityTooLarge, max_size_err)
134142
}
135143

136-
// FormFile returns the first file for the given key `myFile`
144+
// FormFile returns the first file for the given file field key
137145
// it also returns the FileHeader so we can get the Filename,
138146
// the Header and the size of the file
139-
file, handler, ff_err := r.FormFile("myFile")
147+
file, handler, ff_err := r.FormFile(u.FileFieldName)
140148
if ff_err != nil {
141149
u.logger.Error("FormFile Error",
142150
zap.String("requuid", requuid),
@@ -216,6 +224,10 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
216224
if !d.Args(&u.DestDir) {
217225
return d.ArgErr()
218226
}
227+
case "file_field_name":
228+
if !d.Args(&u.FileFieldName) {
229+
return d.ArgErr()
230+
}
219231
case "max_form_buffer":
220232
var sizeStr string
221233
if !d.AllArgs(&sizeStr) {

0 commit comments

Comments
 (0)