ClassifyStoredMediaType returns the media type that durable chat storage would use for the given filename and bytes. Unsupported or blocked content is returned as its detected media type so callers can report the specific type.
(name string, data []byte)
| 201 | // would use for the given filename and bytes. Unsupported or blocked content is |
| 202 | // returned as its detected media type so callers can report the specific type. |
| 203 | func ClassifyStoredMediaType(name string, data []byte) string { |
| 204 | if HasSVGRootElement(data) { |
| 205 | return "image/svg+xml" |
| 206 | } |
| 207 | |
| 208 | mediaType := DetectMediaType(data) |
| 209 | switch mediaType { |
| 210 | case "image/png", "image/jpeg", "image/gif", "image/webp", |
| 211 | "text/markdown", "text/csv", "application/json", |
| 212 | "application/pdf", "application/xml", "text/xml": |
| 213 | return mediaType |
| 214 | case "text/plain": |
| 215 | return refineTextMediaType(name, data) |
| 216 | default: |
| 217 | if strings.HasPrefix(mediaType, "text/") { |
| 218 | return "text/plain" |
| 219 | } |
| 220 | return mediaType |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | func refineTextMediaType(name string, data []byte) string { |
| 225 | switch strings.ToLower(filepath.Ext(name)) { |