block.go 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package commands

import (
	"bytes"
	"io"
	"io/ioutil"
	"time"

	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
	"github.com/jbenet/go-ipfs/blocks"
	cmds "github.com/jbenet/go-ipfs/commands"
	u "github.com/jbenet/go-ipfs/util"
)

type Block struct {
	Key    string
	Length int
}

var blockCmd = &cmds.Command{
23 24 25 26
	Helptext: cmds.HelpText{
		Tagline: "Manipulate raw IPFS blocks",
		ShortDescription: `
'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
27
Reads from stdin or writes to stdout, and <key> is a base58 encoded
28 29 30 31
multihash.
`,
	},

32 33 34 35 36 37 38
	Subcommands: map[string]*cmds.Command{
		"get": blockGetCmd,
		"put": blockPutCmd,
	},
}

var blockGetCmd = &cmds.Command{
39 40 41 42 43 44 45
	Helptext: cmds.HelpText{
		Tagline: "Get a raw IPFS block",
		ShortDescription: `
'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.
It outputs to stdout, and <key> is a base58 encoded multihash.
`,
	},
46

47
	Arguments: []cmds.Argument{
48
		cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get"),
49
	},
50
	Run: func(req cmds.Request) (interface{}, error) {
51 52 53 54
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
55 56 57

		key, ok := req.Arguments()[0].(string)
		if !ok {
58
			return nil, u.ErrCast()
59 60 61
		}

		if !u.IsValidHash(key) {
62
			return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
63 64 65 66
		}

		h, err := mh.FromB58String(key)
		if err != nil {
67
			return nil, err
68 69 70 71 72 73
		}

		k := u.Key(h)
		ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
		b, err := n.Blocks.GetBlock(ctx, k)
		if err != nil {
74
			return nil, err
75
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
76
		log.Debugf("BlockGet key: '%q'", b.Key())
77

78
		return bytes.NewReader(b.Data), nil
79 80 81 82
	},
}

var blockPutCmd = &cmds.Command{
83 84 85 86 87 88 89
	Helptext: cmds.HelpText{
		Tagline: "Stores input as an IPFS block",
		ShortDescription: `
ipfs block put is a plumbing command for storing raw ipfs blocks.
It reads from stdin, and <key> is a base58 encoded multihash.
`,
	},
90

91
	Arguments: []cmds.Argument{
92
		cmds.FileArg("data", true, false, "The data to be stored as an IPFS block").EnableStdin(),
93
	},
94
	Run: func(req cmds.Request) (interface{}, error) {
95 96 97 98
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
99 100 101

		in, ok := req.Arguments()[0].(io.Reader)
		if !ok {
102
			return nil, u.ErrCast()
103 104 105 106
		}

		data, err := ioutil.ReadAll(in)
		if err != nil {
107
			return nil, err
108 109 110
		}

		b := blocks.NewBlock(data)
111
		log.Debugf("BlockPut key: '%q'", b.Key())
112 113 114

		k, err := n.Blocks.AddBlock(b)
		if err != nil {
115
			return nil, err
116 117
		}

118
		return &Block{
119 120
			Key:    k.String(),
			Length: len(data),
121
		}, nil
122 123
	},
	Type: &Block{},
124
	Marshalers: cmds.MarshalerMap{
125 126
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			block := res.Output().(*Block)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127
			return []byte(block.Key + "\n"), nil
128 129 130
		},
	},
}