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 ...@@ -3,20 +3,15 @@ package commands
import ( import (
"fmt" "fmt"
"io" "io"
"net/http" "net/url"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
filestore "github.com/ipfs/go-ipfs/filestore" 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" cmdkit "github.com/ipfs/go-ipfs-cmdkit"
cmds "github.com/ipfs/go-ipfs-cmds" cmds "github.com/ipfs/go-ipfs-cmds"
balanced "github.com/ipfs/go-unixfs/importer/balanced" files "github.com/ipfs/go-ipfs-files"
ihelper "github.com/ipfs/go-unixfs/importer/helpers" "github.com/ipfs/interface-go-ipfs-core/options"
trickle "github.com/ipfs/go-unixfs/importer/trickle"
mh "github.com/multiformats/go-multihash"
) )
var urlStoreCmd = &cmds.Command{ var urlStoreCmd = &cmds.Command{
...@@ -32,6 +27,8 @@ var urlAdd = &cmds.Command{ ...@@ -32,6 +27,8 @@ var urlAdd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
Tagline: "Add URL via urlstore.", Tagline: "Add URL via urlstore.",
LongDescription: ` LongDescription: `
DEPRECATED: Use 'ipfs add --nocopy --cid-version=1 URL'.
Add URLs to ipfs without storing the data locally. Add URLs to ipfs without storing the data locally.
The URL provided must be stable and ideally on a web server under your The URL provided must be stable and ideally on a web server under your
...@@ -39,10 +36,6 @@ control. ...@@ -39,10 +36,6 @@ control.
The file is added using raw-leaves but otherwise using the default The file is added using raw-leaves but otherwise using the default
settings for 'ipfs add'. 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{ Options: []cmdkit.Option{
...@@ -55,87 +48,52 @@ time. ...@@ -55,87 +48,52 @@ time.
Type: &BlockStat{}, Type: &BlockStat{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
url := req.Arguments[0] log.Error("The 'ipfs urlstore' command is deprecated, please use 'ipfs add --nocopy --cid-version=1")
n, err := cmdenv.GetNode(env)
if err != nil {
return err
}
if !filestore.IsURL(url) { urlString := req.Arguments[0]
return fmt.Errorf("unsupported url syntax: %s", url) 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 { if err != nil {
return err return err
} }
if !cfg.Experimental.UrlstoreEnabled {
return filestore.ErrUrlstoreNotEnabled
}
useTrickledag, _ := req.Options[trickleOptionName].(bool)
dopin, _ := req.Options[pinOptionName].(bool)
enc, err := cmdenv.GetCidEncoder(req) enc, err := cmdenv.GetCidEncoder(req)
if err != nil { if err != nil {
return err return err
} }
hreq, err := http.NewRequest("GET", url, nil) api, err := cmdenv.GetApi(env, req)
if err != nil { if err != nil {
return err return err
} }
hres, err := http.DefaultClient.Do(hreq) useTrickledag, _ := req.Options[trickleOptionName].(bool)
if err != nil { dopin, _ := req.Options[pinOptionName].(bool)
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()
}
chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize) opts := []options.UnixfsAddOption{
prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256) options.Unixfs.Pin(dopin),
dbp := &ihelper.DagBuilderParams{ options.Unixfs.CidVersion(1),
Dagserv: n.DAG, options.Unixfs.RawLeaves(true),
RawLeaves: true, options.Unixfs.Nocopy(true),
Maxlinks: ihelper.DefaultLinksPerBlock,
NoCopy: true,
CidBuilder: &prefix,
URL: url,
} }
layout := balanced.Layout
if useTrickledag { if useTrickledag {
layout = trickle.Layout opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
} }
db, err := dbp.New(chk) file := files.NewWebFile(url)
if err != nil {
return err path, err := api.Unixfs().Add(req.Context, file, opts...)
}
root, err := layout(db)
if err != nil { if err != nil {
return err return err
} }
size, _ := file.Size()
c := root.Cid()
if dopin {
n.Pinning.PinWithMode(c, pin.Recursive)
if err := n.Pinning.Flush(); err != nil {
return err
}
}
return cmds.EmitOnce(res, &BlockStat{ return cmds.EmitOnce(res, &BlockStat{
Key: enc.Encode(c), Key: enc.Encode(path.Cid()),
Size: int(hres.ContentLength), Size: int(size),
}) })
}, },
Encoders: cmds.EncoderMap{ 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