urlstore.go 3.47 KB
Newer Older
Jakub Sztandera's avatar
Jakub Sztandera committed
1 2 3 4 5 6 7
package commands

import (
	"fmt"
	"io"
	"net/http"

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"
Jakub Sztandera's avatar
Jakub Sztandera committed
11

Steven Allen's avatar
Steven Allen committed
12
	cmds "gx/ipfs/QmPdvMtgpnMuU68mWhGtzCxnddXJoV96tT9aPcNbQsqPaM/go-ipfs-cmds"
Steven Allen's avatar
Steven Allen committed
13 14
	chunk "gx/ipfs/QmR4QQVkBZsZENRjYFVi8dEtPL3daZRNKk24m4r6WKJHNm/go-ipfs-chunker"
	cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
Steven Allen's avatar
Steven Allen committed
15 16 17
	balanced "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/importer/balanced"
	ihelper "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/importer/helpers"
	trickle "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/importer/trickle"
18
	cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
Steven Allen's avatar
Steven Allen committed
19
	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
Jakub Sztandera's avatar
Jakub Sztandera committed
20 21
)

22
var urlStoreCmd = &cmds.Command{
Jakub Sztandera's avatar
Jakub Sztandera committed
23 24 25 26 27 28
	Subcommands: map[string]*cmds.Command{
		"add": urlAdd,
	},
}

var urlAdd = &cmds.Command{
29
	Helptext: cmdkit.HelpText{
30
		Tagline: "Add URL via urlstore.",
31 32 33 34 35 36 37 38 39 40 41 42 43 44
		LongDescription: `
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.
`,
	},
45 46
	Options: []cmdkit.Option{
		cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
47
		cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
48
	},
Jakub Sztandera's avatar
Jakub Sztandera committed
49 50 51
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("url", true, false, "URL to add to IPFS"),
	},
keks's avatar
keks committed
52
	Type: &BlockStat{},
Jakub Sztandera's avatar
Jakub Sztandera committed
53

keks's avatar
keks committed
54
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
55
		url := req.Arguments[0]
56
		n, err := cmdenv.GetNode(env)
Jakub Sztandera's avatar
Jakub Sztandera committed
57
		if err != nil {
keks's avatar
keks committed
58
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
59 60
		}

61
		if !filestore.IsURL(url) {
keks's avatar
keks committed
62
			return fmt.Errorf("unsupported url syntax: %s", url)
63 64
		}

65 66
		cfg, err := n.Repo.Config()
		if err != nil {
keks's avatar
keks committed
67
			return err
68 69 70
		}

		if !cfg.Experimental.UrlstoreEnabled {
keks's avatar
keks committed
71
			return filestore.ErrUrlstoreNotEnabled
72 73
		}

74
		useTrickledag, _ := req.Options[trickleOptionName].(bool)
75
		dopin, _ := req.Options[pinOptionName].(bool)
76

Jakub Sztandera's avatar
Jakub Sztandera committed
77 78
		hreq, err := http.NewRequest("GET", url, nil)
		if err != nil {
keks's avatar
keks committed
79
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
80 81 82 83
		}

		hres, err := http.DefaultClient.Do(hreq)
		if err != nil {
keks's avatar
keks committed
84
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
85 86
		}
		if hres.StatusCode != http.StatusOK {
keks's avatar
keks committed
87
			return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
Jakub Sztandera's avatar
Jakub Sztandera committed
88 89
		}

90 91 92 93 94
		if dopin {
			// Take the pinlock
			defer n.Blockstore.PinLock().Unlock()
		}

Jakub Sztandera's avatar
Jakub Sztandera committed
95 96 97
		chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize)
		prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256)
		dbp := &ihelper.DagBuilderParams{
98 99 100 101 102 103
			Dagserv:    n.DAG,
			RawLeaves:  true,
			Maxlinks:   ihelper.DefaultLinksPerBlock,
			NoCopy:     true,
			CidBuilder: &prefix,
			URL:        url,
Jakub Sztandera's avatar
Jakub Sztandera committed
104 105
		}

106 107 108 109
		layout := balanced.Layout
		if useTrickledag {
			layout = trickle.Layout
		}
110

111
		root, err := layout(dbp.New(chk))
Jakub Sztandera's avatar
Jakub Sztandera committed
112
		if err != nil {
keks's avatar
keks committed
113
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
114 115
		}

116 117 118 119 120 121 122 123
		c := root.Cid()
		if dopin {
			n.Pinning.PinWithMode(c, pin.Recursive)
			if err := n.Pinning.Flush(); err != nil {
				return err
			}
		}

keks's avatar
keks committed
124
		return cmds.EmitOnce(res, &BlockStat{
125
			Key:  c.String(),
Jakub Sztandera's avatar
Jakub Sztandera committed
126 127 128
			Size: int(hres.ContentLength),
		})
	},
129 130 131 132 133
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
			_, err := fmt.Fprintln(w, bs.Key)
			return err
		}),
Jakub Sztandera's avatar
Jakub Sztandera committed
134 135
	},
}