-
-
Notifications
You must be signed in to change notification settings - Fork 84
fix: the MIME type of an SVG file should be image/svg+xml #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a9b4419
bf8ec84
c6eb46d
f5a1b76
1be177d
46d10b4
c256b6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,8 @@ func GetMIMEType(filesystem fs.FS, file string) (contentType string, size int64, | |
| // | ||
| // If the detected content type is `"application/octet-stream"` or `"text/plain"`, this function will attempt to | ||
| // find a more precise one using `fsutil.DetectContentTypeByExtension`. | ||
| // If the detected content type is `"text/xml"` or `"application/xml"`, this function promotes it to | ||
| // `"image/svg+xml"` only if the content signature indicates SVG. | ||
| // The header parameter is retained (e.g: `charset=utf-8`). | ||
| // | ||
| // If there is no error, this function always returns a valid MIME type. If it cannot determine a more specific one, | ||
|
|
@@ -140,15 +142,46 @@ func DetectContentType(r io.Reader, fileName string) (string, error) { | |
| contentType := http.DetectContentType(buffer) | ||
| if strings.HasPrefix(contentType, "application/octet-stream") || strings.HasPrefix(contentType, "text/plain") { | ||
| contentType = detectContentTypeByExtension(fileName, contentType) | ||
| } else if (strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml")) && hasSVGSignature(buffer) { | ||
| contentType = "image/svg+xml" | ||
| } | ||
| return contentType, nil | ||
| } | ||
|
|
||
| func hasSVGSignature(buffer []byte) bool { | ||
| content := strings.TrimSpace(strings.ToLower(string(buffer))) | ||
| if after, ok := strings.CutPrefix(content, "\ufeff"); ok { | ||
|
System-Glitch marked this conversation as resolved.
Outdated
|
||
| content = strings.TrimSpace(after) | ||
| } | ||
|
|
||
| for { | ||
|
fnoopv marked this conversation as resolved.
|
||
| if strings.HasPrefix(content, "<?xml") { | ||
| end := strings.Index(content, "?>") | ||
| if end == -1 { | ||
| break | ||
| } | ||
| content = strings.TrimSpace(content[end+2:]) | ||
| continue | ||
| } | ||
| if strings.HasPrefix(content, "<!--") { | ||
| end := strings.Index(content, "-->") | ||
| if end == -1 { | ||
| break | ||
| } | ||
| content = strings.TrimSpace(content[end+3:]) | ||
| continue | ||
| } | ||
| break | ||
| } | ||
|
|
||
| return strings.HasPrefix(content, "<svg") | ||
| } | ||
|
|
||
| func detectContentTypeByExtension(fileName, contentType string) string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an early return here if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| for ext, t := range contentTypeByExtension { | ||
| if strings.HasSuffix(fileName, ext) { | ||
| tmp := t | ||
| if i := strings.Index(contentType, ";"); i != -1 { | ||
| if i := strings.Index(contentType, ";"); i != -1 && t != "image/svg+xml" { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this condition?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. This is because This is not necessary anymore since you added the SVG sniffing. Can you remove this change please?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| tmp = t + contentType[i:] // Keep the "charset" arguments | ||
| } | ||
| contentType = tmp | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.