|
@@ -1,17 +1,48 @@
|
|
|
package imagemeta
|
|
|
|
|
|
import (
|
|
|
+ "encoding/xml"
|
|
|
"io"
|
|
|
+ "sync/atomic"
|
|
|
)
|
|
|
|
|
|
-func init() {
|
|
|
- // Register fake svg decoder. Since we need this only for type detecting, we can
|
|
|
- // return fake image sizes
|
|
|
- decodeMeta := func(io.Reader) (Meta, error) {
|
|
|
- return &meta{format: "svg", width: 1, height: 1}, nil
|
|
|
+var maxSvgBytes int64 = 32 * 1024
|
|
|
+
|
|
|
+type svgHeader struct {
|
|
|
+ XMLName xml.Name
|
|
|
+}
|
|
|
+
|
|
|
+func SetMaxSvgCheckRead(n int) {
|
|
|
+ atomic.StoreInt64(&maxSvgBytes, int64(n))
|
|
|
+}
|
|
|
+
|
|
|
+func IsSVG(r io.Reader) (bool, error) {
|
|
|
+ maxBytes := int(atomic.LoadInt64(&maxSvgBytes))
|
|
|
+
|
|
|
+ var h svgHeader
|
|
|
+
|
|
|
+ buf := make([]byte, 0, maxBytes)
|
|
|
+ b := make([]byte, 1024)
|
|
|
+
|
|
|
+ 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]...)
|
|
|
+
|
|
|
+ if xml.Unmarshal(buf, &h); h.XMLName.Local == "svg" {
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(buf) >= maxBytes {
|
|
|
+ break
|
|
|
+ }
|
|
|
}
|
|
|
- RegisterFormat("<?xml ", decodeMeta)
|
|
|
- RegisterFormat("<svg", decodeMeta)
|
|
|
- // We believe that file starting with HTML comment is SVG
|
|
|
- RegisterFormat("<!--", decodeMeta)
|
|
|
+
|
|
|
+ return false, nil
|
|
|
}
|