urlstore.go 3.67 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 13
	chunk "gx/ipfs/QmR4QQVkBZsZENRjYFVi8dEtPL3daZRNKk24m4r6WKJHNm/go-ipfs-chunker"
	cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
14 15 16
	balanced "gx/ipfs/QmSMJ4rZbCJaih3y82Ebq7BZqK6vU2FHsKcWKQiE1DPTpS/go-unixfs/importer/balanced"
	ihelper "gx/ipfs/QmSMJ4rZbCJaih3y82Ebq7BZqK6vU2FHsKcWKQiE1DPTpS/go-unixfs/importer/helpers"
	trickle "gx/ipfs/QmSMJ4rZbCJaih3y82Ebq7BZqK6vU2FHsKcWKQiE1DPTpS/go-unixfs/importer/trickle"
Hector Sanjuan's avatar
Hector Sanjuan committed
17
	cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
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{
23 24 25
	Helptext: cmdkit.HelpText{
		Tagline: "Interact with urlstore.",
	},
Jakub Sztandera's avatar
Jakub Sztandera committed
26 27 28 29 30 31
	Subcommands: map[string]*cmds.Command{
		"add": urlAdd,
	},
}

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

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

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

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

		if !cfg.Experimental.UrlstoreEnabled {
keks's avatar
keks committed
74
			return filestore.ErrUrlstoreNotEnabled
75 76
		}

77
		useTrickledag, _ := req.Options[trickleOptionName].(bool)
78
		dopin, _ := req.Options[pinOptionName].(bool)
79

80 81 82 83 84
		enc, err := cmdenv.GetCidEncoder(req)
		if err != nil {
			return err
		}

Jakub Sztandera's avatar
Jakub Sztandera committed
85 86
		hreq, err := http.NewRequest("GET", url, nil)
		if err != nil {
keks's avatar
keks committed
87
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
88 89 90 91
		}

		hres, err := http.DefaultClient.Do(hreq)
		if err != nil {
keks's avatar
keks committed
92
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
93 94
		}
		if hres.StatusCode != http.StatusOK {
keks's avatar
keks committed
95
			return fmt.Errorf("expected code 200, got: %d", hres.StatusCode)
Jakub Sztandera's avatar
Jakub Sztandera committed
96 97
		}

98 99 100 101 102
		if dopin {
			// Take the pinlock
			defer n.Blockstore.PinLock().Unlock()
		}

Jakub Sztandera's avatar
Jakub Sztandera committed
103 104 105
		chk := chunk.NewSizeSplitter(hres.Body, chunk.DefaultBlockSize)
		prefix := cid.NewPrefixV1(cid.DagProtobuf, mh.SHA2_256)
		dbp := &ihelper.DagBuilderParams{
106 107 108 109 110 111
			Dagserv:    n.DAG,
			RawLeaves:  true,
			Maxlinks:   ihelper.DefaultLinksPerBlock,
			NoCopy:     true,
			CidBuilder: &prefix,
			URL:        url,
Jakub Sztandera's avatar
Jakub Sztandera committed
112 113
		}

114 115 116 117
		layout := balanced.Layout
		if useTrickledag {
			layout = trickle.Layout
		}
118

119 120 121 122 123
		db, err := dbp.New(chk)
		if err != nil {
			return err
		}
		root, err := layout(db)
Jakub Sztandera's avatar
Jakub Sztandera committed
124
		if err != nil {
keks's avatar
keks committed
125
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
126 127
		}

128 129 130 131 132 133 134 135
		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
136
		return cmds.EmitOnce(res, &BlockStat{
137
			Key:  enc.Encode(c),
Jakub Sztandera's avatar
Jakub Sztandera committed
138 139 140
			Size: int(hres.ContentLength),
		})
	},
141 142 143 144 145
	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
146 147
	},
}