Skip to content

Commit 256822c

Browse files
authored
add support for dest_dir_field_name (#22)
1 parent 29f64ee commit 256822c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

README.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ The default size is **1G**.
9999
|**create_uuid_dir**
100100
|If set to `true`, each file will get a unique directory with a UUID as its name.
101101

102+
|**dest_dir_field_name**
103+
|If set, the dest_dir will be set from the specified form value.
104+
102105
|===
103106

104107
=== Caddy Variables

caddyfile.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func (u *Upload) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
103103
return d.Errf("parsing create_uuid_dir: %v", err)
104104
}
105105
u.CreateUuidDir = uuidDirBool
106+
case "dest_dir_field_name":
107+
if !d.Args(&u.DestDirFieldName) {
108+
return d.ArgErr()
109+
}
106110
default:
107111
return d.Errf("unrecognized servers option '%s'", d.Val())
108112
}

upload.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Upload struct {
3939
NotifyURL string `json:"notify_url,omitempty"`
4040
NotifyMethod string `json:"notify_method,omitempty"`
4141
CreateUuidDir bool `json:"create_uuid_dir,omitempty"`
42+
DestDirFieldName string `json:"dest_dir_field_name,omitempty"`
4243

4344
MyTlsSetting struct {
4445
InsecureSkipVerify bool `json:"insecure,omitempty"`
@@ -66,10 +67,10 @@ func (u *Upload) Provision(ctx caddy.Context) error {
6667

6768
repl := caddy.NewReplacer()
6869

69-
if u.DestDir == "" {
70+
if u.DestDir == "" && u.DestDirFieldName == "" {
7071
u.logger.Error("Provision",
71-
zap.String("msg", "no Destination Directory specified (dest_dir)"))
72-
return fmt.Errorf("no Destination Directory specified (dest_dir)")
72+
zap.String("msg", "no Destination Directory specified (dest_dir or dest_dir_field_name)"))
73+
return fmt.Errorf("no Destination Directory specified (dest_dir or dest_dir_field_name)")
7374
}
7475

7576
if u.RootDir == "" {
@@ -171,6 +172,7 @@ func (u *Upload) Provision(ctx caddy.Context) error {
171172
zap.Bool("CreateUuidDir", u.CreateUuidDir),
172173
zap.String("capath", u.MyTlsSetting.CAPath),
173174
zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify),
175+
zap.String("dest_dir_field_name", u.DestDirFieldName),
174176
)
175177

176178
return nil
@@ -239,6 +241,10 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
239241
}
240242
}
241243

244+
if u.DestDirFieldName != "" {
245+
u.DestDir = r.FormValue(u.DestDirFieldName)
246+
}
247+
242248
concatDir := caddyhttp.SanitizedPathJoin(u.RootDir, u.DestDir)
243249

244250
if u.CreateUuidDir {

0 commit comments

Comments
 (0)