Skip to content

Commit

Permalink
Better text detection
Browse files Browse the repository at this point in the history
  • Loading branch information
andreimarcu committed Oct 28, 2015
1 parent 9b1df43 commit 294e8d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path"
"sort"
"time"
"unicode/utf8"

"bitbucket.org/taruti/mimemagic"
"github.com/dchest/uniuri"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 294e8d8

Please sign in to comment.