MCPcopy Create free account
hub / github.com/coder/coder / HasSVGRootElement

Function HasSVGRootElement

coderd/x/chatfiles/mime.go:171–198  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

169// root element. This catches SVG content even when generic sniffers classify it
170// as text or XML.
171func 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

Callers 2

TestHasSVGRootElementFunction · 0.92
ClassifyStoredMediaTypeFunction · 0.85

Calls 1

TokenMethod · 0.65

Tested by 1

TestHasSVGRootElementFunction · 0.74