Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions util/fsutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"io"
"mime/multipart"
"net/http"
"os"
"sync"

Expand Down Expand Up @@ -143,19 +142,16 @@ func (file *File) Save(fs WritableFS, path string, name string) (filename string
func ParseMultipartFiles(headers []*multipart.FileHeader) ([]File, error) {
files := []File{}
for _, fh := range headers {
fileHeader := make([]byte, 512)
mimeType := "application/octet-stream"

if fh.Size != 0 {
f, err := fh.Open()
if err != nil {
return nil, errors.New(err)
}
if _, err := f.Read(fileHeader); err != nil {
_ = f.Close()
return nil, errors.New(err)
}

if _, err := f.Seek(0, 0); err != nil {
mimeType, err = DetectContentType(f, fh.Filename)
Comment thread
fnoopv marked this conversation as resolved.
Outdated
if err != nil {
_ = f.Close()
return nil, errors.New(err)
}
Expand All @@ -166,7 +162,7 @@ func ParseMultipartFiles(headers []*multipart.FileHeader) ([]File, error) {

file := File{
Header: fh,
MIMEType: http.DetectContentType(fileHeader),
MIMEType: mimeType,
}
files = append(files, file)
}
Expand Down
7 changes: 4 additions & 3 deletions util/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func GetMIMEType(filesystem fs.FS, file string) (contentType string, size int64,

// DetectContentType by sniffing the first 512 bytes of the given reader using `http.DetectContentType`.
//
// If the detected content type is `"application/octet-stream"` or `"text/plain"`, this function will attempt to
// If the detected content type is `"application/octet-stream"`, `"text/plain"`, `"text/xml"` or
// `"application/xml"`, this function will attempt to
// find a more precise one using `fsutil.DetectContentTypeByExtension`.
Comment thread
fnoopv marked this conversation as resolved.
Outdated
// The header parameter is retained (e.g: `charset=utf-8`).
//
Expand All @@ -138,7 +139,7 @@ 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") {
if strings.HasPrefix(contentType, "application/octet-stream") || strings.HasPrefix(contentType, "text/plain") || strings.HasPrefix(contentType, "text/xml") || strings.HasPrefix(contentType, "application/xml") {
Comment thread
fnoopv marked this conversation as resolved.
Outdated
contentType = detectContentTypeByExtension(fileName, contentType)
}
return contentType, nil
Expand All @@ -148,7 +149,7 @@ func detectContentTypeByExtension(fileName, contentType string) string {
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" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this condition?

@fnoopv fnoopv Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image/svg+xml usually does not need to be concatenated with charset=utf-8. If it exists, remove it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. This is because http.DetectContentType returns text/xml; charset=utf-8 and we want just image/svg+xml.

This is not necessary anymore since you added the SVG sniffing. Can you remove this change please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

tmp = t + contentType[i:] // Keep the "charset" arguments
}
contentType = tmp
Expand Down
32 changes: 32 additions & 0 deletions util/fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ func TestDetectContentType(t *testing.T) {
buf: []byte{1, 2, 3},
wantMIME: "text/javascript",
},
{
fileName: "image.svg",
buf: []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg xmlns=\"http://www.w3.org/2000/svg\"></svg>"),
wantMIME: "image/svg+xml",
},
{
fileName: "eof",
buf: []byte{},
Expand Down Expand Up @@ -300,6 +305,12 @@ func TestDetectContentTypeByExtension(t *testing.T) {
contentType: "text/plain; charset=utf-8",
fileName: "test.css",
},
{
desc: "utf8_svg",
want: "image/svg+xml",
contentType: "text/xml; charset=utf-8",
fileName: "image.svg",
},
}

for _, c := range cases {
Expand Down Expand Up @@ -425,6 +436,27 @@ func TestParseMultipartFiles(t *testing.T) {
assert.NoError(t, err)
})

t.Run("svg", func(t *testing.T) {
svgPath := toAbsolutePath("util/fsutil/test.svg")
err := os.WriteFile(svgPath, []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg xmlns=\"http://www.w3.org/2000/svg\"></svg>"), 0644)
require.NoError(t, err)
t.Cleanup(func() {
deleteFile(svgPath)
})

form := createTestForm("util/fsutil/test.svg")
files, err := ParseMultipartFiles(form.File["file"])

expected := []File{
{
Header: form.File["file"][0],
MIMEType: "image/svg+xml",
},
}
assert.Equal(t, expected, files)
assert.NoError(t, err)
})

t.Run("empty_file", func(t *testing.T) {
headers := []*multipart.FileHeader{
{
Expand Down
Loading