From 4e3e07d670b3021b489448621a508047bcf3523e Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 1 May 2023 15:32:36 -0400 Subject: [PATCH] svg image support --- backend.go | 28 ++++++++++++++++++++++++++++ database.go | 2 ++ web.go | 3 +++ 3 files changed, 33 insertions(+) diff --git a/backend.go b/backend.go index 81c67a8..2a9ac02 100644 --- a/backend.go +++ b/backend.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), " 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 diff --git a/database.go b/database.go index 263bbe5..a8a80a1 100644 --- a/database.go +++ b/database.go @@ -548,6 +548,8 @@ func savefileandxid(name string, desc string, url string, media string, local bo xid += ".png" case "image/jpeg": xid += ".jpg" + case "image/svg+xml": + xid += ".svg" case "application/pdf": xid += ".pdf" case "text/plain": diff --git a/web.go b/web.go index 1d0f3be..62e2200 100644 --- a/web.go +++ b/web.go @@ -1503,6 +1503,9 @@ func submitdonk(w http.ResponseWriter, r *http.Request) (*Donk, error) { if format == "jpeg" { format = "jpg" } + if format == "svg+xml" { + format = "svg" + } name = xfiltrate() + "." + format } else { ct := http.DetectContentType(data)