mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
Api.Upload supports single file uploads
using mimetype command matching prefixes regardless of slashes
This commit is contained in:
parent
017af45af5
commit
69e7333e25
2 changed files with 52 additions and 28 deletions
76
bzz/api.go
76
bzz/api.go
|
|
@ -160,35 +160,58 @@ const maxParallelFiles = 5
|
|||
// TODO: localpath should point to a manifest
|
||||
func (self *Api) Upload(lpath 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() {
|
||||
//fmt.Printf("lp %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)
|
||||
|
|
@ -213,7 +236,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()
|
||||
|
|
|
|||
|
|
@ -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