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
55
bzz/api.go
55
bzz/api.go
|
|
@ -188,24 +188,33 @@ const maxParallelFiles = 5
|
||||||
// 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(lpath string) (string, error) {
|
func (self *Api) Upload(lpath, index string) (string, error) {
|
||||||
var list []*manifestTrieEntry
|
var list []*manifestTrieEntry
|
||||||
localpath, err1 := filepath.Abs(filepath.Clean(lpath))
|
localpath, err := filepath.Abs(filepath.Clean(lpath))
|
||||||
if err1 != nil {
|
if err != nil {
|
||||||
return "", err1
|
return "", err
|
||||||
}
|
}
|
||||||
start := len(localpath)
|
|
||||||
if (start > 0) && (localpath[start-1] != os.PathSeparator) {
|
f, err := os.Open(localpath)
|
||||||
start++
|
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)
|
dpaLogger.Debugf("uploading '%s'", localpath)
|
||||||
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
err = filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
||||||
if (err == nil) && !info.IsDir() {
|
if (err == nil) && !info.IsDir() {
|
||||||
dpaLogger.Debugf("localpath: '%s'; path: '%s'\n", localpath, path)
|
//fmt.Printf("lp %s path %s\n", localpath, path)
|
||||||
if len(path) <= start {
|
if len(path) <= start {
|
||||||
return fmt.Errorf("Path is too short")
|
return fmt.Errorf("Path is too short")
|
||||||
}
|
}
|
||||||
if path[:len(localpath)] != localpath {
|
if path[:start] != localpath {
|
||||||
return fmt.Errorf("Path prefix of '%s' does not match localpath '%s'", path, localpath)
|
return fmt.Errorf("Path prefix of '%s' does not match localpath '%s'", path, localpath)
|
||||||
}
|
}
|
||||||
entry := &manifestTrieEntry{
|
entry := &manifestTrieEntry{
|
||||||
|
|
@ -218,6 +227,20 @@ func (self *Api) Upload(lpath string) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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)
|
cnt := len(list)
|
||||||
errors := make([]error, cnt)
|
errors := make([]error, cnt)
|
||||||
|
|
@ -243,7 +266,8 @@ func (self *Api) Upload(lpath string) (string, error) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
if err == nil {
|
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
|
var out bytes.Buffer
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
|
|
@ -269,6 +293,15 @@ func (self *Api) Upload(lpath string) (string, error) {
|
||||||
}
|
}
|
||||||
entry.Path = regularSlashes(entry.Path[start:])
|
entry.Path = regularSlashes(entry.Path[start:])
|
||||||
trie.addEntry(entry)
|
trie.addEntry(entry)
|
||||||
|
|
||||||
|
if entry.Path == index {
|
||||||
|
ientry := &manifestTrieEntry{
|
||||||
|
Path: "",
|
||||||
|
Hash: entry.Hash,
|
||||||
|
ContentType: entry.ContentType,
|
||||||
|
}
|
||||||
|
trie.addEntry(ientry)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err2 := trie.recalcAndStore()
|
err2 := trie.recalcAndStore()
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ func TestApiDirUpload(t *testing.T) {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bzzhash, err := api.Upload(path.Join(testDir, "test0"))
|
bzzhash, err := api.Upload(path.Join(testDir, "test0"), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
|
|
@ -101,7 +101,7 @@ func TestApiFileUpload(t *testing.T) {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bzzhash, err := api.Upload(path.Join(testDir, "test0", "index.html"))
|
bzzhash, err := api.Upload(path.Join(testDir, "test0", "index.html"), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ delimiter='{"entries":[{'
|
||||||
|
|
||||||
if [ -f "$1" ]; then
|
if [ -f "$1" ]; then
|
||||||
hash=`wget -q -O- --post-file="$1" http://localhost:8500/raw`
|
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
|
wget -q -O- --post-data="$delimiter\"hash\":\"$hash\",\"contentType\":\"$mime\"}]}" http://localhost:8500/raw
|
||||||
echo
|
echo
|
||||||
|
|
||||||
|
|
@ -21,7 +21,7 @@ pushd "$bzzroot" > /dev/null
|
||||||
|
|
||||||
(for path in `find . -type f`
|
(for path in `find . -type f`
|
||||||
do
|
do
|
||||||
name=`echo "$path" | cut -c2-`
|
name=`echo "$path" | cut -c3-`
|
||||||
[ _`basename "$name"` = "_$INDEX" ] && name=`dirname "$name"`
|
[ _`basename "$name"` = "_$INDEX" ] && name=`dirname "$name"`
|
||||||
echo -n "$delimiter"
|
echo -n "$delimiter"
|
||||||
hash=`wget -q -O- --post-file="$path" http://localhost:8500/raw`
|
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 {
|
func (self *JSApi) upload(call otto.FunctionCall) otto.Value {
|
||||||
if len(call.ArgumentList) != 1 {
|
var err error
|
||||||
fmt.Println("requires 1 arguments: bzz.put(localpath)")
|
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()
|
return otto.UndefinedValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
|
||||||
var localpath, res string
|
var localpath, res string
|
||||||
localpath, err = call.Argument(0).ToString()
|
localpath, err = call.Argument(0).ToString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -195,7 +202,7 @@ func (self *JSApi) upload(call otto.FunctionCall) otto.Value {
|
||||||
return otto.UndefinedValue()
|
return otto.UndefinedValue()
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err = self.api.Upload(localpath)
|
res, err = self.api.Upload(localpath, index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return otto.UndefinedValue()
|
return otto.UndefinedValue()
|
||||||
|
|
|
||||||
|
|
@ -262,8 +262,8 @@ func (self *manifestTrie) getEntry(spath string) (entry *manifestTrieEntry, full
|
||||||
path := regularSlashes(spath)
|
path := regularSlashes(spath)
|
||||||
var pos int
|
var pos int
|
||||||
entry, pos = self.findPrefixOf(path)
|
entry, pos = self.findPrefixOf(path)
|
||||||
if (pos > 0) && (pos < len(path)) && (path[pos] != '/') {
|
/*if (pos > 0) && (pos < len(path)) && (path[pos] != '/') {
|
||||||
return nil, ""
|
return nil, ""
|
||||||
}
|
}*/
|
||||||
return entry, path[:pos]
|
return entry, path[:pos]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue