urlstore.go 2.42 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
		LongDescription: `
30 31
DEPRECATED: Use 'ipfs add --nocopy URL'.

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

keks's avatar
keks committed
50
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
51 52 53
		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
54 55
		}

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

61 62 63 64 65
		enc, err := cmdenv.GetCidEncoder(req)
		if err != nil {
			return err
		}

66
		api, err := cmdenv.GetApi(env, req)
Jakub Sztandera's avatar
Jakub Sztandera committed
67
		if err != nil {
keks's avatar
keks committed
68
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
69 70
		}

71 72
		useTrickledag, _ := req.Options[trickleOptionName].(bool)
		dopin, _ := req.Options[pinOptionName].(bool)
73

74 75 76 77 78
		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
79 80
		}

81
		if useTrickledag {
82
			opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
83
		}
84

85 86 87
		file := files.NewWebFile(url)

		path, err := api.Unixfs().Add(req.Context, file, opts...)
Jakub Sztandera's avatar
Jakub Sztandera committed
88
		if err != nil {
keks's avatar
keks committed
89
			return err
Jakub Sztandera's avatar
Jakub Sztandera committed
90
		}
91
		size, _ := file.Size()
keks's avatar
keks committed
92
		return cmds.EmitOnce(res, &BlockStat{
93 94
			Key:  enc.Encode(path.Cid()),
			Size: int(size),
Jakub Sztandera's avatar
Jakub Sztandera committed
95 96
		})
	},
97 98 99 100 101
	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
102 103
	},
}