support inline images in received posts

This commit is contained in:
Ted Unangst 2019-10-02 23:02:01 -04:00
parent 81812030a8
commit 99f0e73394
5 changed files with 55 additions and 15 deletions

View File

@ -30,6 +30,7 @@ import (
"sync" "sync"
"time" "time"
"humungus.tedunangst.com/r/webs/htfilter"
"humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/httpsig"
"humungus.tedunangst.com/r/webs/image" "humungus.tedunangst.com/r/webs/image"
"humungus.tedunangst.com/r/webs/junk" "humungus.tedunangst.com/r/webs/junk"
@ -127,16 +128,12 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
if url == "" { if url == "" {
return nil return nil
} }
var donk Donk donk := finddonk(url)
row := stmtFindFile.QueryRow(url) if donk != nil {
err := row.Scan(&donk.FileID) return donk
if err == nil {
return &donk
} }
donk = new(Donk)
log.Printf("saving donk: %s", url) log.Printf("saving donk: %s", url)
if err != nil && err != sql.ErrNoRows {
log.Printf("error querying: %s", err)
}
xid := xfiltrate() xid := xfiltrate()
data := []byte{} data := []byte{}
if localize { if localize {
@ -184,7 +181,8 @@ saveit:
return nil return nil
} }
donk.FileID = fileid donk.FileID = fileid
return &donk donk.XID = xid
return donk
} }
func iszonked(userid int64, xid string) bool { func iszonked(userid int64, xid string) bool {
@ -765,6 +763,11 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
currenttid = convoy currenttid = convoy
} }
// grab any inline imgs
imgfilt := htfilter.New()
imgfilt.Imager = inlineimgs
imgfilt.String(content)
// init xonk // init xonk
xonk.What = what xonk.What = what
xonk.XID = xid xonk.XID = xid

View File

@ -309,6 +309,19 @@ func savefile(xid string, name string, desc string, url string, media string, lo
return fileid, nil 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 { func savehonk(h *Honk) error {
dt := h.Date.UTC().Format(dbtimeformat) dt := h.Date.UTC().Format(dbtimeformat)
aud := strings.Join(h.Audience, " ") aud := strings.Join(h.Audience, " ")
@ -550,7 +563,7 @@ func prepareStatements(db *sql.DB) {
stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, content) values (?, ?, ?)") stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, content) values (?, ?, ?)")
stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?") stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?")
stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and 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 = ?") 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 (?, ?, ?, ?)") stmtSaveDub = preparetodie(db, "insert into honkers (userid, name, xid, flavor) values (?, ?, ?, ?)")
stmtAddDoover = preparetodie(db, "insert into doovers (dt, tries, username, rcpt, msg) values (?, ?, ?, ?, ?)") stmtAddDoover = preparetodie(db, "insert into doovers (dt, tries, username, rcpt, msg) values (?, ?, ?, ?, ?)")

View File

@ -2,6 +2,8 @@ changelog
-- next -- next
+ Inline images in received posts.
+ Times for events. + Times for events.
+ Split media database into separate blob.db. + Split media database into separate blob.db.

24
fun.go
View File

@ -120,9 +120,33 @@ func replaceimg(node *html.Node) string {
} }
alt = html.EscapeString(alt) alt = html.EscapeString(alt)
src = html.EscapeString(src) 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(`&lt;img alt="%s" src="<a href="%s">%s<a>"&gt;`, alt, src, src) return fmt.Sprintf(`&lt;img alt="%s" src="<a href="%s">%s<a>"&gt;`, 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) { func translate(honk *Honk) {
if honk.Format == "html" { if honk.Format == "html" {
return return

8
web.go
View File

@ -1022,11 +1022,9 @@ func submithonk(w http.ResponseWriter, r *http.Request) {
} else { } else {
xid := donkxid xid := donkxid
url := fmt.Sprintf("https://%s/d/%s", serverName, xid) url := fmt.Sprintf("https://%s/d/%s", serverName, xid)
var donk Donk donk := finddonk(url)
row := stmtFindFile.QueryRow(url) if donk != nil {
err := row.Scan(&donk.FileID) honk.Donks = append(honk.Donks, donk)
if err == nil {
honk.Donks = append(honk.Donks, &donk)
} else { } else {
log.Printf("can't find file: %s", xid) log.Printf("can't find file: %s", xid)
} }