save some meta data (w, h, size) for donks
@@ -285,6 +285,7 @@ return donk
} ilog.Printf("saving donk: %s", url) data := []byte{} + var meta DonkMeta if localize { fn := func() (interface{}, error) { return fetchsome(url)@@ -309,6 +310,8 @@ data = []byte{}
goto saveit } data = img.Data + meta.Width = img.Width + meta.Height = img.Height media = "image/" + img.Format } else if media == "application/pdf" { if len(data) > 1000000 {@@ -321,9 +324,10 @@ ilog.Printf("not saving large attachment")
localize = false data = []byte{} } + meta.Length = len(data) } saveit: - fileid, err := savefile(name, desc, url, media, localize, data) + fileid, err := savefile(name, desc, url, media, localize, data, &meta) if err != nil { elog.Printf("error saving file %s: %s", url, err) return nil
@@ -437,7 +437,7 @@ hmap[h.ID] = h
} idset := strings.Join(ids, ",") // grab donks - q := fmt.Sprintf("select honkid, donks.fileid, xid, name, description, url, media, local from donks join filemeta on donks.fileid = filemeta.fileid where honkid in (%s)", idset) + q := fmt.Sprintf("select honkid, donks.fileid, xid, name, description, url, media, local, meta from donks join filemeta on donks.fileid = filemeta.fileid where honkid in (%s)", idset) rows, err := db.Query(q) if err != nil { elog.Printf("error querying donks: %s", err)@@ -446,12 +446,14 @@ }
defer rows.Close() for rows.Next() { var hid int64 + var j string d := new(Donk) - err = rows.Scan(&hid, &d.FileID, &d.XID, &d.Name, &d.Desc, &d.URL, &d.Media, &d.Local) + err = rows.Scan(&hid, &d.FileID, &d.XID, &d.Name, &d.Desc, &d.URL, &d.Media, &d.Local, &j) if err != nil { elog.Printf("error scanning donk: %s", err) continue } + unjsonify(j, &d.Meta) d.External = !strings.HasPrefix(d.URL, serverPrefix) h := hmap[hid] h.Donks = append(h.Donks, d)@@ -571,8 +573,8 @@ ch.Donks = append(ch.Donks, d)
} } -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) +func savefile(name string, desc string, url string, media string, local bool, data []byte, meta *DonkMeta) (int64, error) { + fileid, _, err := savefileandxid(name, desc, url, media, local, data, meta) return fileid, err }@@ -582,7 +584,7 @@ h.Write(data)
return fmt.Sprintf("%x", h.Sum(nil)) } -func savefileandxid(name string, desc string, url string, media string, local bool, data []byte) (int64, string, error) { +func savefileandxid(name string, desc string, url string, media string, local bool, data []byte, meta *DonkMeta) (int64, string, error) { var xid string if local { hash := hashfiledata(data)@@ -615,7 +617,11 @@ url = serverURL("/d/%s", xid)
} } - res, err := stmtSaveFile.Exec(xid, name, desc, url, media, local) + j := "{}" + if meta != nil { + j, _ = jsonify(meta) + } + res, err := stmtSaveFile.Exec(xid, name, desc, url, media, local, j) if err != nil { return 0, "", err }@@ -1300,7 +1306,7 @@ stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)")
stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, chonkid, fileid) values (?, ?, ?)") stmtDeleteDonks = preparetodie(db, "delete from donks where honkid = ?") - stmtSaveFile = preparetodie(db, "insert into filemeta (xid, name, description, url, media, local) values (?, ?, ?, ?, ?, ?)") + stmtSaveFile = preparetodie(db, "insert into filemeta (xid, name, description, url, media, local, meta) values (?, ?, ?, ?, ?, ?, ?)") g_blobdb = openblobdb() stmtSaveFileData = preparetodie(g_blobdb, "insert into filedata (xid, media, hash, content) values (?, ?, ?, ?)") stmtCheckFileData = preparetodie(g_blobdb, "select xid from filedata where hash = ?")
@@ -504,7 +504,7 @@ ct := http.DetectContentType(peek[:n])
fd.Close() url := serverURL("/meme/%s", name) - fileid, err := savefile(name, name, url, ct, false, nil) + fileid, err := savefile(name, name, url, ct, false, nil, nil) if err != nil { elog.Printf("error saving meme: %s", err) return x
@@ -203,6 +203,7 @@ if !loudandproud(honk.Audience) {
honk.Whofore = 3 } for _, att := range toot.Object.Attachment { + var meta DonkMeta switch att.Type { case "Document": fname := fmt.Sprintf("%s/%s", source, att.Url)@@ -215,7 +216,7 @@ u := xfiltrate()
name := att.Name desc := name newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) - fileid, err := savefile(name, desc, newurl, att.MediaType, true, data) + fileid, err := savefile(name, desc, newurl, att.MediaType, true, data, &meta) if err != nil { elog.Printf("error saving media: %s", fname) continue@@ -458,6 +459,7 @@ for _, r := range t.Tweet.Entities.Urls {
noise = strings.Replace(noise, r.URL, r.ExpandedURL, -1) } for _, m := range t.Tweet.Entities.Media { + var meta DonkMeta u := m.MediaURL idx := strings.LastIndexByte(u, '/') u = u[idx+1:]@@ -469,7 +471,7 @@ continue
} newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) - fileid, err := savefile(u, u, newurl, "image/jpg", true, data) + fileid, err := savefile(u, u, newurl, "image/jpg", true, data, &meta) if err != nil { elog.Printf("error saving media: %s", fname) continue@@ -540,6 +542,7 @@ Public: true,
Whofore: 2, } { + var meta DonkMeta u := xfiltrate() fname := fmt.Sprintf("%s/%s", source, g.URI) data, err := ioutil.ReadFile(fname)@@ -549,7 +552,7 @@ continue
} newurl := fmt.Sprintf("https://%s/d/%s", serverName, u) - fileid, err := savefile(u, u, newurl, "image/jpg", true, data) + fileid, err := savefile(u, u, newurl, "image/jpg", true, data, &meta) if err != nil { elog.Printf("error saving media: %s", fname) continue
@@ -1,7 +1,7 @@
create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer, plain text); create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text); create table donks (honkid integer, chonkid integer, fileid integer); -create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer); +create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer, meta text); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, owner text, meta text, folxid text); create table xonkers (xonkerid integer primary key, name text, info text, flavor text, dt text); create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text);
@@ -23,7 +23,7 @@
"humungus.tedunangst.com/r/webs/htfilter" ) -var myVersion = 50 // index honks rid +var myVersion = 51 // filemeta.meta type dbexecer interface { Exec(query string, args ...interface{}) (sql.Result, error)@@ -218,6 +218,11 @@ try("create index idx_honksrid on honks(rid) where rid <> ''")
setV(50) fallthrough case 50: + try("alter table filemeta add column meta text") + try("update filemeta set meta = '{}'") + setV(51) + fallthrough + case 51: try("analyze") closedatabases()
@@ -68,14 +68,14 @@ {{ end }}
{{ range .Donks }} {{ if .Local }} {{ if eq .Media "text/plain" }} -<p><a href="/d/{{ .XID }}">Attachment: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }}</p> +<p><a href="/d/{{ .XID }}">Attachment: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }} ({{ .Meta.Length }})</p> {{ else if eq .Media "application/pdf" }} -<p><a href="/d/{{ .XID }}">Attachment: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }}</p> +<p><a href="/d/{{ .XID }}">Attachment: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }} ({{ .Meta.Length }})</p> {{ else }} {{ if $omitimages }} -<p><a href="/d/{{ .XID }}">Image: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }}</p> +<p><a href="/d/{{ .XID }}">Image: {{ .Name }}</a>{{ if not (eq .Desc .Name) }} {{ .Desc }}{{ end }} ({{.Meta.Width}}x{{.Meta.Height}} {{ .Meta.Length }})</p> {{ else }} -<img class="donk donklink" src="/d/{{ .XID }}" loading=lazy title="{{ .Desc }}" alt="{{ .Desc }}"> +<img class="donk donklink" src="/d/{{ .XID }}" loading=lazy title="{{ .Desc }}" alt="{{ .Desc }}" width="{{.Meta.Width}}" height="{{.Meta.Height}}"> {{ end }} {{ end }} {{ else }}
@@ -337,6 +337,9 @@ background: var(--bg-page);
} img, video { max-width: 100%; + object-fit: scale-down; + width: auto; + height: auto; } .noise img:not(.emu) { display: block;@@ -349,7 +352,6 @@ }
img.emu { height: 2em; vertical-align: middle; - object-fit: contain; } .nophone { position: fixed;
@@ -1847,9 +1847,12 @@ io.Copy(&buf, file)
file.Close() data := buf.Bytes() var media, name string + var donkmeta DonkMeta img, err := bigshrink(data) if err == nil { data = img.Data + donkmeta.Width = img.Width + donkmeta.Height = img.Height format := img.Format media = "image/" + format if format == "jpeg" {@@ -1895,11 +1898,12 @@ name = xfiltrate() + ".txt"
} } } + donkmeta.Length = len(data) desc := strings.TrimSpace(r.FormValue("donkdesc")) if desc == "" { desc = name } - fileid, xid, err := savefileandxid(name, desc, "", media, true, data) + fileid, xid, err := savefileandxid(name, desc, "", media, true, data, &donkmeta) if err != nil { elog.Printf("unable to save image: %s", err) http.Error(w, "failed to save attachment", http.StatusUnsupportedMediaType)