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>
<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>

35
web.go
View File

@ -22,6 +22,7 @@ import (
"html/template"
"io"
notrand "math/rand"
"mime/multipart"
"net/http"
"net/url"
"os"
@ -1612,11 +1613,23 @@ func canedithonk(user *WhatAbout, honk *Honk) bool {
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 @@ func submithonk(w http.ResponseWriter, r *http.Request) *Honk {
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 @@ func submitchonk(w http.ResponseWriter, r *http.Request) {
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 @@ func apihandler(w http.ResponseWriter, r *http.Request) {
}
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":