switch to only generating xid for donks where needed

This commit is contained in:
Ted Unangst 2020-09-30 11:35:18 -04:00
parent 69fea996ad
commit 023073de03
5 changed files with 33 additions and 27 deletions

View File

@ -197,7 +197,6 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
return donk return donk
} }
log.Printf("saving donk: %s", url) log.Printf("saving donk: %s", url)
xid := xfiltrate()
data := []byte{} data := []byte{}
if localize { if localize {
fn := func() (interface{}, error) { fn := func() (interface{}, error) {
@ -223,12 +222,7 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
goto saveit goto saveit
} }
data = img.Data data = img.Data
format := img.Format media = "image/" + img.Format
media = "image/" + format
if format == "jpeg" {
format = "jpg"
}
xid = xid + "." + format
} else if media == "application/pdf" { } else if media == "application/pdf" {
if len(data) > 1000000 { if len(data) > 1000000 {
log.Printf("not saving large pdf") log.Printf("not saving large pdf")
@ -242,14 +236,13 @@ func savedonk(url string, name, desc, media string, localize bool) *Donk {
} }
} }
saveit: saveit:
fileid, err := savefile(xid, name, desc, url, media, localize, data) fileid, err := savefile(name, desc, 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
} }
donk := new(Donk) donk := new(Donk)
donk.FileID = fileid donk.FileID = fileid
donk.XID = xid
return donk return donk
} }

View File

@ -504,19 +504,39 @@ func donksforchonks(chonks []*Chonk) {
} }
} }
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) res, err := stmtSaveFile.Exec(xid, name, desc, url, media, local)
if err != nil { if err != nil {
return 0, err return 0, "", err
} }
fileid, _ := res.LastInsertId() fileid, _ := res.LastInsertId()
if local { if local {
_, err = stmtSaveFileData.Exec(xid, media, data) _, err = stmtSaveFileData.Exec(xid, media, data)
if err != nil { if err != nil {
return 0, err return 0, "", err
} }
} }
return fileid, nil return fileid, xid, nil
} }
func finddonk(url string) *Donk { func finddonk(url string) *Donk {

3
fun.go
View File

@ -420,14 +420,13 @@ func memetize(honk *Honk) {
fd.Close() fd.Close()
url := fmt.Sprintf("https://%s/meme/%s", serverName, name) 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 { if err != nil {
log.Printf("error saving meme: %s", err) log.Printf("error saving meme: %s", err)
return x return x
} }
d := &Donk{ d := &Donk{
FileID: fileid, FileID: fileid,
XID: "",
Name: name, Name: name,
Media: ct, Media: ct,
URL: url, URL: url,

View File

@ -133,7 +133,7 @@ func importMastodon(username, source string) {
name := att.Name name := att.Name
desc := name desc := name
newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) 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 { if err != nil {
log.Printf("error saving media: %s", fname) log.Printf("error saving media: %s", fname)
continue continue
@ -264,7 +264,7 @@ func importTwitter(username, source string) {
} }
newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) 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 { if err != nil {
log.Printf("error saving media: %s", fname) log.Printf("error saving media: %s", fname)
continue continue

14
web.go
View File

@ -1428,7 +1428,6 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
io.Copy(&buf, file) io.Copy(&buf, file)
file.Close() file.Close()
data := buf.Bytes() data := buf.Bytes()
xid := xfiltrate()
var media, name string var media, name string
img, err := shrinkit(data) img, err := shrinkit(data)
if err == nil { if err == nil {
@ -1438,8 +1437,7 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
if format == "jpeg" { if format == "jpeg" {
format = "jpg" format = "jpg"
} }
name = xid + "." + format name = xfiltrate() + "." + format
xid = name
} else { } else {
ct := http.DetectContentType(data) ct := http.DetectContentType(data)
switch ct { switch ct {
@ -1451,10 +1449,9 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
return nil, err return nil, err
} }
media = ct media = ct
xid += ".pdf"
name = filehdr.Filename name = filehdr.Filename
if name == "" { if name == "" {
name = xid name = xfiltrate() + ".pdf"
} }
default: default:
maxsize := 100000 maxsize := 100000
@ -1471,10 +1468,9 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
} }
} }
media = "text/plain" media = "text/plain"
xid += ".txt"
name = filehdr.Filename name = filehdr.Filename
if name == "" { if name == "" {
name = xid name = xfiltrate() + ".txt"
} }
} }
} }
@ -1482,8 +1478,7 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
if desc == "" { if desc == "" {
desc = name desc = name
} }
url := fmt.Sprintf("https://%s/d/%s", serverName, xid) fileid, xid, err := savefileandxid(name, desc, "", media, true, data)
fileid, err := savefile(xid, name, desc, url, media, true, data)
if err != nil { if err != nil {
log.Printf("unable to save image: %s", err) log.Printf("unable to save image: %s", err)
http.Error(w, "failed to save attachment", http.StatusUnsupportedMediaType) http.Error(w, "failed to save attachment", http.StatusUnsupportedMediaType)
@ -1493,7 +1488,6 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) {
FileID: fileid, FileID: fileid,
XID: xid, XID: xid,
Desc: desc, Desc: desc,
URL: url,
Local: true, Local: true,
} }
return d, nil return d, nil