svg.go 505 B

123456789101112131415161718192021222324252627282930
  1. package imagemeta
  2. import (
  3. "io"
  4. "strings"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/tdewolff/parse/v2"
  7. "github.com/tdewolff/parse/v2/xml"
  8. )
  9. func IsSVG(r io.Reader) bool {
  10. maxBytes := config.MaxSvgCheckBytes
  11. l := xml.NewLexer(parse.NewInput(io.LimitReader(r, int64(maxBytes))))
  12. for {
  13. tt, _ := l.Next()
  14. switch tt {
  15. case xml.ErrorToken:
  16. return false
  17. case xml.StartTagToken:
  18. tag := strings.ToLower(string(l.Text()))
  19. return tag == "svg" || tag == "svg:svg"
  20. }
  21. }
  22. }