Browse Source

Merge pull request #120 from mutantmonkey/switch_to_filetype

Switch to https://github.com/h2non/filetype
pull/121/head
Andrei Marcu 8 years ago
committed by GitHub
parent
commit
1c1d3127ab
  1. 9
      meta.go
  2. 13
      upload.go

9
meta.go

@ -15,8 +15,8 @@ import (
"time" "time"
"unicode" "unicode"
"bitbucket.org/taruti/mimemagic"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"gopkg.in/h2non/filetype.v1"
) )
type MetadataJSON struct { type MetadataJSON struct {
@ -66,7 +66,12 @@ func generateMetadata(fName string, exp time.Time, delKey string) (m Metadata, e
header := make([]byte, 512) header := make([]byte, 512)
file.Read(header) 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 == "" { if m.Mimetype == "" {
// Check if the file seems anything like text // Check if the file seems anything like text

13
upload.go

@ -15,9 +15,9 @@ import (
"strings" "strings"
"time" "time"
"bitbucket.org/taruti/mimemagic"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"github.com/zenazn/goji/web" "github.com/zenazn/goji/web"
"gopkg.in/h2non/filetype.v1"
) )
var fileBlacklist = map[string]bool{ var fileBlacklist = map[string]bool{
@ -218,15 +218,12 @@ func processUpload(upReq UploadRequest) (upload Upload, err error) {
header = header[:n] header = header[:n]
// Determine the type of file from header // 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 = "ext"
} }
extension = kind.Extension
} }
upload.Filename = strings.Join([]string{barename, extension}, ".") upload.Filename = strings.Join([]string{barename, extension}, ".")

Loading…
Cancel
Save