Browse Source

Temporary fix for text detection

pull/73/head
andreimarcu 9 years ago
parent
commit
e6d79eb5cf
  1. 24
      meta.go

24
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
}
Loading…
Cancel
Save