|
|
@ -20,6 +20,8 @@ package main |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"io" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"os/user" |
|
|
|
"os/user" |
|
|
|
"path" |
|
|
|
"path" |
|
|
@ -31,21 +33,42 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func upload(ctx *cli.Context) { |
|
|
|
func upload(ctx *cli.Context) { |
|
|
|
|
|
|
|
|
|
|
|
args := ctx.Args() |
|
|
|
args := ctx.Args() |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
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) |
|
|
|
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name) |
|
|
|
|
|
|
|
fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name) |
|
|
|
|
|
|
|
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var client = swarm.NewClient(bzzapi) |
|
|
|
|
|
|
|
var entry swarm.ManifestEntry |
|
|
|
|
|
|
|
var file string |
|
|
|
|
|
|
|
|
|
|
|
if len(args) != 1 { |
|
|
|
if len(args) != 1 { |
|
|
|
utils.Fatalf("Need filename as the first and only argument") |
|
|
|
if fromStdin { |
|
|
|
|
|
|
|
tmp, err := ioutil.TempFile("", "swarm-stdin") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
utils.Fatalf("error create tempfile: %s", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
defer os.Remove(tmp.Name()) |
|
|
|
|
|
|
|
n, err := io.Copy(tmp, os.Stdin) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
utils.Fatalf("error copying stdin to tempfile: %s", err) |
|
|
|
|
|
|
|
} else if n == 0 { |
|
|
|
|
|
|
|
utils.Fatalf("error reading from stdin: zero length") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
file = tmp.Name() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
utils.Fatalf("Need filename as the first and only argument") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
file = args[0] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
|
|
file = args[0] |
|
|
|
|
|
|
|
client = swarm.NewClient(bzzapi) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
fi, err := os.Stat(expandPath(file)) |
|
|
|
fi, err := os.Stat(expandPath(file)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
utils.Fatalf("Failed to stat file: %v", err) |
|
|
|
utils.Fatalf("Failed to stat file: %v", err) |
|
|
@ -64,7 +87,7 @@ func upload(ctx *cli.Context) { |
|
|
|
fmt.Println(mhash) |
|
|
|
fmt.Println(mhash) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
entry, err := client.UploadFile(file, fi) |
|
|
|
entry, err = client.UploadFile(file, fi, mimeType) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
utils.Fatalf("Upload failed: %v", err) |
|
|
|
utils.Fatalf("Upload failed: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|