commands(feat): use the coreapi in the urlstore command
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Apr 25, 2019 at 17:15 UTC
467beea940b549c5434775423dbb483818c1698b
1 file changed
+25
-58
core/commands/urlstore.go
+25
-58
@@ -3,20 +3,15 @@ package commands
3
import (
4
"fmt"
5
"io"
6
- "net/http"
6
+ "net/url"
7
8
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
filestore "github.com/ipfs/go-ipfs/filestore"
10
- pin "github.com/ipfs/go-ipfs/pin"
10
12
- cid "github.com/ipfs/go-cid"
13
- chunk "github.com/ipfs/go-ipfs-chunker"
11
cmdkit "github.com/ipfs/go-ipfs-cmdkit"
12
cmds "github.com/ipfs/go-ipfs-cmds"
16
- balanced "github.com/ipfs/go-unixfs/importer/balanced"
17
- ihelper "github.com/ipfs/go-unixfs/importer/helpers"
18
- trickle "github.com/ipfs/go-unixfs/importer/trickle"
19
- mh "github.com/multiformats/go-multihash"
13
+ files "github.com/ipfs/go-ipfs-files"
14
+ "github.com/ipfs/interface-go-ipfs-core/options"
15
)
16
17
var urlStoreCmd = &cmds.Command{
@@ -55,17 +50,17 @@ time.
50
Type: &BlockStat{},
51
52
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
58
- url := req.Arguments[0]
59
- n, err := cmdenv.GetNode(env)
60
- if err != nil {
61
- return err
53
+ urlString := req.Arguments[0]
54
+ if !filestore.IsURL(req.Arguments[0]) {
55
+ return fmt.Errorf("unsupported url syntax: %s", urlString)
56
}
57
64
- if !filestore.IsURL(url) {
65
- return fmt.Errorf("unsupported url syntax: %s", url)
58
+ url, err := url.Parse(urlString)
59
+ if err != nil {
60
+ return err
61
}
62
68
- cfg, err := n.Repo.Config()
63
+ cfg, err := cmdenv.GetConfig(env)
64
if err != nil {
65
return err
66
}
@@ -74,68 +69,40 @@ time.
69
return filestore.ErrUrlstoreNotEnabled
70
}
71
77
- useTrickledag, _ := req.Options[trickleOptionName].(bool)
78
- dopin, _ := req.Options[pinOptionName].(bool)
79
-
72
enc, err := cmdenv.GetCidEncoder(req)
73
if err != nil {
74
return err
75
}
76
85
- hreq, err := http.NewRequest("GET", url, nil)
86
- if err != nil {
87
- return err
88
- }
89
-
90
- hres, err := http.DefaultClient.Do(hreq)
77
+ api, err := cmdenv.GetApi(env, req)
78
if err != nil {
79
return err
80
}
94
- if hres.StatusCode != http.StatusOK {
95
- return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
96
- }
81
98
- if dopin {
99
- // Take the pinlock
100
- defer n.Blockstore.PinLock().Unlock()
101
- }
82
+ useTrickledag, _ := req.Options[trickleOptionName].(bool)
83
+ dopin, _ := req.Options[pinOptionName].(bool)
84
103
- chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize)
104
- prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256)
105
- dbp := &ihelper.DagBuilderParams{
106
- Dagserv: n.DAG,
107
- RawLeaves: true,
108
- Maxlinks: ihelper.DefaultLinksPerBlock,
109
- NoCopy: true,
110
- CidBuilder: &prefix,
111
- URL: url,
85
+ opts := []options.UnixfsAddOption{
86
+ options.Unixfs.Pin(dopin),
87
+ options.Unixfs.CidVersion(1),
88
+ options.Unixfs.RawLeaves(true),
89
+ options.Unixfs.Nocopy(true),
90
}
91
114
- layout := balanced.Layout
92
if useTrickledag {
116
- layout = trickle.Layout
93
+ opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
94
}
95
119
- db, err := dbp.New(chk)
120
- if err != nil {
121
- return err
122
- }
123
- root, err := layout(db)
96
+ file := files.NewWebFile(url)
97
+
98
+ path, err := api.Unixfs().Add(req.Context, file, opts...)
99
if err != nil {
100
return err
101
}
127
-
128
- c := root.Cid()
129
- if dopin {
130
- n.Pinning.PinWithMode(c, pin.Recursive)
131
- if err := n.Pinning.Flush(); err != nil {
132
- return err
133
- }
134
- }
135
-
102
+ size, _ := file.Size()
103
return cmds.EmitOnce(res, &BlockStat{
137
- Key: enc.Encode(c),
138
- Size: int(hres.ContentLength),
104
+ Key: enc.Encode(path.Cid()),
105
+ Size: int(size),
106
})
107
},
108
Encoders: cmds.EncoderMap{