all repos — honk @ 96324e8c03337595bd491644bd316a7a8a92f84f

my fork of honk

switch to only generating xid for donks where needed
Ted Unangst tedu@tedunangst.com
Wed, 30 Sep 2020 11:35:18 -0400
commit

96324e8c03337595bd491644bd316a7a8a92f84f

parent

5e42067c8b5faa9837f1a9788646e5103e1d91c8

5 files changed, 33 insertions(+), 27 deletions(-)

jump to
M activity.goactivity.go

@@ -197,7 +197,6 @@ if donk := finddonk(url); donk != nil {

return donk } log.Printf("saving donk: %s", url) - xid := xfiltrate() data := []byte{} if localize { fn := func() (interface{}, error) {

@@ -223,12 +222,7 @@ data = []byte{}

goto saveit } data = img.Data - format := img.Format - media = "image/" + format - if format == "jpeg" { - format = "jpg" - } - xid = xid + "." + format + media = "image/" + img.Format } else if media == "application/pdf" { if len(data) > 1000000 { log.Printf("not saving large pdf")

@@ -242,14 +236,13 @@ data = []byte{}

} } saveit: - fileid, err := savefile(xid, name, desc, url, media, localize, data) + fileid, err := savefile(name, desc, url, media, localize, data) if err != nil { log.Printf("error saving file %s: %s", url, err) return nil } donk := new(Donk) donk.FileID = fileid - donk.XID = xid return donk }
M database.godatabase.go

@@ -504,19 +504,39 @@ ch.Donks = append(ch.Donks, d)

} } -func savefile(xid string, name string, desc string, url string, media string, local bool, data []byte) (int64, error) { +func savefile(name string, desc string, url string, media string, local bool, data []byte) (int64, error) { + fileid, _, err := savefileandxid(name, desc, url, media, local, data) + return fileid, err +} + +func savefileandxid(name string, desc string, url string, media string, local bool, data []byte) (int64, string, error) { + xid := xfiltrate() + switch media { + case "image/png": + xid += ".png" + case "image/jpeg": + xid += ".jpg" + case "application/pdf": + xid += ".pdf" + case "text/plain": + xid += ".txt" + } + if url == "" { + url = fmt.Sprintf("https://%s/d/%s", serverName, xid) + } + res, err := stmtSaveFile.Exec(xid, name, desc, url, media, local) if err != nil { - return 0, err + return 0, "", err } fileid, _ := res.LastInsertId() if local { _, err = stmtSaveFileData.Exec(xid, media, data) if err != nil { - return 0, err + return 0, "", err } } - return fileid, nil + return fileid, xid, nil } func finddonk(url string) *Donk {
M fun.gofun.go

@@ -420,14 +420,13 @@ ct := http.DetectContentType(peek[:n])

fd.Close() url := fmt.Sprintf("https://%s/meme/%s", serverName, name) - fileid, err := savefile("", name, name, url, ct, false, nil) + fileid, err := savefile(name, name, url, ct, false, nil) if err != nil { log.Printf("error saving meme: %s", err) return x } d := &Donk{ FileID: fileid, - XID: "", Name: name, Media: ct, URL: url,
M import.goimport.go

@@ -133,7 +133,7 @@ u := xfiltrate()

name := att.Name desc := name newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) - fileid, err := savefile(u, name, desc, newurl, att.MediaType, true, data) + fileid, err := savefile(name, desc, newurl, att.MediaType, true, data) if err != nil { log.Printf("error saving media: %s", fname) continue

@@ -264,7 +264,7 @@ continue

} newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) - fileid, err := savefile(u, u, u, newurl, "image/jpg", true, data) + fileid, err := savefile(u, u, newurl, "image/jpg", true, data) if err != nil { log.Printf("error saving media: %s", fname) continue
M web.goweb.go

@@ -1428,7 +1428,6 @@ var buf bytes.Buffer

io.Copy(&buf, file) file.Close() data := buf.Bytes() - xid := xfiltrate() var media, name string img, err := shrinkit(data) if err == nil {

@@ -1438,8 +1437,7 @@ media = "image/" + format

if format == "jpeg" { format = "jpg" } - name = xid + "." + format - xid = name + name = xfiltrate() + "." + format } else { ct := http.DetectContentType(data) switch ct {

@@ -1451,10 +1449,9 @@ http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType)

return nil, err } media = ct - xid += ".pdf" name = filehdr.Filename if name == "" { - name = xid + name = xfiltrate() + ".pdf" } default: maxsize := 100000

@@ -1471,10 +1468,9 @@ return nil, err

} } media = "text/plain" - xid += ".txt" name = filehdr.Filename if name == "" { - name = xid + name = xfiltrate() + ".txt" } } }

@@ -1482,8 +1478,7 @@ desc := strings.TrimSpace(r.FormValue("donkdesc"))

if desc == "" { desc = name } - url := fmt.Sprintf("https://%s/d/%s", serverName, xid) - fileid, err := savefile(xid, name, desc, url, media, true, data) + fileid, xid, err := savefileandxid(name, desc, "", media, true, data) if err != nil { log.Printf("unable to save image: %s", err) http.Error(w, "failed to save attachment", http.StatusUnsupportedMediaType)

@@ -1493,7 +1488,6 @@ d := &Donk{

FileID: fileid, XID: xid, Desc: desc, - URL: url, Local: true, } return d, nil