diff --git a/README.md b/README.md index 7c8161d..1fa1ba9 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,11 @@ A MIME type sniffer for Go. MIMESniffer implements the algorithm described at -[here](https://mimesniff.spec.whatwg.org) to determine the MIME type of the -given data. So it can be used as a substitute for `http.DetectContentType()`. +[here](https://mimesniff.spec.whatwg.org) and uses the file signatures (aka +[magic numbers](https://en.wikipedia.org/wiki/Magic_number_\(programming\)#Magic_numbers_in_files)) +listed [here](https://www.garykessler.net/library/file_sigs.html) to determine +the MIME type of the given data. So it can be used as an alternative for the +`http.DetectContentType()`. ## Features diff --git a/mimesniffer.go b/mimesniffer.go index 6866d9e..b7bcc76 100644 --- a/mimesniffer.go +++ b/mimesniffer.go @@ -78,10 +78,8 @@ func Register(mimeType string, sniffer func([]byte) bool) { // It always returns a valid MIME type: if it cannot determine a more specific // one, it returns "application/octet-stream". func Sniff(b []byte) string { - const unknownType = "application/octet-stream" - if len(b) == 0 { - return unknownType + return "application/octet-stream" } for mt, s := range registeredSniffers { @@ -90,17 +88,13 @@ func Sniff(b []byte) string { } } - if mt := http.DetectContentType(b); mt != unknownType { - return mt - } - for mt, s := range defaultSniffers { if s(b) { return mt } } - return unknownType + return http.DetectContentType(b) } // applicationEPUBZip reports whether the b's MIME type is diff --git a/mimesniffer_test.go b/mimesniffer_test.go index 8450c75..8b260dd 100644 --- a/mimesniffer_test.go +++ b/mimesniffer_test.go @@ -32,6 +32,7 @@ func TestSniff(t *testing.T) { }) assert.Equal(t, Sniff([]byte{0x00}), "foo/bar") - assert.Equal(t, Sniff([]byte("foobar")), "text/plain; charset=utf-8") assert.Equal(t, Sniff([]byte{0x01}), "application/octet-stream") + assert.Equal(t, Sniff([]byte{0xff, 0xf1}), "audio/aac") + assert.Equal(t, Sniff([]byte("foobar")), "text/plain; charset=utf-8") }