Improve Mimetype detecting

pull/144/head
Henrique Dias 2016-10-22 16:37:20 +01:00
parent 4e7c730faf
commit 82cc6e77bd
1 changed files with 13 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package file
import ( import (
"io/ioutil" "io/ioutil"
"mime"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -50,25 +51,27 @@ func GetInfo(url *url.URL, c *config.Config, u *config.User) (*Info, int, error)
// RetrieveFileType obtains the mimetype and a simplified internal Type // RetrieveFileType obtains the mimetype and a simplified internal Type
// using the first 512 bytes from the file. // using the first 512 bytes from the file.
func (i *Info) RetrieveFileType() error { func (i *Info) RetrieveFileType() error {
file, err := os.Open(i.Path) i.Mimetype = mime.TypeByExtension(filepath.Ext(i.Name()))
if err != nil {
return err
}
defer file.Close()
p := make([]byte, 512) if i.Mimetype == "" {
_, err = file.Read(p) err := i.Read()
if err != nil { if err != nil {
return err return err
}
i.Mimetype = http.DetectContentType(i.Content)
} }
i.Mimetype = http.DetectContentType(p)
i.Type = simplifyMediaType(i.Mimetype) i.Type = simplifyMediaType(i.Mimetype)
return nil return nil
} }
// Reads the file. // Reads the file.
func (i *Info) Read() error { func (i *Info) Read() error {
if len(i.Content) != 0 {
return nil
}
var err error var err error
i.Content, err = ioutil.ReadFile(i.Path) i.Content, err = ioutil.ReadFile(i.Path)
if err != nil { if err != nil {