save attached images past preview

This commit is contained in:
Ted Unangst 2019-08-02 10:38:42 -04:00
parent e15d81638b
commit 2ef8cc8a80
3 changed files with 67 additions and 45 deletions

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+ Attached images are not lost when previewing.
- Remove sensitivity to spicy peppers. - Remove sensitivity to spicy peppers.
+ Keep reply to setting during preview. + Keep reply to setting during preview.

107
honk.go
View File

@ -1014,59 +1014,77 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
honk.Noise = noise honk.Noise = noise
honk.Convoy = convoy honk.Convoy = convoy
file, filehdr, err := r.FormFile("donk") donkxid := r.FormValue("donkxid")
if err == nil { if donkxid == "" {
var buf bytes.Buffer file, filehdr, err := r.FormFile("donk")
io.Copy(&buf, file)
file.Close()
data := buf.Bytes()
xid := xfiltrate()
var media, name string
img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048})
if err == nil { if err == nil {
data = img.Data var buf bytes.Buffer
format := img.Format io.Copy(&buf, file)
media = "image/" + format file.Close()
if format == "jpeg" { data := buf.Bytes()
format = "jpg" xid := xfiltrate()
} var media, name string
name = xid + "." + format img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048})
xid = name if err == nil {
} else { data = img.Data
maxsize := 100000 format := img.Format
if len(data) > maxsize { media = "image/" + format
log.Printf("bad image: %s too much text: %d", err, len(data)) if format == "jpeg" {
http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType) format = "jpg"
return }
} name = xid + "." + format
for i := 0; i < len(data); i++ { xid = name
if data[i] < 32 && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' { } else {
log.Printf("bad image: %s not text: %d", err, data[i]) maxsize := 100000
if len(data) > maxsize {
log.Printf("bad image: %s too much text: %d", err, len(data))
http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType) http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType)
return return
} }
for i := 0; i < len(data); i++ {
if data[i] < 32 && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
log.Printf("bad image: %s not text: %d", err, data[i])
http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType)
return
}
}
media = "text/plain"
name = filehdr.Filename
if name == "" {
name = xid + ".txt"
}
xid += ".txt"
} }
media = "text/plain" url := fmt.Sprintf("https://%s/d/%s", serverName, xid)
name = filehdr.Filename res, err := stmtSaveFile.Exec(xid, name, url, media, 1, data)
if name == "" { if err != nil {
name = xid + ".txt" log.Printf("unable to save image: %s", err)
return
} }
xid += ".txt" var d Donk
d.FileID, _ = res.LastInsertId()
d.XID = name
d.Name = name
d.Media = media
d.URL = url
d.Local = true
honk.Donks = append(honk.Donks, &d)
donkxid = d.XID
} }
} else {
xid := donkxid
url := fmt.Sprintf("https://%s/d/%s", serverName, xid) url := fmt.Sprintf("https://%s/d/%s", serverName, xid)
res, err := stmtSaveFile.Exec(xid, name, url, media, 1, data) var donk Donk
if err != nil { row := stmtFindFile.QueryRow(url)
log.Printf("unable to save image: %s", err) err := row.Scan(&donk.FileID)
return if err == nil {
donk.XID = xid
donk.Local = true
donk.URL = url
honk.Donks = append(honk.Donks, &donk)
} else {
log.Printf("can't find file: %s", xid)
} }
var d Donk
d.FileID, _ = res.LastInsertId()
d.XID = name
d.Name = name
d.Media = media
d.URL = url
d.Local = true
honk.Donks = append(honk.Donks, &d)
} }
herd := herdofemus(honk.Noise) herd := herdofemus(honk.Noise)
for _, e := range herd { for _, e := range herd {
@ -1091,6 +1109,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
templinfo["Honks"] = honks templinfo["Honks"] = honks
templinfo["InReplyTo"] = r.FormValue("rid") templinfo["InReplyTo"] = r.FormValue("rid")
templinfo["Noise"] = r.FormValue("noise") templinfo["Noise"] = r.FormValue("noise")
templinfo["SavedFile"] = donkxid
templinfo["ServerMessage"] = "honk preview" templinfo["ServerMessage"] = "honk preview"
err := readviews.Execute(w, "honkpage.html", templinfo) err := readviews.Execute(w, "honkpage.html", templinfo)
if err != nil { if err != nil {

View File

@ -5,7 +5,8 @@
in reply to: <input type="text" name="rid" id="ridinput" value="{{ .InReplyTo }}" autocomplete=off> in reply to: <input type="text" name="rid" id="ridinput" value="{{ .InReplyTo }}" autocomplete=off>
<input type="hidden" name="CSRF" value="{{ .HonkCSRF }}"> <input type="hidden" name="CSRF" value="{{ .HonkCSRF }}">
<p> <p>
<label id="donker">attach: <input onchange="updatedonker();" type="file" name="donk"><span></span></label> <label id="donker">attach: {{ if .SavedFile }} {{ .SavedFile }} {{ else }} <input onchange="updatedonker();" type="file" name="donk"><span></span> {{ end }}</label>
<input type="hidden" name="donkxid" value="{{ .SavedFile }}">
<p> <p>
<textarea name="noise" id="honknoise">{{ .Noise }}</textarea> <textarea name="noise" id="honknoise">{{ .Noise }}</textarea>
<p> <p>