block.go 2.86 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 23
package commands

import (
	"bytes"
	"fmt"
	"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{
24 25 26 27
	Helptext: cmds.HelpText{
		Tagline: "Manipulate raw IPFS blocks",
		ShortDescription: `
'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
28
Reads from stdin or writes to stdout, and <key> is a base58 encoded
29 30 31 32
multihash.
`,
	},

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

var blockGetCmd = &cmds.Command{
40 41 42 43 44 45 46
	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.
`,
	},
47

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

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

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

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

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

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

var blockPutCmd = &cmds.Command{
84 85 86 87 88 89 90
	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.
`,
	},
91

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

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

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

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

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

119
		return &Block{
120 121
			Key:    k.String(),
			Length: len(data),
122
		}, nil
123 124
	},
	Type: &Block{},
125
	Marshalers: cmds.MarshalerMap{
126 127 128 129 130 131 132
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			block := res.Output().(*Block)
			s := fmt.Sprintf("Block added (%v bytes): %s\n", block.Length, block.Key)
			return []byte(s), nil
		},
	},
}