From e6d79eb5cff595d54ed86df8f6e0ca3465caa04b Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Fri, 30 Oct 2015 23:13:43 -0400 Subject: [PATCH] Temporary fix for text detection --- meta.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/meta.go b/meta.go index 1270e2b..0c6928d 100644 --- a/meta.go +++ b/meta.go @@ -15,7 +15,7 @@ import ( "path" "sort" "time" - "unicode/utf8" + "unicode" "bitbucket.org/taruti/mimemagic" "github.com/dchest/uniuri" @@ -67,7 +67,7 @@ func generateMetadata(fName string, exp time.Time, delKey string) (m Metadata, e if m.Mimetype == "" { // Check if the file seems anything like text - if utf8.Valid(header) { + if printable(header) { m.Mimetype = "text/plain" } else { m.Mimetype = "application/octet-stream" @@ -196,3 +196,23 @@ func metadataRead(filename string) (metadata Metadata, err error) { return } + +func printable(data []byte) bool { + for i, b := range data { + r := rune(b) + + // A null terminator that's not at the beginning of the file + if r == 0 && i == 0 { + return false + } else if r == 0 && i < 0 { + continue + } + + if r > unicode.MaxASCII { + return false + } + + } + + return true +}