add.go 2.35 KB
Newer Older
1 2 3
package commands

import (
4
	"errors"
5 6 7 8
	"fmt"
	"io"

	cmds "github.com/jbenet/go-ipfs/commands"
9 10
	core "github.com/jbenet/go-ipfs/core"
	importer "github.com/jbenet/go-ipfs/importer"
11 12 13 14 15 16
	dag "github.com/jbenet/go-ipfs/merkledag"
)

// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

17 18 19 20
type AddOutput struct {
	Added []Object
}

21 22 23 24 25 26 27 28 29 30 31 32
var addCmd = &cmds.Command{
	Options: []cmds.Option{
		cmds.Option{[]string{"recursive", "r"}, cmds.Bool},
	},
	Arguments: []cmds.Argument{
		cmds.Argument{"file", cmds.ArgFile, false, true},
	},
	Help: "TODO",
	Run: func(res cmds.Response, req cmds.Request) {
		n := req.Context().Node

		// if recursive, set depth to reflect so
Brian Tiger Chow's avatar
fmt  
Brian Tiger Chow committed
33 34 35
		// opt, found := req.Option("r")
		// if r, _ := opt.(bool); found && r {
		// }
36

37 38 39 40 41
		readers := make([]io.Reader, 0)
		for _, arg := range req.Arguments() {
			reader, ok := arg.(io.Reader)
			if !ok {
				res.SetError(errors.New("cast error"), cmds.ErrNormal)
42 43
				return
			}
44 45
			readers = append(readers, reader)
		}
46

47 48 49 50 51 52 53 54 55 56
		dagnodes, err := add(n, readers)
		if err != nil {
			res.SetError(errors.New("cast error"), cmds.ErrNormal)
			return
		}

		added := make([]Object, len(req.Arguments()))
		for _, dagnode := range dagnodes {

			k, err := dagnode.Key()
57 58 59
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
60
			}
61
			added = append(added, Object{Hash: k.String(), Links: nil})
62 63
		}

64
		res.SetOutput(&AddOutput{added})
65
	},
66 67 68 69 70 71 72
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*AddOutput).Added
			if len(v) == 1 {
				s := fmt.Sprintf("Added object: %s\n", v[0].Hash)
				return []byte(s), nil
			}
73

74 75 76 77 78 79
			s := fmt.Sprintf("Added %v objects:\n", len(v))
			for _, obj := range v {
				s += fmt.Sprintf("- %s\n", obj.Hash)
			}
			return []byte(s), nil
		},
80
	},
81
	Type: &AddOutput{},
82 83
}

84
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
85

86
	dagnodes := make([]*dag.Node, 0)
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	for _, reader := range readers {
		node, err := importer.NewDagFromReader(reader)
		if err != nil {
			return nil, err
		}

		err = n.DAG.AddRecursive(node) // add the file to the graph + local storage
		if err != nil {
			return nil, err
		}

		err = n.Pinning.Pin(node, true) // ensure we keep it
		if err != nil {
			return nil, err
		}

		dagnodes = append(dagnodes, node)
	}
	return dagnodes, nil
107
}