Browse Source

Switch to https://github.com/h2non/filetype

This library is much better at detecting MIME types properly than the
existing one. Fixes #117.
pull/120/head
mutantmonkey 8 years ago
parent
commit
e6ac89d6dc
  1. 9
      meta.go
  2. 13
      upload.go

9
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

13
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}, ".")

Loading…
Cancel
Save