Unverified Commit 3ffbdfd9 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #6259 from ipfs/feat/urlstore-coreapi

commands(feat): use the coreapi in the urlstore command
parents 6e5c2515 39513c6e
......@@ -3,20 +3,15 @@ package commands
import (
"fmt"
"io"
"net/http"
"net/url"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
filestore "github.com/ipfs/go-ipfs/filestore"
pin "github.com/ipfs/go-ipfs/pin"
cid "github.com/ipfs/go-cid"
chunk "github.com/ipfs/go-ipfs-chunker"
cmdkit "github.com/ipfs/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs-cmds"
balanced "github.com/ipfs/go-unixfs/importer/balanced"
ihelper "github.com/ipfs/go-unixfs/importer/helpers"
trickle "github.com/ipfs/go-unixfs/importer/trickle"
mh "github.com/multiformats/go-multihash"
files "github.com/ipfs/go-ipfs-files"
"github.com/ipfs/interface-go-ipfs-core/options"
)
var urlStoreCmd = &cmds.Command{
......@@ -32,6 +27,8 @@ var urlAdd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Add URL via urlstore.",
LongDescription: `
DEPRECATED: Use 'ipfs add --nocopy --cid-version=1 URL'.
Add URLs to ipfs without storing the data locally.
The URL provided must be stable and ideally on a web server under your
......@@ -39,10 +36,6 @@ control.
The file is added using raw-leaves but otherwise using the default
settings for 'ipfs add'.
This command is considered temporary until a better solution can be
found. It may disappear or the semantics can change at any
time.
`,
},
Options: []cmdkit.Option{
......@@ -55,87 +48,52 @@ time.
Type: &BlockStat{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
url := req.Arguments[0]
n, err := cmdenv.GetNode(env)
if err != nil {
return err
}
log.Error("The 'ipfs urlstore' command is deprecated, please use 'ipfs add --nocopy --cid-version=1")
if !filestore.IsURL(url) {
return fmt.Errorf("unsupported url syntax: %s", url)
urlString := req.Arguments[0]
if !filestore.IsURL(req.Arguments[0]) {
return fmt.Errorf("unsupported url syntax: %s", urlString)
}
cfg, err := n.Repo.Config()
url, err := url.Parse(urlString)
if err != nil {
return err
}
if !cfg.Experimental.UrlstoreEnabled {
return filestore.ErrUrlstoreNotEnabled
}
useTrickledag, _ := req.Options[trickleOptionName].(bool)
dopin, _ := req.Options[pinOptionName].(bool)
enc, err := cmdenv.GetCidEncoder(req)
if err != nil {
return err
}
hreq, err := http.NewRequest("GET", url, nil)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
hres, err := http.DefaultClient.Do(hreq)
if err != nil {
return err
}
if hres.StatusCode != http.StatusOK {
return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
}
if dopin {
// Take the pinlock
defer n.Blockstore.PinLock().Unlock()
}
useTrickledag, _ := req.Options[trickleOptionName].(bool)
dopin, _ := req.Options[pinOptionName].(bool)
chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize)
prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256)
dbp := &ihelper.DagBuilderParams{
Dagserv: n.DAG,
RawLeaves: true,
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: true,
CidBuilder: &prefix,
URL: url,
opts := []options.UnixfsAddOption{
options.Unixfs.Pin(dopin),
options.Unixfs.CidVersion(1),
options.Unixfs.RawLeaves(true),
options.Unixfs.Nocopy(true),
}
layout := balanced.Layout
if useTrickledag {
layout = trickle.Layout
opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
}
db, err := dbp.New(chk)
if err != nil {
return err
}
root, err := layout(db)
file := files.NewWebFile(url)
path, err := api.Unixfs().Add(req.Context, file, opts...)
if err != nil {
return err
}
c := root.Cid()
if dopin {
n.Pinning.PinWithMode(c, pin.Recursive)
if err := n.Pinning.Flush(); err != nil {
return err
}
}
size, _ := file.Size()
return cmds.EmitOnce(res, &BlockStat{
Key: enc.Encode(c),
Size: int(hres.ContentLength),
Key: enc.Encode(path.Cid()),
Size: int(size),
})
},
Encoders: cmds.EncoderMap{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment