|
|
@ -27,6 +27,8 @@ import ( |
|
|
|
"mime" |
|
|
|
"mime" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
"os/user" |
|
|
|
|
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
@ -39,6 +41,7 @@ func upload(ctx *cli.Context) { |
|
|
|
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") |
|
|
|
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") |
|
|
|
recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name) |
|
|
|
recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name) |
|
|
|
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) |
|
|
|
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) |
|
|
|
|
|
|
|
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name) |
|
|
|
) |
|
|
|
) |
|
|
|
if len(args) != 1 { |
|
|
|
if len(args) != 1 { |
|
|
|
log.Fatal("need filename as the first and only argument") |
|
|
|
log.Fatal("need filename as the first and only argument") |
|
|
@ -48,8 +51,9 @@ func upload(ctx *cli.Context) { |
|
|
|
file = args[0] |
|
|
|
file = args[0] |
|
|
|
client = &client{api: bzzapi} |
|
|
|
client = &client{api: bzzapi} |
|
|
|
mroot manifest |
|
|
|
mroot manifest |
|
|
|
|
|
|
|
entry manifestEntry |
|
|
|
) |
|
|
|
) |
|
|
|
fi, err := os.Stat(file) |
|
|
|
fi, err := os.Stat(expandPath(file)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
@ -57,28 +61,49 @@ func upload(ctx *cli.Context) { |
|
|
|
if !recursive { |
|
|
|
if !recursive { |
|
|
|
log.Fatal("argument is a directory and recursive upload is disabled") |
|
|
|
log.Fatal("argument is a directory and recursive upload is disabled") |
|
|
|
} |
|
|
|
} |
|
|
|
mroot, err = client.uploadDirectory(file) |
|
|
|
mroot, err = client.uploadDirectory(file, defaultPath) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mroot, err = client.uploadFile(file, fi) |
|
|
|
entry, err = client.uploadFile(file, fi) |
|
|
|
if wantManifest { |
|
|
|
mroot = manifest{[]manifestEntry{entry}} |
|
|
|
// Wrap the raw file entry in a proper manifest so both hashes get printed.
|
|
|
|
|
|
|
|
mroot = manifest{Entries: []manifest{mroot}} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatalln("upload failed:", err) |
|
|
|
log.Fatalln("upload failed:", err) |
|
|
|
} |
|
|
|
} |
|
|
|
if wantManifest { |
|
|
|
if !wantManifest { |
|
|
|
hash, err := client.uploadManifest(mroot) |
|
|
|
// Print the manifest. This is the only output to stdout.
|
|
|
|
if err != nil { |
|
|
|
mrootJSON, _ := json.MarshalIndent(mroot, "", " ") |
|
|
|
log.Fatalln("manifest upload failed:", err) |
|
|
|
fmt.Println(string(mrootJSON)) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
hash, err := client.uploadManifest(mroot) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatalln("manifest upload failed:", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println(hash) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Expands a file path
|
|
|
|
|
|
|
|
// 1. replace tilde with users home dir
|
|
|
|
|
|
|
|
// 2. expands embedded environment variables
|
|
|
|
|
|
|
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
|
|
|
|
|
|
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
|
|
|
|
|
|
|
func expandPath(p string) string { |
|
|
|
|
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") { |
|
|
|
|
|
|
|
if home := homeDir(); home != "" { |
|
|
|
|
|
|
|
p = home + p[1:] |
|
|
|
} |
|
|
|
} |
|
|
|
mroot.Hash = hash |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return path.Clean(os.ExpandEnv(p)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Print the manifest. This is the only output to stdout.
|
|
|
|
func homeDir() string { |
|
|
|
mrootJSON, _ := json.MarshalIndent(mroot, "", " ") |
|
|
|
if home := os.Getenv("HOME"); home != "" { |
|
|
|
fmt.Println(string(mrootJSON)) |
|
|
|
return home |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if usr, err := user.Current(); err == nil { |
|
|
|
|
|
|
|
return usr.HomeDir |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return "" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// client wraps interaction with the swarm HTTP gateway.
|
|
|
|
// client wraps interaction with the swarm HTTP gateway.
|
|
|
@ -86,25 +111,41 @@ type client struct { |
|
|
|
api string |
|
|
|
api string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// manifest is the JSON representation of a swarm manifest.
|
|
|
|
|
|
|
|
type manifestEntry struct { |
|
|
|
|
|
|
|
Hash string `json:"hash,omitempty"` |
|
|
|
|
|
|
|
ContentType string `json:"contentType,omitempty"` |
|
|
|
|
|
|
|
Path string `json:"path,omitempty"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// manifest is the JSON representation of a swarm manifest.
|
|
|
|
// manifest is the JSON representation of a swarm manifest.
|
|
|
|
type manifest struct { |
|
|
|
type manifest struct { |
|
|
|
Hash string `json:"hash,omitempty"` |
|
|
|
Entries []manifestEntry `json:"entries,omitempty"` |
|
|
|
ContentType string `json:"contentType,omitempty"` |
|
|
|
|
|
|
|
Path string `json:"path,omitempty"` |
|
|
|
|
|
|
|
Entries []manifest `json:"entries,omitempty"` |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *client) uploadFile(file string, fi os.FileInfo) (manifest, error) { |
|
|
|
func (c *client) uploadFile(file string, fi os.FileInfo) (manifestEntry, error) { |
|
|
|
hash, err := c.uploadFileContent(file, fi) |
|
|
|
hash, err := c.uploadFileContent(file, fi) |
|
|
|
m := manifest{ |
|
|
|
m := manifestEntry{ |
|
|
|
Hash: hash, |
|
|
|
Hash: hash, |
|
|
|
ContentType: mime.TypeByExtension(filepath.Ext(fi.Name())), |
|
|
|
ContentType: mime.TypeByExtension(filepath.Ext(fi.Name())), |
|
|
|
} |
|
|
|
} |
|
|
|
return m, err |
|
|
|
return m, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *client) uploadDirectory(dir string) (manifest, error) { |
|
|
|
func (c *client) uploadDirectory(dir string, defaultPath string) (manifest, error) { |
|
|
|
dirm := manifest{} |
|
|
|
dirm := manifest{} |
|
|
|
|
|
|
|
if len(defaultPath) > 0 { |
|
|
|
|
|
|
|
fi, err := os.Stat(defaultPath) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
entry, err := c.uploadFile(defaultPath, fi) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
log.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
entry.Path = "" |
|
|
|
|
|
|
|
dirm.Entries = append(dirm.Entries, entry) |
|
|
|
|
|
|
|
} |
|
|
|
prefix := filepath.ToSlash(filepath.Clean(dir)) + "/" |
|
|
|
prefix := filepath.ToSlash(filepath.Clean(dir)) + "/" |
|
|
|
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { |
|
|
|
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { |
|
|
|
if err != nil || fi.IsDir() { |
|
|
|
if err != nil || fi.IsDir() { |
|
|
|