From 99f0e73394dc9a5f5525af1256ead174b90f4463 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Wed, 2 Oct 2019 23:02:01 -0400 Subject: [PATCH] support inline images in received posts --- activity.go | 21 ++++++++++++--------- database.go | 15 ++++++++++++++- docs/changelog.txt | 2 ++ fun.go | 24 ++++++++++++++++++++++++ web.go | 8 +++----- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/activity.go b/activity.go index 2cb66c9..3b810fb 100644 --- a/activity.go +++ b/activity.go @@ -30,6 +30,7 @@ import ( "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 @@ saveit: return nil } donk.FileID = fileid - return &donk + donk.XID = xid + return donk } func iszonked(userid int64, xid string) bool { @@ -765,6 +763,11 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { currenttid = convoy } + // grab any inline imgs + imgfilt := htfilter.New() + imgfilt.Imager = inlineimgs + imgfilt.String(content) + // init xonk xonk.What = what xonk.XID = xid diff --git a/database.go b/database.go index 70cbfe8..86cb946 100644 --- a/database.go +++ b/database.go @@ -309,6 +309,19 @@ func savefile(xid string, name string, desc string, url string, media string, lo 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 @@ func prepareStatements(db *sql.DB) { 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 (?, ?, ?, ?, ?)") diff --git a/docs/changelog.txt b/docs/changelog.txt index 19a78b7..205a96a 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -2,6 +2,8 @@ changelog -- next ++ Inline images in received posts. + + Times for events. + Split media database into separate blob.db. diff --git a/fun.go b/fun.go index abe824e..ed9415d 100644 --- a/fun.go +++ b/fun.go @@ -120,9 +120,33 @@ func replaceimg(node *html.Node) string { } 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(`%s`, alt, alt, src) + } return fmt.Sprintf(`<img alt="%s" src="%s">`, 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(`