mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 22:56:43 +00:00
swarm: upload from stdin
Indended to be a swarm alternative to termbin.com added --stdin flag to swarm executable if set, swarm will read data from stdin and postRaw this currently the data limit is hardcoded to 1MB the content-type in manifest is hardcoded to text/plain
This commit is contained in:
parent
1a00e39539
commit
16970b2bf1
2 changed files with 81 additions and 24 deletions
|
|
@ -112,6 +112,10 @@ var (
|
||||||
Name: "defaultpath",
|
Name: "defaultpath",
|
||||||
Usage: "path to file served for empty url path (none)",
|
Usage: "path to file served for empty url path (none)",
|
||||||
}
|
}
|
||||||
|
SwarmUpFromStdinFlag = cli.BoolFlag{
|
||||||
|
Name: "stdin",
|
||||||
|
Usage: "reads data to be uploaded from stdin",
|
||||||
|
}
|
||||||
CorsStringFlag = cli.StringFlag{
|
CorsStringFlag = cli.StringFlag{
|
||||||
Name: "corsdomain",
|
Name: "corsdomain",
|
||||||
Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
|
Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
|
||||||
|
|
@ -235,6 +239,7 @@ Cleans database of corrupted entries.
|
||||||
SwarmRecursiveUploadFlag,
|
SwarmRecursiveUploadFlag,
|
||||||
SwarmWantManifestFlag,
|
SwarmWantManifestFlag,
|
||||||
SwarmUploadDefaultPath,
|
SwarmUploadDefaultPath,
|
||||||
|
SwarmUpFromStdinFlag,
|
||||||
}
|
}
|
||||||
app.Flags = append(app.Flags, debug.Flags...)
|
app.Flags = append(app.Flags, debug.Flags...)
|
||||||
app.Before = func(ctx *cli.Context) error {
|
app.Before = func(ctx *cli.Context) error {
|
||||||
|
|
|
||||||
|
|
@ -30,49 +30,101 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/cmd/utils"
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"gopkg.in/urfave/cli.v1"
|
"gopkg.in/urfave/cli.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getStdin(b []byte) (int, error) {
|
||||||
|
s := os.Stdin
|
||||||
|
return s.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var client = &client{api: bzzapi}
|
||||||
|
|
||||||
|
var entry manifestEntry
|
||||||
|
|
||||||
|
data := make([]byte, 1024*1000)
|
||||||
|
datalength := int(0)
|
||||||
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
utils.Fatalf("Need filename as the first and only argument")
|
if fromStdin {
|
||||||
|
var err error
|
||||||
|
datalength, err = getStdin(data)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Read from stdin failed")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
utils.Fatalf("Need filename as the first and only argument")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// if we have data already (stdin)
|
||||||
file = args[0]
|
if datalength > 0 {
|
||||||
client = &client{api: bzzapi}
|
|
||||||
)
|
var hash string
|
||||||
fi, err := os.Stat(expandPath(file))
|
wg := &sync.WaitGroup{}
|
||||||
if err != nil {
|
|
||||||
utils.Fatalf("Failed to stat file: %v", err)
|
pr, pw := io.Pipe()
|
||||||
}
|
|
||||||
if fi.IsDir() {
|
go func(hash *string, wg *sync.WaitGroup) {
|
||||||
if !recursive {
|
wg.Add(1)
|
||||||
utils.Fatalf("Argument is a directory and recursive upload is disabled")
|
mhash, err := client.postRaw("text/plain", int64(datalength), pr)
|
||||||
}
|
*hash = mhash
|
||||||
if !wantManifest {
|
if err != nil {
|
||||||
utils.Fatalf("Manifest is required for directory uploads")
|
utils.Fatalf("Failed to post data: %v", err)
|
||||||
}
|
}
|
||||||
mhash, err := client.uploadDirectory(file, defaultPath)
|
wg.Done()
|
||||||
|
}(&hash, wg)
|
||||||
|
|
||||||
|
_, err := pw.Write(data[:datalength])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Failed to upload directory: %v", err)
|
utils.Fatalf("Failed to write to HTTP pipe: %v", err)
|
||||||
|
}
|
||||||
|
pw.Close()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
entry.Hash = hash
|
||||||
|
entry.ContentType = "text/plain"
|
||||||
|
|
||||||
|
} else {
|
||||||
|
file := args[0]
|
||||||
|
fi, err := os.Stat(expandPath(file))
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to stat file: %v", err)
|
||||||
|
}
|
||||||
|
if fi.IsDir() {
|
||||||
|
if !recursive {
|
||||||
|
utils.Fatalf("Argument is a directory and recursive upload is disabled")
|
||||||
|
}
|
||||||
|
if !wantManifest {
|
||||||
|
utils.Fatalf("Manifest is required for directory uploads")
|
||||||
|
}
|
||||||
|
mhash, err := client.uploadDirectory(file, defaultPath)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to upload directory: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println(mhash)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entry, err = client.uploadFile(file, fi)
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Upload failed: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println(mhash)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
entry, err := client.uploadFile(file, fi)
|
|
||||||
if err != nil {
|
|
||||||
utils.Fatalf("Upload failed: %v", err)
|
|
||||||
}
|
}
|
||||||
mroot := manifest{[]manifestEntry{entry}}
|
mroot := manifest{[]manifestEntry{entry}}
|
||||||
if !wantManifest {
|
if !wantManifest {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue