From 294e8d8be21c7497c4c0ba8e3167c96af85a19c0 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Wed, 28 Oct 2015 15:21:54 -0400 Subject: [PATCH] Better text detection --- display.go | 2 +- meta.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/display.go b/display.go index a449108..73aec29 100644 --- a/display.go +++ b/display.go @@ -77,7 +77,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) { } else if metadata.Mimetype == "application/pdf" { tpl = Templates["display/pdf.html"] - } else if supportedBinExtension(extension) { + } else if metadata.Mimetype == "text/plain" || supportedBinExtension(extension) { if metadata.Size < maxDisplayFileSizeBytes { bytes, err := ioutil.ReadFile(filePath) if err == nil { diff --git a/meta.go b/meta.go index 9df230f..1270e2b 100644 --- a/meta.go +++ b/meta.go @@ -15,6 +15,7 @@ import ( "path" "sort" "time" + "unicode/utf8" "bitbucket.org/taruti/mimemagic" "github.com/dchest/uniuri" @@ -64,6 +65,15 @@ func generateMetadata(fName string, exp time.Time, delKey string) (m Metadata, e m.Mimetype = mimemagic.Match("", header) + if m.Mimetype == "" { + // Check if the file seems anything like text + if utf8.Valid(header) { + m.Mimetype = "text/plain" + } else { + m.Mimetype = "application/octet-stream" + } + } + // Compute the sha256sum hasher := sha256.New() file.Seek(0, 0)