Skip to content

Commit bbf37c0

Browse files
committed
cmd/bzzup: add swarm upload tool
1 parent e336b0d commit bbf37c0

1 file changed

Lines changed: 161 additions & 0 deletions

File tree

‎cmd/bzzup/main.go‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Command bzzup uploads files to the swarm HTTP API.
18+
package main
19+
20+
import (
21+
"bytes"
22+
"encoding/json"
23+
"flag"
24+
"fmt"
25+
"io"
26+
"io/ioutil"
27+
"log"
28+
"mime"
29+
"net/http"
30+
"os"
31+
"path/filepath"
32+
"strings"
33+
)
34+
35+
func main() {
36+
var (
37+
bzzapiFlag = flag.String("bzzapi", "http://127.0.0.1:8500", "Swarm HTTP endpoint")
38+
recursiveFlag = flag.Bool("recursive", false, "Upload directories recursively")
39+
manifestFlag = flag.Bool("manifest", true, "Skip automatic manifest upload")
40+
)
41+
log.SetOutput(os.Stderr)
42+
log.SetFlags(0)
43+
flag.Parse()
44+
if flag.NArg() != 1 {
45+
log.Fatal("need filename as the first and only argument")
46+
}
47+
48+
var (
49+
file = flag.Arg(0)
50+
client = &client{api: *bzzapiFlag}
51+
mroot manifest
52+
)
53+
fi, err := os.Stat(file)
54+
if err != nil {
55+
log.Fatal(err)
56+
}
57+
if fi.IsDir() {
58+
if !*recursiveFlag {
59+
log.Fatal("argument is a directory and recursive upload is disabled")
60+
}
61+
mroot, err = client.uploadDirectory(file)
62+
} else {
63+
mroot, err = client.uploadFile(file, fi)
64+
if *manifestFlag {
65+
// Wrap the raw file entry in a proper manifest so both hashes get printed.
66+
mroot = manifest{Entries: []manifest{mroot}}
67+
}
68+
}
69+
if err != nil {
70+
log.Fatalln("upload failed:", err)
71+
}
72+
if *manifestFlag {
73+
hash, err := client.uploadManifest(mroot)
74+
if err != nil {
75+
log.Fatalln("manifest upload failed:", err)
76+
}
77+
mroot.Hash = hash
78+
}
79+
80+
// Print the manifest. This is the only output to stdout.
81+
mrootJSON, _ := json.MarshalIndent(mroot, "", " ")
82+
fmt.Println(string(mrootJSON))
83+
}
84+
85+
// client wraps interaction with the swarm HTTP gateway.
86+
type client struct {
87+
api string
88+
}
89+
90+
// manifest is the JSON representation of a swarm manifest.
91+
type manifest struct {
92+
Hash string `json:"hash,omitempty"`
93+
ContentType string `json:"contentType,omitempty"`
94+
Path string `json:"path,omitempty"`
95+
Entries []manifest `json:"entries,omitempty"`
96+
}
97+
98+
func (c *client) uploadFile(file string, fi os.FileInfo) (manifest, error) {
99+
hash, err := c.uploadFileContent(file, fi)
100+
m := manifest{
101+
Hash: hash,
102+
ContentType: mime.TypeByExtension(filepath.Ext(fi.Name())),
103+
}
104+
return m, err
105+
}
106+
107+
func (c *client) uploadDirectory(dir string) (manifest, error) {
108+
dirm := manifest{}
109+
prefix := filepath.ToSlash(dir) + "/"
110+
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
111+
if err != nil || fi.IsDir() {
112+
return err
113+
}
114+
if !strings.HasPrefix(path, dir) {
115+
return fmt.Errorf("path %s outside directory %s", path, dir)
116+
}
117+
entry, err := c.uploadFile(path, fi)
118+
entry.Path = strings.TrimPrefix(filepath.ToSlash(path), prefix)
119+
dirm.Entries = append(dirm.Entries, entry)
120+
return err
121+
})
122+
return dirm, err
123+
}
124+
125+
func (c *client) uploadFileContent(file string, fi os.FileInfo) (string, error) {
126+
fd, err := os.Open(file)
127+
if err != nil {
128+
return "", err
129+
}
130+
defer fd.Close()
131+
log.Printf("uploading file %s (%d bytes)", file, fi.Size())
132+
return c.postRaw("application/octet-stream", fi.Size(), fd)
133+
}
134+
135+
func (c *client) uploadManifest(m manifest) (string, error) {
136+
jsm, err := json.Marshal(m)
137+
if err != nil {
138+
panic(err)
139+
}
140+
log.Println("uploading manifest")
141+
return c.postRaw("application/json", int64(len(jsm)), ioutil.NopCloser(bytes.NewReader(jsm)))
142+
}
143+
144+
func (c *client) postRaw(mimetype string, size int64, body io.ReadCloser) (string, error) {
145+
req, err := http.NewRequest("POST", c.api+"/bzzr:/", body)
146+
if err != nil {
147+
return "", err
148+
}
149+
req.Header.Set("content-type", mimetype)
150+
req.ContentLength = size
151+
resp, err := http.DefaultClient.Do(req)
152+
if err != nil {
153+
return "", err
154+
}
155+
defer resp.Body.Close()
156+
if resp.StatusCode >= 400 {
157+
return "", fmt.Errorf("bad status: %s", resp.Status)
158+
}
159+
content, err := ioutil.ReadAll(resp.Body)
160+
return string(content), err
161+
}

0 commit comments

Comments
 (0)