From 12264fe36d5e3b1fa9f7ee670c2e3dfdd9c21877 Mon Sep 17 00:00:00 2001 From: zsfelfoldi Date: Thu, 4 Jun 2015 19:33:31 +0200 Subject: [PATCH] mime type detection --- bzz/api.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bzz/api.go b/bzz/api.go index 7065f76775..0e1813311f 100644 --- a/bzz/api.go +++ b/bzz/api.go @@ -2,12 +2,11 @@ package bzz import ( "bufio" - "bytes" "fmt" "io" "net" + "net/http" "os" - "os/exec" "path/filepath" "regexp" "strings" @@ -319,16 +318,19 @@ func (self *Api) Upload(lpath, index string) (string, error) { list[i].Hash = fmt.Sprintf("%064x", hash) } wg.Wait() - } - if err == nil { - cmd := exec.Command("file", "--mime-type", "-b", entry.Path) - // cmd := exec.Command("mimetype", "-b", entry.Path) - var out bytes.Buffer - cmd.Stdout = &out - err = cmd.Run() if err == nil { - list[i].ContentType = strings.TrimSuffix(out.String(), "\n") + first512 := make([]byte, 512) + fread, _ := sr.ReadAt(first512, 0) + if fread > 0 { + mimeType := http.DetectContentType(first512[:fread]) + if filepath.Ext(entry.Path) == ".css" { + mimeType = "text/css" + } + list[i].ContentType = mimeType + //fmt.Printf("%v %v %v\n", entry.Path, mimeType, filepath.Ext(entry.Path)) + } } + f.Close() } errors[i] = err done <- true