HasSVGRootElement reports whether the provided file bytes decode to an SVG root element. This catches SVG content even when generic sniffers classify it as text or XML.
(data []byte)
| 169 | // root element. This catches SVG content even when generic sniffers classify it |
| 170 | // as text or XML. |
| 171 | func HasSVGRootElement(data []byte) bool { |
| 172 | data = bytes.TrimPrefix(data, utf8BOM) |
| 173 | if len(data) == 0 { |
| 174 | return false |
| 175 | } |
| 176 | |
| 177 | decoder := xml.NewDecoder(bytes.NewReader(data)) |
| 178 | for { |
| 179 | token, err := decoder.Token() |
| 180 | if err != nil { |
| 181 | return false |
| 182 | } |
| 183 | |
| 184 | switch token := token.(type) { |
| 185 | case xml.ProcInst, xml.Directive, xml.Comment: |
| 186 | continue |
| 187 | case xml.CharData: |
| 188 | if len(bytes.TrimSpace(token)) == 0 { |
| 189 | continue |
| 190 | } |
| 191 | return false |
| 192 | case xml.StartElement: |
| 193 | return strings.EqualFold(token.Name.Local, "svg") |
| 194 | default: |
| 195 | return false |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // ClassifyStoredMediaType returns the media type that durable chat storage |
| 201 | // would use for the given filename and bytes. Unsupported or blocked content is |