Better text detection

This commit is contained in:
andreimarcu 2015-10-28 15:21:54 -04:00
parent 9b1df43ef2
commit 294e8d8be2
2 changed files with 11 additions and 1 deletions

View File

@ -77,7 +77,7 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
} else if metadata.Mimetype == "application/pdf" { } else if metadata.Mimetype == "application/pdf" {
tpl = Templates["display/pdf.html"] tpl = Templates["display/pdf.html"]
} else if supportedBinExtension(extension) { } else if metadata.Mimetype == "text/plain" || supportedBinExtension(extension) {
if metadata.Size < maxDisplayFileSizeBytes { if metadata.Size < maxDisplayFileSizeBytes {
bytes, err := ioutil.ReadFile(filePath) bytes, err := ioutil.ReadFile(filePath)
if err == nil { if err == nil {

10
meta.go
View File

@ -15,6 +15,7 @@ import (
"path" "path"
"sort" "sort"
"time" "time"
"unicode/utf8"
"bitbucket.org/taruti/mimemagic" "bitbucket.org/taruti/mimemagic"
"github.com/dchest/uniuri" "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) 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 // Compute the sha256sum
hasher := sha256.New() hasher := sha256.New()
file.Seek(0, 0) file.Seek(0, 0)