bzz.Api.Upload determines mime types

fixed absolute path conversion
This commit is contained in:
zsfelfoldi 2015-05-29 03:13:37 +02:00
parent ff2081f5fd
commit c3bb14e352

View file

@ -7,6 +7,7 @@ import (
"io" "io"
"net" "net"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -110,9 +111,12 @@ func (self *Api) Download(bzzpath, localpath string) (string, error) {
// Upload replicates a local directory as a manifest file and uploads it // Upload replicates a local directory as a manifest file and uploads it
// using dpa store // using dpa store
// TODO: localpath should point to a manifest // TODO: localpath should point to a manifest
func (self *Api) Upload(localpath string) (string, error) { func (self *Api) Upload(lpath string) (string, error) {
var files []string var files []string
localpath = common.ExpandHomePath(localpath) localpath, err1 := filepath.Abs(lpath)
if err1 != nil {
return "", err1
}
start := len(localpath) start := len(localpath)
if (start > 0) && (localpath[start-1] != os.PathSeparator) { if (start > 0) && (localpath[start-1] != os.PathSeparator) {
start++ start++
@ -125,7 +129,7 @@ func (self *Api) Upload(localpath string) (string, error) {
return fmt.Errorf("Path is too short") return fmt.Errorf("Path is too short")
} }
if path[:len(localpath)] != localpath { if path[:len(localpath)] != localpath {
return fmt.Errorf("Path prefix does not match localpath") return fmt.Errorf("Path prefix of '%s' does not match localpath '%s'", path, localpath)
} }
files = append(files, path) files = append(files, path)
} }
@ -138,6 +142,7 @@ func (self *Api) Upload(localpath string) (string, error) {
cnt := len(files) cnt := len(files)
hashes := make([]Key, cnt) hashes := make([]Key, cnt)
errors := make([]error, cnt) errors := make([]error, cnt)
ctypes := make([]string, cnt)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
for i, path := range files { for i, path := range files {
@ -149,6 +154,15 @@ func (self *Api) Upload(localpath string) (string, error) {
sr := io.NewSectionReader(f, 0, stat.Size()) sr := io.NewSectionReader(f, 0, stat.Size())
hashes[i], err = self.dpa.Store(sr, wg) hashes[i], err = self.dpa.Store(sr, wg)
} }
if err == nil {
cmd := exec.Command("file", "--mime-type", "-b", path)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err == nil {
ctypes[i] = strings.TrimSuffix(out.String(), "\n")
}
}
errors[i] = err errors[i] = err
wg.Done() wg.Done()
}(i, path) }(i, path)
@ -169,7 +183,7 @@ func (self *Api) Upload(localpath string) (string, error) {
if i == cnt-1 { if i == cnt-1 {
sc = "]}" sc = "]}"
} }
buffer.WriteString(fmt.Sprintf(`{"hash":"%064x","path":"%s","contentType":"text/plain"}%s`, hashes[i], path[start:], sc)) buffer.WriteString(fmt.Sprintf(`{"hash":"%064x","path":"%s","contentType":"%s"}%s`, hashes[i], path[start:], ctypes[i], sc))
} }
manifest := buffer.Bytes() manifest := buffer.Bytes()