mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
Merge remote-tracking branch 'origin/bzz' into bzz
This commit is contained in:
commit
aae5551978
5 changed files with 77 additions and 37 deletions
87
bzz/api.go
87
bzz/api.go
|
|
@ -188,37 +188,60 @@ const maxParallelFiles = 5
|
|||
// Upload replicates a local directory as a manifest file and uploads it
|
||||
// using dpa store
|
||||
// TODO: localpath should point to a manifest
|
||||
func (self *Api) Upload(lpath string) (string, error) {
|
||||
func (self *Api) Upload(lpath, index string) (string, error) {
|
||||
var list []*manifestTrieEntry
|
||||
localpath, err1 := filepath.Abs(filepath.Clean(lpath))
|
||||
if err1 != nil {
|
||||
return "", err1
|
||||
}
|
||||
start := len(localpath)
|
||||
if (start > 0) && (localpath[start-1] != os.PathSeparator) {
|
||||
start++
|
||||
}
|
||||
dpaLogger.Debugf("uploading '%s'", localpath)
|
||||
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
||||
if (err == nil) && !info.IsDir() {
|
||||
dpaLogger.Debugf("localpath: '%s'; path: '%s'\n", localpath, path)
|
||||
if len(path) <= start {
|
||||
return fmt.Errorf("Path is too short")
|
||||
}
|
||||
if path[:len(localpath)] != localpath {
|
||||
return fmt.Errorf("Path prefix of '%s' does not match localpath '%s'", path, localpath)
|
||||
}
|
||||
entry := &manifestTrieEntry{
|
||||
Path: path,
|
||||
}
|
||||
list = append(list, entry)
|
||||
}
|
||||
return err
|
||||
})
|
||||
localpath, err := filepath.Abs(filepath.Clean(lpath))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
f, err := os.Open(localpath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
stat, err := f.Stat()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var start int
|
||||
if stat.IsDir() {
|
||||
start = len(localpath)
|
||||
dpaLogger.Debugf("uploading '%s'", localpath)
|
||||
err = filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
||||
if (err == nil) && !info.IsDir() {
|
||||
//fmt.Printf("lp %s path %s\n", localpath, path)
|
||||
if len(path) <= start {
|
||||
return fmt.Errorf("Path is too short")
|
||||
}
|
||||
if path[:start] != localpath {
|
||||
return fmt.Errorf("Path prefix of '%s' does not match localpath '%s'", path, localpath)
|
||||
}
|
||||
entry := &manifestTrieEntry{
|
||||
Path: path,
|
||||
}
|
||||
list = append(list, entry)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
dir := filepath.Dir(localpath)
|
||||
start = len(dir)
|
||||
if len(localpath) <= start {
|
||||
return "", fmt.Errorf("Path is too short")
|
||||
}
|
||||
if localpath[:start] != dir {
|
||||
return "", fmt.Errorf("Path prefix of '%s' does not match dir '%s'", localpath, dir)
|
||||
}
|
||||
entry := &manifestTrieEntry{
|
||||
Path: localpath,
|
||||
}
|
||||
list = append(list, entry)
|
||||
}
|
||||
|
||||
cnt := len(list)
|
||||
errors := make([]error, cnt)
|
||||
done := make(chan bool, maxParallelFiles)
|
||||
|
|
@ -243,7 +266,8 @@ func (self *Api) Upload(lpath string) (string, error) {
|
|||
wg.Wait()
|
||||
}
|
||||
if err == nil {
|
||||
cmd := exec.Command("file", "--mime-type", "-b", entry.Path)
|
||||
//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()
|
||||
|
|
@ -269,6 +293,15 @@ func (self *Api) Upload(lpath string) (string, error) {
|
|||
}
|
||||
entry.Path = regularSlashes(entry.Path[start:])
|
||||
trie.addEntry(entry)
|
||||
|
||||
if entry.Path == index {
|
||||
ientry := &manifestTrieEntry{
|
||||
Path: "",
|
||||
Hash: entry.Hash,
|
||||
ContentType: entry.ContentType,
|
||||
}
|
||||
trie.addEntry(ientry)
|
||||
}
|
||||
}
|
||||
|
||||
err2 := trie.recalcAndStore()
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ func TestApiDirUpload(t *testing.T) {
|
|||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
bzzhash, err := api.Upload(path.Join(testDir, "test0"))
|
||||
bzzhash, err := api.Upload(path.Join(testDir, "test0"), "")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
@ -101,7 +101,7 @@ func TestApiFileUpload(t *testing.T) {
|
|||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
bzzhash, err := api.Upload(path.Join(testDir, "test0", "index.html"))
|
||||
bzzhash, err := api.Upload(path.Join(testDir, "test0", "index.html"), "")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ delimiter='{"entries":[{'
|
|||
|
||||
if [ -f "$1" ]; then
|
||||
hash=`wget -q -O- --post-file="$1" http://localhost:8500/raw`
|
||||
mime=`file --mime-type -b "$1"`
|
||||
mime=`mimetype -b "$1"`
|
||||
wget -q -O- --post-data="$delimiter\"hash\":\"$hash\",\"contentType\":\"$mime\"}]}" http://localhost:8500/raw
|
||||
echo
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ pushd "$bzzroot" > /dev/null
|
|||
|
||||
(for path in `find . -type f`
|
||||
do
|
||||
name=`echo "$path" | cut -c2-`
|
||||
name=`echo "$path" | cut -c3-`
|
||||
[ _`basename "$name"` = "_$INDEX" ] && name=`dirname "$name"`
|
||||
echo -n "$delimiter"
|
||||
hash=`wget -q -O- --post-file="$path" http://localhost:8500/raw`
|
||||
|
|
|
|||
|
|
@ -182,12 +182,19 @@ func (self *JSApi) download(call otto.FunctionCall) otto.Value {
|
|||
}
|
||||
|
||||
func (self *JSApi) upload(call otto.FunctionCall) otto.Value {
|
||||
if len(call.ArgumentList) != 1 {
|
||||
fmt.Println("requires 1 arguments: bzz.put(localpath)")
|
||||
var err error
|
||||
var index string
|
||||
if len(call.ArgumentList) == 2 {
|
||||
index, err = call.Argument(1).ToString()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
} else if len(call.ArgumentList) != 1 {
|
||||
fmt.Println("requires 1 or 2 arguments: bzz.put(localpath[, index])")
|
||||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
var err error
|
||||
var localpath, res string
|
||||
localpath, err = call.Argument(0).ToString()
|
||||
if err != nil {
|
||||
|
|
@ -195,7 +202,7 @@ func (self *JSApi) upload(call otto.FunctionCall) otto.Value {
|
|||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
res, err = self.api.Upload(localpath)
|
||||
res, err = self.api.Upload(localpath, index)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return otto.UndefinedValue()
|
||||
|
|
|
|||
|
|
@ -262,8 +262,8 @@ func (self *manifestTrie) getEntry(spath string) (entry *manifestTrieEntry, full
|
|||
path := regularSlashes(spath)
|
||||
var pos int
|
||||
entry, pos = self.findPrefixOf(path)
|
||||
if (pos > 0) && (pos < len(path)) && (path[pos] != '/') {
|
||||
/*if (pos > 0) && (pos < len(path)) && (path[pos] != '/') {
|
||||
return nil, ""
|
||||
}
|
||||
}*/
|
||||
return entry, path[:pos]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue