Skip to content

Commit 0ec1104

Browse files
nolashfjl
authored andcommitted
cmd/swarm: allow uploading from stdin (bnb-chain#3744)
- intended to be a swarm alternative to termbin.com - added --stdin flag to swarm executable. if set, swarm will read data from stdin and postRaw it.
1 parent c76ad94 commit 0ec1104

3 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎cmd/swarm/main.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ var (
112112
Name: "defaultpath",
113113
Usage: "path to file served for empty url path (none)",
114114
}
115+
SwarmUpFromStdinFlag = cli.BoolFlag{
116+
Name: "stdin",
117+
Usage: "reads data to be uploaded from stdin",
118+
}
119+
SwarmUploadMimeType = cli.StringFlag{
120+
Name: "mime",
121+
Usage: "force mime type",
122+
}
115123
CorsStringFlag = cli.StringFlag{
116124
Name: "corsdomain",
117125
Usage: "Domain on which to send Access-Control-Allow-Origin header (multiple domains can be supplied separated by a ',')",
@@ -244,6 +252,8 @@ Cleans database of corrupted entries.
244252
SwarmRecursiveUploadFlag,
245253
SwarmWantManifestFlag,
246254
SwarmUploadDefaultPath,
255+
SwarmUpFromStdinFlag,
256+
SwarmUploadMimeType,
247257
}
248258
app.Flags = append(app.Flags, debug.Flags...)
249259
app.Before = func(ctx *cli.Context) error {

‎cmd/swarm/upload.go‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package main
2020
import (
2121
"encoding/json"
2222
"fmt"
23+
"io"
24+
"io/ioutil"
2325
"os"
2426
"os/user"
2527
"path"
@@ -31,21 +33,42 @@ import (
3133
)
3234

3335
func upload(ctx *cli.Context) {
36+
3437
args := ctx.Args()
3538
var (
3639
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
3740
recursive = ctx.GlobalBool(SwarmRecursiveUploadFlag.Name)
3841
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
3942
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
43+
fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
44+
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
4045
)
46+
47+
var client = swarm.NewClient(bzzapi)
48+
var entry swarm.ManifestEntry
49+
var file string
50+
4151
if len(args) != 1 {
42-
utils.Fatalf("Need filename as the first and only argument")
52+
if fromStdin {
53+
tmp, err := ioutil.TempFile("", "swarm-stdin")
54+
if err != nil {
55+
utils.Fatalf("error create tempfile: %s", err)
56+
}
57+
defer os.Remove(tmp.Name())
58+
n, err := io.Copy(tmp, os.Stdin)
59+
if err != nil {
60+
utils.Fatalf("error copying stdin to tempfile: %s", err)
61+
} else if n == 0 {
62+
utils.Fatalf("error reading from stdin: zero length")
63+
}
64+
file = tmp.Name()
65+
} else {
66+
utils.Fatalf("Need filename as the first and only argument")
67+
}
68+
} else {
69+
file = args[0]
4370
}
4471

45-
var (
46-
file = args[0]
47-
client = swarm.NewClient(bzzapi)
48-
)
4972
fi, err := os.Stat(expandPath(file))
5073
if err != nil {
5174
utils.Fatalf("Failed to stat file: %v", err)
@@ -64,7 +87,7 @@ func upload(ctx *cli.Context) {
6487
fmt.Println(mhash)
6588
return
6689
}
67-
entry, err := client.UploadFile(file, fi)
90+
entry, err = client.UploadFile(file, fi, mimeType)
6891
if err != nil {
6992
utils.Fatalf("Upload failed: %v", err)
7093
}

‎swarm/api/client/client.go‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,32 @@ func (c *Client) UploadDirectory(dir string, defaultPath string) (string, error)
8989
return mhash, err
9090
}
9191

92-
func (c *Client) UploadFile(file string, fi os.FileInfo) (ManifestEntry, error) {
92+
func (c *Client) UploadFile(file string, fi os.FileInfo, mimetype_hint string) (ManifestEntry, error) {
93+
var mimetype string
9394
hash, err := c.uploadFileContent(file, fi)
95+
if mimetype_hint != "" {
96+
mimetype = mimetype_hint
97+
log.Info("Mime type set by override", "mime", mimetype)
98+
} else {
99+
ext := filepath.Ext(file)
100+
log.Info("Ext", "ext", ext, "file", file)
101+
if ext != "" {
102+
mimetype = mime.TypeByExtension(filepath.Ext(fi.Name()))
103+
log.Info("Mime type set by fileextension", "mime", mimetype, "ext", filepath.Ext(file))
104+
} else {
105+
f, err := os.Open(file)
106+
if err == nil {
107+
first512 := make([]byte, 512)
108+
fread, _ := f.ReadAt(first512, 0)
109+
if fread > 0 {
110+
mimetype = http.DetectContentType(first512[:fread])
111+
log.Info("Mime type set by autodetection", "mime", mimetype)
112+
}
113+
}
114+
f.Close()
115+
}
116+
117+
}
94118
m := ManifestEntry{
95119
Hash: hash,
96120
ContentType: mime.TypeByExtension(filepath.Ext(fi.Name())),

0 commit comments

Comments
 (0)