Merge pull request #120 from mutantmonkey/switch_to_filetype
Switch to https://github.com/h2non/filetype
This commit is contained in:
commit
1c1d3127ab
9
meta.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
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…
Reference in New Issue
Block a user