-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix some mp3/mp4 media files show as Raw #18458
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 14 commits
8c59741
5870ffd
3479fe4
de34dda
6e9a24f
74041c1
4c34c16
ee0b1fe
fd511ad
4a17d41
b5d72e9
344d179
cb4cfc5
19c42b0
1f58f56
b7716d4
d4ed164
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 |
---|---|---|
|
@@ -7,7 +7,9 @@ package typesniffer | |
import ( | ||
"fmt" | ||
"io" | ||
"mime" | ||
"net/http" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
|
@@ -87,6 +89,26 @@ func DetectContentType(data []byte) SniffedType { | |
return SniffedType{ct} | ||
} | ||
|
||
func DetectContentTypeExtFirst(name string, readSeekerOrBytes interface{}) (SniffedType, error) { | ||
// we can merge `setting.MimeTypeMap` by mime.AddExtensionType() | ||
ct := mime.TypeByExtension(filepath.Ext(name)) | ||
if ct != "" && !strings.Contains(ct, "text/") { | ||
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. Is it possible to change Contains -> HasPrefix? |
||
return SniffedType{ct}, nil | ||
} | ||
if r, ok := readSeekerOrBytes.(io.ReadSeeker); ok { | ||
st, err := DetectContentTypeFromReader(r) | ||
if nil != err { | ||
return SniffedType{}, err | ||
} | ||
_, err = r.Seek(0, io.SeekStart) | ||
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 is |
||
if nil != err { | ||
return SniffedType{}, err | ||
} | ||
return st, nil | ||
} | ||
return DetectContentType(readSeekerOrBytes.([]byte)), nil | ||
} | ||
|
||
// DetectContentTypeFromReader guesses the content type contained in the reader. | ||
func DetectContentTypeFromReader(r io.Reader) (SniffedType, error) { | ||
buf := make([]byte, sniffLen) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader) | |
// Google Chrome dislike commas in filenames, so let's change it to a space | ||
name = strings.ReplaceAll(name, ",", " ") | ||
|
||
// st, err := typesniffer.DetectContentTypeExtFirst(name, buf) | ||
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. Commented code is confusing and we shouldn't have it. If you want to retain it for future reference, it's better to add a sentence or two about it. |
||
st := typesniffer.DetectContentType(buf) | ||
|
||
mappedMimeType := "" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should ignore a error here.