fixes for api.upload

This commit is contained in:
zelig 2015-05-28 09:27:55 +01:00
parent 0139cdade4
commit 06fdf4acb3
2 changed files with 14 additions and 4 deletions

View file

@ -91,6 +91,8 @@ func (self *Api) Download(bzzpath, localpath string) (string, error) {
// TODO: localpath should point to a manifest
func (self *Api) Upload(localpath, address, domain string) (string, error) {
var files []string
localpath = common.ExpandHomePath(localpath)
dpaLogger.Debugf("uploading '%s'", localpath)
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
if err == nil {
files = append(files, path)
@ -124,6 +126,13 @@ func (self *Api) Upload(localpath, address, domain string) (string, error) {
var buffer bytes.Buffer
buffer.WriteString(`{"entries":[`)
sc := ","
var pathPrefix *regexp.Regexp
var relativePath string
pathPrefix, err = regexp.Compile("^" + localpath + fmt.Sprintf("%s", os.PathSeparator))
if err != nil {
return "", err
}
for i, path := range files {
if errors[i] != nil {
return "", errors[i]
@ -131,7 +140,8 @@ func (self *Api) Upload(localpath, address, domain string) (string, error) {
if i == cnt-1 {
sc = "]}"
}
buffer.WriteString(fmt.Sprintf(`{"hash":"%064x","path":"%s","contentType":"text/plain"}%s`, hashes[i], path, sc))
relativePath = pathPrefix.ReplaceAllString(path, "")
buffer.WriteString(fmt.Sprintf(`{"hash":"%064x","path":"%s","contentType":"text/plain"}%s`, hashes[i], relativePath, sc))
}
manifest := buffer.Bytes()

View file

@ -54,7 +54,7 @@ func (self *JSApi) get(call otto.FunctionCall) otto.Value {
}
func (self *JSApi) put(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 2 || len(call.ArgumentList) != 4 {
if len(call.ArgumentList) != 2 && len(call.ArgumentList) != 4 {
fmt.Println("requires 2 or 4 arguments: bzz.put(content, content-type[, address, domain])")
return otto.UndefinedValue()
}
@ -124,8 +124,8 @@ func (self *JSApi) download(call otto.FunctionCall) otto.Value {
}
func (self *JSApi) upload(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) != 1 || len(call.ArgumentList) != 3 {
fmt.Println("requires 1 or 1 arguments: bzz.put(localpath[, address, domain])")
if len(call.ArgumentList) != 1 && len(call.ArgumentList) != 3 {
fmt.Println("requires 1 or 3 arguments: bzz.put(localpath[, address, domain])")
return otto.UndefinedValue()
}