allow uploading multiple files (but beware, it gets weird fast)

This commit is contained in:
Ted Unangst 2023-08-25 01:34:48 -04:00
parent bddd8554a2
commit 6f5bf49fca
2 changed files with 26 additions and 11 deletions

View File

@ -9,7 +9,7 @@
<details> <details>
<summary>more options</summary> <summary>more options</summary>
<p> <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 }}"> <input type="hidden" id="saveddonkxid" name="donkxid" value="{{ .SavedFile }}">
<p id="donkdescriptor"><label for=donkdesc>description:</label><br> <p id="donkdescriptor"><label for=donkdesc>description:</label><br>
<input type="text" name="donkdesc" value="{{ .DonkDesc }}" autocomplete=off> <input type="text" name="donkdesc" value="{{ .DonkDesc }}" autocomplete=off>

35
web.go
View File

@ -22,6 +22,7 @@ import (
"html/template" "html/template"
"io" "io"
notrand "math/rand" notrand "math/rand"
"mime/multipart"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -1612,11 +1613,23 @@ func canedithonk(user *WhatAbout, honk *Honk) bool {
return true 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") { if !strings.HasPrefix(strings.ToLower(r.Header.Get("Content-Type")), "multipart/form-data") {
return nil, nil 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 != nil {
if err == http.ErrMissingFile { if err == http.ErrMissingFile {
return nil, nil return nil, nil
@ -1814,12 +1827,13 @@ func submithonk(w http.ResponseWriter, r *http.Request) *Honk {
donkxid := r.FormValue("donkxid") donkxid := r.FormValue("donkxid")
if donkxid == "" { if donkxid == "" {
d, err := submitdonk(w, r) donks, err := submitdonk(w, r)
if err != nil && err != http.ErrMissingFile { if err != nil && err != http.ErrMissingFile {
return nil return nil
} }
if d != nil { if len(donks) > 0 {
honk.Donks = append(honk.Donks, d) honk.Donks = append(honk.Donks, donks...)
d := donks[0]
donkxid = fmt.Sprintf("%s:%d", d.XID, d.FileID) donkxid = fmt.Sprintf("%s:%d", d.XID, d.FileID)
} }
} else { } else {
@ -1992,12 +2006,12 @@ func submitchonk(w http.ResponseWriter, r *http.Request) {
Noise: noise, Noise: noise,
Format: format, Format: format,
} }
d, err := submitdonk(w, r) donks, err := submitdonk(w, r)
if err != nil && err != http.ErrMissingFile { if err != nil && err != http.ErrMissingFile {
return return
} }
if d != nil { if len(donks) > 0 {
ch.Donks = append(ch.Donks, d) ch.Donks = append(ch.Donks, donks...)
} }
translatechonk(&ch) translatechonk(&ch)
@ -2505,15 +2519,16 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
} }
fmt.Fprintf(w, "%s", h.XID) fmt.Fprintf(w, "%s", h.XID)
case "donk": case "donk":
d, err := submitdonk(w, r) donks, err := submitdonk(w, r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
} }
if d == nil { if len(donks) == 0 {
http.Error(w, "missing donk", http.StatusBadRequest) http.Error(w, "missing donk", http.StatusBadRequest)
return return
} }
d := donks[0]
donkxid := fmt.Sprintf("%s:%d", d.XID, d.FileID) donkxid := fmt.Sprintf("%s:%d", d.XID, d.FileID)
w.Write([]byte(donkxid)) w.Write([]byte(donkxid))
case "zonkit": case "zonkit":