mirror of
https://github.com/imgproxy/imgproxy.git
synced 2024-11-24 08:12:38 +02:00
61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package imagemeta
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
"golang.org/x/text/encoding/charmap"
|
|
)
|
|
|
|
type svgHeader struct {
|
|
XMLName xml.Name
|
|
}
|
|
|
|
func xmlCharsetReader(charset string, input io.Reader) (io.Reader, error) {
|
|
if strings.EqualFold(charset, "iso-8859-1") {
|
|
return charmap.ISO8859_1.NewDecoder().Reader(input), nil
|
|
}
|
|
return nil, fmt.Errorf("Unknown SVG charset: %s", charset)
|
|
}
|
|
|
|
func IsSVG(r io.Reader) (bool, error) {
|
|
maxBytes := config.MaxSvgCheckBytes
|
|
|
|
var h svgHeader
|
|
|
|
buf := make([]byte, 0, maxBytes)
|
|
b := make([]byte, 1024)
|
|
|
|
rr := bytes.NewReader(buf)
|
|
|
|
for {
|
|
n, err := r.Read(b)
|
|
if err != nil && err != io.EOF {
|
|
return false, err
|
|
}
|
|
if n <= 0 {
|
|
return false, nil
|
|
}
|
|
|
|
buf = append(buf, b[:n]...)
|
|
rr.Reset(buf)
|
|
|
|
dec := xml.NewDecoder(rr)
|
|
dec.Strict = false
|
|
dec.CharsetReader = xmlCharsetReader
|
|
if dec.Decode(&h); h.XMLName.Local == "svg" {
|
|
return true, nil
|
|
}
|
|
|
|
if len(buf) >= maxBytes {
|
|
break
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|