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
package commands

import (
	"bytes"
	"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
}

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

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

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

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

55
		key := req.Arguments()[0]
56 57

		if !u.IsValidHash(key) {
58
			return nil, cmds.Error{"Not a valid hash", cmds.ErrClient}
59 60 61 62
		}

		h, err := mh.FromB58String(key)
		if err != nil {
63
			return nil, err
64 65 66 67 68 69
		}

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

74
		return bytes.NewReader(b.Data), nil
75 76 77 78
	},
}

var blockPutCmd = &cmds.Command{
79 80 81 82 83 84 85
	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.
`,
	},
86

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

96 97 98 99 100 101 102 103
		file, err := req.Files().NextFile()
		if err != nil {
			return nil, err
		}

		data, err := ioutil.ReadAll(file)
		if err != nil {
			return nil, err
104 105
		}

106
		err = file.Close()
107
		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
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			block := res.Output().(*Block)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
128
			return []byte(block.Key + "\n"), nil
129 130 131
		},
	},
}