fix: dont fail when can't detect file type (#609)

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
pull/606/head
Henrique Dias 2019-01-06 12:59:53 +00:00 committed by GitHub
parent e22598a126
commit 07f3ee38e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"encoding/hex" "encoding/hex"
"hash" "hash"
"io" "io"
"log"
"mime" "mime"
"net/http" "net/http"
"os" "os"
@ -127,16 +128,24 @@ func (i *FileInfo) Checksum(algo string) error {
} }
func (i *FileInfo) detectType(modify, saveContent bool) error { func (i *FileInfo) detectType(modify, saveContent bool) error {
// failing to detect the type should not return error.
// imagine the situation where a file in a dir with thousands
// of files couldn't be opened: we'd have immediately
// a 500 even though it doesn't matter. So we just log it.
reader, err := i.Fs.Open(i.Path) reader, err := i.Fs.Open(i.Path)
if err != nil { if err != nil {
return err log.Print(err)
i.Type = "blob"
return nil
} }
defer reader.Close() defer reader.Close()
buffer := make([]byte, 512) buffer := make([]byte, 512)
n, err := reader.Read(buffer) n, err := reader.Read(buffer)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return err log.Print(err)
i.Type = "blob"
return nil
} }
mimetype := mime.TypeByExtension(i.Extension) mimetype := mime.TypeByExtension(i.Extension)