From 728776dc03142b3532499da4e05550d01ee19991 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 10 Jul 2017 08:43:46 +0100 Subject: [PATCH] Fix #100 Former-commit-id: 1130e6e7ae67fdcb866c5dc4eee11de28fc69a96 [formerly 55dc3c0c9bf3b07608f8cade510e2d0d94782ab3] [formerly d5a48b8bef9ae5cc613558b15e98d584a5ed6fa5 [formerly cacca2e6f2650c387715d58af614d20431719e3d]] Former-commit-id: cd6f80f2a273d1fa5ed5d35924e416bed7c64194 [formerly d9b42a6d8f025a26107ba91ece5cc4855a2cb681] Former-commit-id: 2f28d37577a16078e1c191192dfba7ae2b9f2acd --- file.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/file.go b/file.go index 4352fa68..aeb67253 100644 --- a/file.go +++ b/file.go @@ -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 } }