From e6ac89d6dc1b32164617b424689c12a21eaeafb3 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sat, 25 Mar 2017 01:05:23 -0700 Subject: [PATCH] Switch to https://github.com/h2non/filetype This library is much better at detecting MIME types properly than the existing one. Fixes #117. --- meta.go | 9 +++++++-- upload.go | 13 +++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/meta.go b/meta.go index df2e0b5..d2fb637 100644 --- a/meta.go +++ b/meta.go @@ -15,8 +15,8 @@ import ( "time" "unicode" - "bitbucket.org/taruti/mimemagic" "github.com/dchest/uniuri" + "gopkg.in/h2non/filetype.v1" ) type MetadataJSON struct { @@ -66,7 +66,12 @@ func generateMetadata(fName string, exp time.Time, delKey string) (m Metadata, e header := make([]byte, 512) file.Read(header) - m.Mimetype = mimemagic.Match("", header) + kind, err := filetype.Match(header) + if err != nil { + m.Mimetype = "application/octet-stream" + } else { + m.Mimetype = kind.MIME.Value + } if m.Mimetype == "" { // Check if the file seems anything like text diff --git a/upload.go b/upload.go index 1233d1a..bd2bed6 100644 --- a/upload.go +++ b/upload.go @@ -15,9 +15,9 @@ import ( "strings" "time" - "bitbucket.org/taruti/mimemagic" "github.com/dchest/uniuri" "github.com/zenazn/goji/web" + "gopkg.in/h2non/filetype.v1" ) var fileBlacklist = map[string]bool{ @@ -218,15 +218,12 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) { header = header[:n] // Determine the type of file from header - mimetype := mimemagic.Match("", header) - - // If the mime type is in our map, use that - // otherwise just use "ext" - if val, exists := mimeToExtension[mimetype]; exists { - extension = val - } else { + kind, err := filetype.Match(header) + if err != nil { extension = "ext" } + + extension = kind.Extension } upload.Filename = strings.Join([]string{barename, extension}, ".")