allow uploading multiple files (but beware, it gets weird fast)
Ted Unangst tedu@tedunangst.com
Fri, 25 Aug 2023 01:34:48 -0400
2 files changed,
26 insertions(+),
11 deletions(-)
M
views/honkform.html
→
views/honkform.html
@@ -9,7 +9,7 @@ <p>
<details> <summary>more options</summary> <p> -<label class=button id="donker">attach: <input type="file" name="donk"><span>{{ .SavedFile }}</span></label> +<label class=button id="donker">attach: <input type="file" multiple name="donk"><span>{{ .SavedFile }}</span></label> <input type="hidden" id="saveddonkxid" name="donkxid" value="{{ .SavedFile }}"> <p id="donkdescriptor"><label for=donkdesc>description:</label><br> <input type="text" name="donkdesc" value="{{ .DonkDesc }}" autocomplete=off>
M
web.go
→
web.go
@@ -22,6 +22,7 @@ "fmt"
"html/template" "io" notrand "math/rand" + "mime/multipart" "net/http" "net/url" "os"@@ -1612,11 +1613,23 @@ }
return true } -func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) { +func submitdonk(w http.ResponseWriter, r *http.Request) ([]*Donk, error) { if !strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "multipart/form-data") { return nil, nil } - file, filehdr, err := r.FormFile("donk") + var donks []*Donk + for _, hdr := range r.MultipartForm.File["donk"] { + donk, err := formtodonk(w, r, hdr) + if err != nil { + return nil, err + } + donks = append(donks, donk) + } + return donks, nil +} + +func formtodonk(w http.ResponseWriter, r *http.Request, filehdr *multipart.FileHeader) (*Donk, error) { + file, err := filehdr.Open() if err != nil { if err == http.ErrMissingFile { return nil, nil@@ -1814,12 +1827,13 @@ honk.Convoy = convoy
donkxid := r.FormValue("donkxid") if donkxid == "" { - d, err := submitdonk(w, r) + donks, err := submitdonk(w, r) if err != nil && err != http.ErrMissingFile { return nil } - if d != nil { - honk.Donks = append(honk.Donks, d) + if len(donks) > 0 { + honk.Donks = append(honk.Donks, donks...) + d := donks[0] donkxid = fmt.Sprintf("%s:%d", d.XID, d.FileID) } } else {@@ -1992,12 +2006,12 @@ Date: dt,
Noise: noise, Format: format, } - d, err := submitdonk(w, r) + donks, err := submitdonk(w, r) if err != nil && err != http.ErrMissingFile { return } - if d != nil { - ch.Donks = append(ch.Donks, d) + if len(donks) > 0 { + ch.Donks = append(ch.Donks, donks...) } translatechonk(&ch)@@ -2505,15 +2519,16 @@ return
} fmt.Fprintf(w, "%s", h.XID) case "donk": - d, err := submitdonk(w, r) + donks, err := submitdonk(w, r) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } - if d == nil { + if len(donks) == 0 { http.Error(w, "missing donk", http.StatusBadRequest) return } + d := donks[0] donkxid := fmt.Sprintf("%s:%d", d.XID, d.FileID) w.Write([]byte(donkxid)) case "zonkit":