support inline images in received posts
Ted Unangst tedu@tedunangst.com
Wed, 02 Oct 2019 23:02:01 -0400
5 files changed,
55 insertions(+),
15 deletions(-)
M
activity.go
→
activity.go
@@ -30,6 +30,7 @@ "strings"
"sync" "time" + "humungus.tedunangst.com/r/webs/htfilter" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/junk"@@ -127,16 +128,12 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
if url == "" { return nil } - var donk Donk - row := stmtFindFile.QueryRow(url) - err := row.Scan(&donk.FileID) - if err == nil { - return &donk + donk := finddonk(url) + if donk != nil { + return donk } + donk = new(Donk) log.Printf("saving donk: %s", url) - if err != nil && err != sql.ErrNoRows { - log.Printf("error querying: %s", err) - } xid := xfiltrate() data := []byte{} if localize {@@ -184,7 +181,8 @@ log.Printf("error saving file %s: %s", url, err)
return nil } donk.FileID = fileid - return &donk + donk.XID = xid + return donk } func iszonked(userid int64, xid string) bool {@@ -764,6 +762,11 @@
if currenttid == "" { currenttid = convoy } + + // grab any inline imgs + imgfilt := htfilter.New() + imgfilt.Imager = inlineimgs + imgfilt.String(content) // init xonk xonk.What = what
M
database.go
→
database.go
@@ -309,6 +309,19 @@ }
return fileid, nil } +func finddonk(url string) *Donk { + donk := new(Donk) + row := stmtFindFile.QueryRow(url) + err := row.Scan(&donk.FileID, &donk.XID) + if err == nil { + return donk + } + if err != sql.ErrNoRows { + log.Printf("error finding file: %s", err) + } + return nil +} + func savehonk(h *Honk) error { dt := h.Date.UTC().Format(dbtimeformat) aud := strings.Join(h.Audience, " ")@@ -550,7 +563,7 @@ blobdb := openblobdb()
stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, content) values (?, ?, ?)") stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") - stmtFindFile = preparetodie(db, "select fileid from filemeta where url = ? and local = 1") + stmtFindFile = preparetodie(db, "select fileid, xid from filemeta where url = ? and local = 1") stmtWhatAbout = preparetodie(db, "select userid, username, displayname, about, pubkey, options from users where username = ?") stmtSaveDub = preparetodie(db, "insert into honkers (userid, name, xid, flavor) values (?, ?, ?, ?)") stmtAddDoover = preparetodie(db, "insert into doovers (dt, tries, username, rcpt, msg) values (?, ?, ?, ?, ?)")
M
docs/changelog.txt
→
docs/changelog.txt
@@ -2,6 +2,8 @@ changelog
-- next ++ Inline images in received posts. + + Times for events. + Split media database into separate blob.db.
M
fun.go
→
fun.go
@@ -120,7 +120,31 @@ return alt
} alt = html.EscapeString(alt) src = html.EscapeString(src) + d := finddonk(src) + if d != nil { + src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID) + return fmt.Sprintf(`<img alt="%s" title="%s" src="%s">`, alt, alt, src) + } return fmt.Sprintf(`<img alt="%s" src="<a href="%s">%s<a>">`, alt, src, src) +} + +func inlineimgs(node *html.Node) string { + src := htfilter.GetAttr(node, "src") + alt := htfilter.GetAttr(node, "alt") + //title := GetAttr(node, "title") + if htfilter.HasClass(node, "Emoji") && alt != "" { + return alt + } + alt = html.EscapeString(alt) + src = html.EscapeString(src) + if !strings.HasPrefix(src, "https://"+serverName+"/") { + d := savedonk(src, "image", alt, "image", true) + if d != nil { + src = fmt.Sprintf("https://%s/d/%s", serverName, d.XID) + } + } + log.Printf("inline img with src: %s", src) + return fmt.Sprintf(`<img alt="%s" title="%s" src="%s>`, alt, alt, src) } func translate(honk *Honk) {
M
web.go
→
web.go
@@ -1022,11 +1022,9 @@ }
} else { xid := donkxid url := fmt.Sprintf("https://%s/d/%s", serverName, xid) - var donk Donk - row := stmtFindFile.QueryRow(url) - err := row.Scan(&donk.FileID) - if err == nil { - honk.Donks = append(honk.Donks, &donk) + donk := finddonk(url) + if donk != nil { + honk.Donks = append(honk.Donks, donk) } else { log.Printf("can't find file: %s", xid) }