pull/144/head
Henrique Dias 2017-07-10 08:43:46 +01:00
parent dee322a88d
commit cacca2e6f2
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
1 changed files with 11 additions and 3 deletions

14
file.go
View File

@ -209,14 +209,22 @@ func (i *file) RetrieveFileType(checkContent bool) error {
mimetype := mime.TypeByExtension(i.Extension)
if mimetype == "" && checkContent {
content, err = ioutil.ReadFile(i.Path)
file, err := os.Open(i.Path)
if err != nil {
return err
}
defer file.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil && err != io.EOF {
return err
}
// Tries to get the file mimetype using its first
// 512 bytes.
mimetype = http.DetectContentType(content)
mimetype = http.DetectContentType(buffer)
}
if strings.HasPrefix(mimetype, "video") {
@ -249,7 +257,7 @@ func (i *file) RetrieveFileType(checkContent bool) error {
for _, extension := range textExtensions {
if strings.HasSuffix(i.Name, extension) {
i.Type = "text"
return nil
goto End
}
}