urlstore.go 2.48 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
	filestore "github.com/ipfs/go-filestore"
9
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jakub Sztandera's avatar
Jakub Sztandera committed
10

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

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

var urlAdd = &cmds.Command{
Steven Allen's avatar
Steven Allen committed
26
	Helptext: cmds.HelpText{
27
		Tagline: "Add URL via urlstore.",
28
		LongDescription: `
29
DEPRECATED: Use 'ipfs add --nocopy --cid-version=1 URL'.
30

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

keks's avatar
keks committed
49
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
50 51
		log.Error("The 'ipfs urlstore' command is deprecated, please use 'ipfs add --nocopy --cid-version=1")

52 53 54
		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
55 56
		}

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

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

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

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

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

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

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

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