all repos — honk @ 90e6ed010b0fadf342fa0a1976dcdfc79ef7e790

my fork of honk

svg image support
Ted Unangst tedu@tedunangst.com
Mon, 01 May 2023 15:32:36 -0400
commit

90e6ed010b0fadf342fa0a1976dcdfc79ef7e790

parent

764308c6bd46e984cee267f517e3f2f224d1f198

3 files changed, 33 insertions(+), 0 deletions(-)

jump to
M backend.gobackend.go

@@ -17,10 +17,13 @@ package main

import ( "bytes" + "errors" "net" + "net/http" "net/rpc" "os" "os/exec" + "strings" "humungus.tedunangst.com/r/webs/gate" "humungus.tedunangst.com/r/webs/image"

@@ -55,7 +58,32 @@ func backendSockname() string {

return dataDir + "/backend.sock" } +func isSVG(data []byte) bool { + ct := http.DetectContentType(data) + if strings.HasPrefix(ct, "text/xml") { + return strings.Index(string(data), "<!DOCTYPE svg PUBLIC") != -1 + } + if strings.HasPrefix(ct, "text/plain") { + return bytes.HasPrefix(data, []byte("<svg ")) + } + return ct == "image/svg+xml" +} + +func imageFromSVG(data []byte) (*image.Image, error) { + if len(data) > 100000 { + return nil, errors.New("my svg is too big") + } + svg := &image.Image{ + Data: data, + Format: "svg+xml", + } + return svg, nil +} + func shrinkit(data []byte) (*image.Image, error) { + if isSVG(data) { + return imageFromSVG(data) + } cl, err := rpc.Dial("unix", backendSockname()) if err != nil { return nil, err
M database.godatabase.go

@@ -548,6 +548,8 @@ case "image/png":

xid += ".png" case "image/jpeg": xid += ".jpg" + case "image/svg+xml": + xid += ".svg" case "application/pdf": xid += ".pdf" case "text/plain":
M web.goweb.go

@@ -1503,6 +1503,9 @@ media = "image/" + format

if format == "jpeg" { format = "jpg" } + if format == "svg+xml" { + format = "svg" + } name = xfiltrate() + "." + format } else { ct := http.DetectContentType(data)