even if we don't save the file, save a donk

This commit is contained in:
Ted Unangst 2019-05-28 04:23:36 -04:00
parent 014ceea415
commit 2fdd553d8e
2 changed files with 39 additions and 33 deletions

View File

@ -22,7 +22,6 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"html"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -202,7 +201,10 @@ func jsongetmap(j interface{}, key string) (map[string]interface{}, bool) {
return jsonfindmap(j, []string{key}) return jsonfindmap(j, []string{key})
} }
func savedonk(url string, name, media string) *Donk { func savedonk(url string, name, media string, localize bool) *Donk {
if url == "" {
return nil
}
var donk Donk var donk Donk
row := stmtFindFile.QueryRow(url) row := stmtFindFile.QueryRow(url)
err := row.Scan(&donk.FileID) err := row.Scan(&donk.FileID)
@ -213,31 +215,37 @@ func savedonk(url string, name, media string) *Donk {
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
log.Printf("error querying: %s", err) log.Printf("error querying: %s", err)
} }
xid := xfiltrate()
data := []byte{}
if localize {
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
log.Printf("error fetching %s: %s", url, err) log.Printf("error fetching %s: %s", url, err)
return nil localize = false
goto saveit
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil localize = false
goto saveit
} }
var buf bytes.Buffer var buf bytes.Buffer
io.Copy(&buf, resp.Body) io.Copy(&buf, resp.Body)
xid := xfiltrate() data = buf.Bytes()
data := buf.Bytes()
if strings.HasPrefix(media, "image") { if strings.HasPrefix(media, "image") {
img, err := image.Vacuum(&buf) img, err := image.Vacuum(&buf)
if err != nil { if err != nil {
log.Printf("unable to decode image: %s", err) log.Printf("unable to decode image: %s", err)
return nil localize = false
goto saveit
} }
data = img.Data data = img.Data
media = "image/" + img.Format media = "image/" + img.Format
} }
res, err := stmtSaveFile.Exec(xid, name, url, media, 1, data) }
saveit:
res, err := stmtSaveFile.Exec(xid, name, url, media, localize, data)
if err != nil { if err != nil {
log.Printf("error saving file %s: %s", url, err) log.Printf("error saving file %s: %s", url, err)
return nil return nil
@ -551,6 +559,7 @@ func xonkxonk(user *WhatAbout, item interface{}, origin string) *Honk {
mt, _ := jsongetstring(att, "mediaType") mt, _ := jsongetstring(att, "mediaType")
u, _ := jsongetstring(att, "url") u, _ := jsongetstring(att, "url")
name, _ := jsongetstring(att, "name") name, _ := jsongetstring(att, "name")
localize := false
if i > 4 { if i > 4 {
log.Printf("excessive attachment: %s", at) log.Printf("excessive attachment: %s", at)
} else if at == "Document" || at == "Image" { } else if at == "Document" || at == "Image" {
@ -558,18 +567,15 @@ func xonkxonk(user *WhatAbout, item interface{}, origin string) *Honk {
log.Printf("attachment: %s %s", mt, u) log.Printf("attachment: %s %s", mt, u)
if mt == "image/jpeg" || mt == "image/png" || if mt == "image/jpeg" || mt == "image/png" ||
mt == "text/plain" { mt == "text/plain" {
donk := savedonk(u, name, mt) localize = true
if donk != nil {
xonk.Donks = append(xonk.Donks, donk)
}
} else {
u = html.EscapeString(u)
content += fmt.Sprintf(
`<p>External attachment: <a href="%s" rel=noreferrer>%s</a>`, u, u)
} }
} else { } else {
log.Printf("unknown attachment: %s", at) log.Printf("unknown attachment: %s", at)
} }
donk := savedonk(u, name, mt, localize)
if donk != nil {
xonk.Donks = append(xonk.Donks, donk)
}
} }
tags, _ := jsongetarray(obj, "tag") tags, _ := jsongetarray(obj, "tag")
for _, tag := range tags { for _, tag := range tags {
@ -582,7 +588,7 @@ func xonkxonk(user *WhatAbout, item interface{}, origin string) *Honk {
mt = "image/png" mt = "image/png"
} }
u, _ := jsongetstring(icon, "url") u, _ := jsongetstring(icon, "url")
donk := savedonk(u, name, mt) donk := savedonk(u, name, mt, true)
if donk != nil { if donk != nil {
xonk.Donks = append(xonk.Donks, donk) xonk.Donks = append(xonk.Donks, donk)
} }

View File

@ -911,7 +911,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
} }
herd := herdofemus(honk.Noise) herd := herdofemus(honk.Noise)
for _, e := range herd { for _, e := range herd {
donk := savedonk(e.ID, e.Name, "image/png") donk := savedonk(e.ID, e.Name, "image/png", true)
if donk != nil { if donk != nil {
donk.Name = e.Name donk.Name = e.Name
honk.Donks = append(honk.Donks, donk) honk.Donks = append(honk.Donks, donk)