block.go 2.71 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
	Description: "Manipulate raw IPFS blocks",
	Help: `'ipfs block' is a plumbing command used to manipulate raw ipfs blocks.
26 27
Reads from stdin or writes to stdout, and <key> is a base58 encoded
multihash.`,
28 29 30 31 32 33 34
	Subcommands: map[string]*cmds.Command{
		"get": blockGetCmd,
		"put": blockPutCmd,
	},
}

var blockGetCmd = &cmds.Command{
35
	Description: "Get a raw IPFS block",
36 37
	Help: `ipfs block get is a plumbing command for retreiving raw ipfs blocks.
It outputs to stdout, and <key> is a base58 encoded multihash.`,
38

39
	Arguments: []cmds.Argument{
40
		cmds.StringArg("key", true, false, "The base58 multihash of an existing block to get"),
41
	},
42
	Run: func(req cmds.Request) (interface{}, error) {
43 44 45 46
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
47 48 49

		key, ok := req.Arguments()[0].(string)
		if !ok {
50
			return nil, u.ErrCast()
51 52 53
		}

		if !u.IsValidHash(key) {
54
			return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
55 56 57 58
		}

		h, err := mh.FromB58String(key)
		if err != nil {
59
			return nil, err
60 61 62 63 64 65
		}

		k := u.Key(h)
		ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
		b, err := n.Blocks.GetBlock(ctx, k)
		if err != nil {
66
			return nil, err
67 68
		}

69
		return bytes.NewReader(b.Data), nil
70 71 72 73
	},
}

var blockPutCmd = &cmds.Command{
74
	Description: "Stores input as an IPFS block",
75 76
	Help: `ipfs block put is a plumbing command for storing raw ipfs blocks.
It reads from stdin, and <key> is a base58 encoded multihash.`,
77

78
	Arguments: []cmds.Argument{
79
		cmds.FileArg("data", true, false, "The data to be stored as an IPFS block"),
80
	},
81
	Run: func(req cmds.Request) (interface{}, error) {
82 83 84 85
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
86 87 88

		in, ok := req.Arguments()[0].(io.Reader)
		if !ok {
89
			return nil, u.ErrCast()
90 91 92 93
		}

		data, err := ioutil.ReadAll(in)
		if err != nil {
94
			return nil, err
95 96 97
		}

		b := blocks.NewBlock(data)
98
		log.Debugf("BlockPut key: '%q'", b.Key())
99 100 101

		k, err := n.Blocks.AddBlock(b)
		if err != nil {
102
			return nil, err
103 104
		}

105
		return &Block{
106 107
			Key:    k.String(),
			Length: len(data),
108
		}, nil
109 110 111 112 113 114 115 116 117 118
	},
	Type: &Block{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		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
		},
	},
}