Skip to content

Commit 71fdaa4

Browse files
lmarsfjl
authored andcommitted
swarm/api: refactor and improve HTTP API (bnb-chain#3773)
This PR deprecates the file related RPC calls in favour of an improved HTTP API. The main aim is to expose a simple to use API which can be consumed by thin clients (e.g. curl and HTML forms) without the need for complex logic (e.g. manipulating prefix trie manifests).
1 parent 9aca9e6 commit 71fdaa4

20 files changed

Lines changed: 1779 additions & 663 deletions

‎cmd/swarm/list.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@ func list(ctx *cli.Context) {
4444

4545
bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
4646
client := swarm.NewClient(bzzapi)
47-
entries, err := client.ManifestFileList(manifest, prefix)
47+
list, err := client.List(manifest, prefix)
4848
if err != nil {
4949
utils.Fatalf("Failed to generate file and directory list: %s", err)
5050
}
5151

5252
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
5353
defer w.Flush()
5454
fmt.Fprintln(w, "HASH\tCONTENT TYPE\tPATH")
55-
for _, entry := range entries {
55+
for _, prefix := range list.CommonPrefixes {
56+
fmt.Fprintf(w, "%s\t%s\t%s\n", "", "DIR", prefix)
57+
}
58+
for _, entry := range list.Entries {
5659
fmt.Fprintf(w, "%s\t%s\t%s\n", entry.Hash, entry.ContentType, entry.Path)
5760
}
5861
}

‎cmd/swarm/manifest.go‎

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"strings"
2626

2727
"github.com/ethereum/go-ethereum/cmd/utils"
28+
"github.com/ethereum/go-ethereum/swarm/api"
2829
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
2930
"gopkg.in/urfave/cli.v1"
3031
)
@@ -42,7 +43,7 @@ func add(ctx *cli.Context) {
4243

4344
ctype string
4445
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
45-
mroot swarm.Manifest
46+
mroot api.Manifest
4647
)
4748

4849
if len(args) > 3 {
@@ -76,7 +77,7 @@ func update(ctx *cli.Context) {
7677

7778
ctype string
7879
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
79-
mroot swarm.Manifest
80+
mroot api.Manifest
8081
)
8182
if len(args) > 3 {
8283
ctype = args[3]
@@ -106,7 +107,7 @@ func remove(ctx *cli.Context) {
106107
path = args[1]
107108

108109
wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name)
109-
mroot swarm.Manifest
110+
mroot api.Manifest
110111
)
111112

112113
newManifest := removeEntryFromManifest(ctx, mhash, path)
@@ -125,11 +126,7 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
125126
var (
126127
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
127128
client = swarm.NewClient(bzzapi)
128-
longestPathEntry = swarm.ManifestEntry{
129-
Path: "",
130-
Hash: "",
131-
ContentType: "",
132-
}
129+
longestPathEntry = api.ManifestEntry{}
133130
)
134131

135132
mroot, err := client.DownloadManifest(mhash)
@@ -163,7 +160,7 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
163160
newHash := addEntryToManifest(ctx, longestPathEntry.Hash, newPath, hash, ctype)
164161

165162
// Replace the hash for parent Manifests
166-
newMRoot := swarm.Manifest{}
163+
newMRoot := &api.Manifest{}
167164
for _, entry := range mroot.Entries {
168165
if longestPathEntry.Path == entry.Path {
169166
entry.Hash = newHash
@@ -173,9 +170,9 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
173170
mroot = newMRoot
174171
} else {
175172
// Add the entry in the leaf Manifest
176-
newEntry := swarm.ManifestEntry{
177-
Path: path,
173+
newEntry := api.ManifestEntry{
178174
Hash: hash,
175+
Path: path,
179176
ContentType: ctype,
180177
}
181178
mroot.Entries = append(mroot.Entries, newEntry)
@@ -192,18 +189,10 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
192189
func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) string {
193190

194191
var (
195-
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
196-
client = swarm.NewClient(bzzapi)
197-
newEntry = swarm.ManifestEntry{
198-
Path: "",
199-
Hash: "",
200-
ContentType: "",
201-
}
202-
longestPathEntry = swarm.ManifestEntry{
203-
Path: "",
204-
Hash: "",
205-
ContentType: "",
206-
}
192+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
193+
client = swarm.NewClient(bzzapi)
194+
newEntry = api.ManifestEntry{}
195+
longestPathEntry = api.ManifestEntry{}
207196
)
208197

209198
mroot, err := client.DownloadManifest(mhash)
@@ -237,7 +226,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
237226
newHash := updateEntryInManifest(ctx, longestPathEntry.Hash, newPath, hash, ctype)
238227

239228
// Replace the hash for parent Manifests
240-
newMRoot := swarm.Manifest{}
229+
newMRoot := &api.Manifest{}
241230
for _, entry := range mroot.Entries {
242231
if longestPathEntry.Path == entry.Path {
243232
entry.Hash = newHash
@@ -250,12 +239,12 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
250239

251240
if newEntry.Path != "" {
252241
// Replace the hash for leaf Manifest
253-
newMRoot := swarm.Manifest{}
242+
newMRoot := &api.Manifest{}
254243
for _, entry := range mroot.Entries {
255244
if newEntry.Path == entry.Path {
256-
myEntry := swarm.ManifestEntry{
257-
Path: entry.Path,
245+
myEntry := api.ManifestEntry{
258246
Hash: hash,
247+
Path: entry.Path,
259248
ContentType: ctype,
260249
}
261250
newMRoot.Entries = append(newMRoot.Entries, myEntry)
@@ -276,18 +265,10 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
276265
func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
277266

278267
var (
279-
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
280-
client = swarm.NewClient(bzzapi)
281-
entryToRemove = swarm.ManifestEntry{
282-
Path: "",
283-
Hash: "",
284-
ContentType: "",
285-
}
286-
longestPathEntry = swarm.ManifestEntry{
287-
Path: "",
288-
Hash: "",
289-
ContentType: "",
290-
}
268+
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
269+
client = swarm.NewClient(bzzapi)
270+
entryToRemove = api.ManifestEntry{}
271+
longestPathEntry = api.ManifestEntry{}
291272
)
292273

293274
mroot, err := client.DownloadManifest(mhash)
@@ -319,7 +300,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
319300
newHash := removeEntryFromManifest(ctx, longestPathEntry.Hash, newPath)
320301

321302
// Replace the hash for parent Manifests
322-
newMRoot := swarm.Manifest{}
303+
newMRoot := &api.Manifest{}
323304
for _, entry := range mroot.Entries {
324305
if longestPathEntry.Path == entry.Path {
325306
entry.Hash = newHash
@@ -331,7 +312,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
331312

332313
if entryToRemove.Path != "" {
333314
// remove the entry in this Manifest
334-
newMRoot := swarm.Manifest{}
315+
newMRoot := &api.Manifest{}
335316
for _, entry := range mroot.Entries {
336317
if entryToRemove.Path != entry.Path {
337318
newMRoot.Entries = append(newMRoot.Entries, entry)

‎cmd/swarm/upload.go‎

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
package main
1919

2020
import (
21-
"encoding/json"
2221
"fmt"
2322
"io"
2423
"io/ioutil"
24+
"mime"
25+
"net/http"
2526
"os"
2627
"os/user"
2728
"path"
29+
"path/filepath"
2830
"strings"
2931

3032
"github.com/ethereum/go-ethereum/cmd/utils"
@@ -42,12 +44,10 @@ func upload(ctx *cli.Context) {
4244
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
4345
fromStdin = ctx.GlobalBool(SwarmUpFromStdinFlag.Name)
4446
mimeType = ctx.GlobalString(SwarmUploadMimeType.Name)
47+
client = swarm.NewClient(bzzapi)
48+
file string
4549
)
4650

47-
var client = swarm.NewClient(bzzapi)
48-
var entry swarm.ManifestEntry
49-
var file string
50-
5151
if len(args) != 1 {
5252
if fromStdin {
5353
tmp, err := ioutil.TempFile("", "swarm-stdin")
@@ -66,41 +66,47 @@ func upload(ctx *cli.Context) {
6666
utils.Fatalf("Need filename as the first and only argument")
6767
}
6868
} else {
69-
file = args[0]
69+
file = expandPath(args[0])
70+
}
71+
72+
if !wantManifest {
73+
f, err := swarm.Open(file)
74+
if err != nil {
75+
utils.Fatalf("Error opening file: %s", err)
76+
}
77+
defer f.Close()
78+
hash, err := client.UploadRaw(f, f.Size)
79+
if err != nil {
80+
utils.Fatalf("Upload failed: %s", err)
81+
}
82+
fmt.Println(hash)
83+
return
7084
}
7185

72-
fi, err := os.Stat(expandPath(file))
86+
stat, err := os.Stat(file)
7387
if err != nil {
74-
utils.Fatalf("Failed to stat file: %v", err)
88+
utils.Fatalf("Error opening file: %s", err)
7589
}
76-
if fi.IsDir() {
90+
var hash string
91+
if stat.IsDir() {
7792
if !recursive {
7893
utils.Fatalf("Argument is a directory and recursive upload is disabled")
7994
}
80-
if !wantManifest {
81-
utils.Fatalf("Manifest is required for directory uploads")
95+
hash, err = client.UploadDirectory(file, defaultPath, "")
96+
} else {
97+
if mimeType == "" {
98+
mimeType = detectMimeType(file)
8299
}
83-
mhash, err := client.UploadDirectory(file, defaultPath)
100+
f, err := swarm.Open(file)
84101
if err != nil {
85-
utils.Fatalf("Failed to upload directory: %v", err)
102+
utils.Fatalf("Error opening file: %s", err)
86103
}
87-
fmt.Println(mhash)
88-
return
104+
defer f.Close()
105+
f.ContentType = mimeType
106+
hash, err = client.Upload(f, "")
89107
}
90-
entry, err = client.UploadFile(file, fi, mimeType)
91108
if err != nil {
92-
utils.Fatalf("Upload failed: %v", err)
93-
}
94-
mroot := swarm.Manifest{Entries: []swarm.ManifestEntry{entry}}
95-
if !wantManifest {
96-
// Print the manifest. This is the only output to stdout.
97-
mrootJSON, _ := json.MarshalIndent(mroot, "", " ")
98-
fmt.Println(string(mrootJSON))
99-
return
100-
}
101-
hash, err := client.UploadManifest(mroot)
102-
if err != nil {
103-
utils.Fatalf("Manifest upload failed: %v", err)
109+
utils.Fatalf("Upload failed: %s", err)
104110
}
105111
fmt.Println(hash)
106112
}
@@ -128,3 +134,19 @@ func homeDir() string {
128134
}
129135
return ""
130136
}
137+
138+
func detectMimeType(file string) string {
139+
if ext := filepath.Ext(file); ext != "" {
140+
return mime.TypeByExtension(ext)
141+
}
142+
f, err := os.Open(file)
143+
if err != nil {
144+
return ""
145+
}
146+
defer f.Close()
147+
buf := make([]byte, 512)
148+
if n, _ := f.Read(buf); n > 0 {
149+
return http.DetectContentType(buf)
150+
}
151+
return ""
152+
}

0 commit comments

Comments
 (0)