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

3
fun.go
View File

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

View File

@ -133,7 +133,7 @@ func importMastodon(username, source string) {
name := att.Name
desc := name
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 {
log.Printf("error saving media: %s", fname)
continue
@ -264,7 +264,7 @@ func importTwitter(username, source string) {
}
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 {
log.Printf("error saving media: %s", fname)
continue

14
web.go
View File

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