svg image support

This commit is contained in:
Ted Unangst 2023-05-01 15:32:36 -04:00
parent 65c29304f2
commit 4e3e07d670
3 changed files with 33 additions and 0 deletions

View File

@ -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

View File

@ -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":

3
web.go
View File

@ -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)