Skip to content

Commit

Permalink
Merge pull request #6259 from ipfs/feat/urlstore-coreapi
Browse files Browse the repository at this point in the history
commands(feat): use the coreapi in the urlstore command
  • Loading branch information
Stebalien authored Apr 26, 2019
2 parents 6e5c251 + 39513c6 commit 3ffbdfd
Showing 1 changed file with 25 additions and 67 deletions.
92 changes: 25 additions & 67 deletions core/commands/urlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -32,17 +27,15 @@ 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
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{
Expand All @@ -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{
Expand Down

0 comments on commit 3ffbdfd

Please sign in to comment.