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

import (
	"fmt"
	"io"
6
	"net/url"
Jakub Sztandera's avatar
Jakub Sztandera committed
7

8
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
	filestore "github.com/ipfs/go-ipfs/filestore"
Jakub Sztandera's avatar
Jakub Sztandera committed
10

Jakub Sztandera's avatar
Jakub Sztandera committed
11 12
	cmdkit "github.com/ipfs/go-ipfs-cmdkit"
	cmds "github.com/ipfs/go-ipfs-cmds"
13 14
	files "github.com/ipfs/go-ipfs-files"
	"github.com/ipfs/interface-go-ipfs-core/options"
Jakub Sztandera's avatar
Jakub Sztandera committed
15 16
)

17
var urlStoreCmd = &cmds.Command{
18 19 20
	Helptext: cmdkit.HelpText{
		Tagline: "Interact with urlstore.",
	},
Jakub Sztandera's avatar
Jakub Sztandera committed
21 22 23 24 25 26
	Subcommands: map[string]*cmds.Command{
		"add": urlAdd,
	},
}

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

keks's avatar
keks committed
52
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
53 54 55
		urlString := req.Arguments[0]
		if !filestore.IsURL(req.Arguments[0]) {
			return fmt.Errorf("unsupported url syntax: %s", urlString)
Jakub Sztandera's avatar
Jakub Sztandera committed
56 57
		}

58 59 60
		url, err := url.Parse(urlString)
		if err != nil {
			return err
61 62
		}

63
		cfg, err := cmdenv.GetConfig(env)
64
		if err != nil {
keks's avatar
keks committed
65
			return err
66 67 68
		}

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

72 73 74 75 76
		enc, err := cmdenv.GetCidEncoder(req)
		if err != nil {
			return err
		}

77
		api, err := cmdenv.GetApi(env, req)
Jakub Sztandera's avatar
Jakub Sztandera committed
78
		if err != nil {
keks's avatar
keks committed
79
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
80 81
		}

82 83
		useTrickledag, _ := req.Options[trickleOptionName].(bool)
		dopin, _ := req.Options[pinOptionName].(bool)
84

85 86 87 88 89
		opts := []options.UnixfsAddOption{
			options.Unixfs.Pin(dopin),
			options.Unixfs.CidVersion(1),
			options.Unixfs.RawLeaves(true),
			options.Unixfs.Nocopy(true),
Jakub Sztandera's avatar
Jakub Sztandera committed
90 91
		}

92
		if useTrickledag {
93
			opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
94
		}
95

96 97 98
		file := files.NewWebFile(url)

		path, err := api.Unixfs().Add(req.Context, file, opts...)
Jakub Sztandera's avatar
Jakub Sztandera committed
99
		if err != nil {
keks's avatar
keks committed
100
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
101
		}
102
		size, _ := file.Size()
keks's avatar
keks committed
103
		return cmds.EmitOnce(res, &BlockStat{
104 105
			Key:  enc.Encode(path.Cid()),
			Size: int(size),
Jakub Sztandera's avatar
Jakub Sztandera committed
106 107
		})
	},
108 109 110 111 112
	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
113 114
	},
}