Skip to content

Commit 3ffbdfd

Browse files
authored
Merge pull request #6259 from ipfs/feat/urlstore-coreapi
commands(feat): use the coreapi in the urlstore command
2 parents 6e5c251 + 39513c6 commit 3ffbdfd

File tree

1 file changed

+25
-67
lines changed

1 file changed

+25
-67
lines changed

core/commands/urlstore.go

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ package commands
33
import (
44
"fmt"
55
"io"
6-
"net/http"
6+
"net/url"
77

88
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
99
filestore "github.com/ipfs/go-ipfs/filestore"
10-
pin "github.com/ipfs/go-ipfs/pin"
1110

12-
cid "github.com/ipfs/go-cid"
13-
chunk "github.com/ipfs/go-ipfs-chunker"
1411
cmdkit "github.com/ipfs/go-ipfs-cmdkit"
1512
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"
2015
)
2116

2217
var urlStoreCmd = &cmds.Command{
@@ -32,17 +27,15 @@ var urlAdd = &cmds.Command{
3227
Helptext: cmdkit.HelpText{
3328
Tagline: "Add URL via urlstore.",
3429
LongDescription: `
30+
DEPRECATED: Use 'ipfs add --nocopy --cid-version=1 URL'.
31+
3532
Add URLs to ipfs without storing the data locally.
3633
3734
The URL provided must be stable and ideally on a web server under your
3835
control.
3936
4037
The file is added using raw-leaves but otherwise using the default
4138
settings for 'ipfs add'.
42-
43-
This command is considered temporary until a better solution can be
44-
found. It may disappear or the semantics can change at any
45-
time.
4639
`,
4740
},
4841
Options: []cmdkit.Option{
@@ -55,87 +48,52 @@ time.
5548
Type: &BlockStat{},
5649

5750
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
62-
}
51+
log.Error("The 'ipfs urlstore' command is deprecated, please use 'ipfs add --nocopy --cid-version=1")
6352

64-
if !filestore.IsURL(url) {
65-
return fmt.Errorf("unsupported url syntax: %s", url)
53+
urlString := req.Arguments[0]
54+
if !filestore.IsURL(req.Arguments[0]) {
55+
return fmt.Errorf("unsupported url syntax: %s", urlString)
6656
}
6757

68-
cfg, err := n.Repo.Config()
58+
url, err := url.Parse(urlString)
6959
if err != nil {
7060
return err
7161
}
7262

73-
if !cfg.Experimental.UrlstoreEnabled {
74-
return filestore.ErrUrlstoreNotEnabled
75-
}
76-
77-
useTrickledag, _ := req.Options[trickleOptionName].(bool)
78-
dopin, _ := req.Options[pinOptionName].(bool)
79-
8063
enc, err := cmdenv.GetCidEncoder(req)
8164
if err != nil {
8265
return err
8366
}
8467

85-
hreq, err := http.NewRequest("GET", url, nil)
68+
api, err := cmdenv.GetApi(env, req)
8669
if err != nil {
8770
return err
8871
}
8972

90-
hres, err := http.DefaultClient.Do(hreq)
91-
if err != nil {
92-
return err
93-
}
94-
if hres.StatusCode != http.StatusOK {
95-
return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
96-
}
97-
98-
if dopin {
99-
// Take the pinlock
100-
defer n.Blockstore.PinLock().Unlock()
101-
}
73+
useTrickledag, _ := req.Options[trickleOptionName].(bool)
74+
dopin, _ := req.Options[pinOptionName].(bool)
10275

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,
76+
opts := []options.UnixfsAddOption{
77+
options.Unixfs.Pin(dopin),
78+
options.Unixfs.CidVersion(1),
79+
options.Unixfs.RawLeaves(true),
80+
options.Unixfs.Nocopy(true),
11281
}
11382

114-
layout := balanced.Layout
11583
if useTrickledag {
116-
layout = trickle.Layout
84+
opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
11785
}
11886

119-
db, err := dbp.New(chk)
120-
if err != nil {
121-
return err
122-
}
123-
root, err := layout(db)
87+
file := files.NewWebFile(url)
88+
89+
path, err := api.Unixfs().Add(req.Context, file, opts...)
12490
if err != nil {
12591
return err
12692
}
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-
93+
size, _ := file.Size()
13694
return cmds.EmitOnce(res, &BlockStat{
137-
Key: enc.Encode(c),
138-
Size: int(hres.ContentLength),
95+
Key: enc.Encode(path.Cid()),
96+
Size: int(size),
13997
})
14098
},
14199
Encoders: cmds.EncoderMap{

0 commit comments

Comments
 (0)