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
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
}

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 56

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

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

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

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

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

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

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

99 100 101 102 103 104 105 106
		file, err := req.Files().NextFile()
		if err != nil {
			return nil, err
		}

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

109
		err = file.Close()
110
		if err != nil {
111
			return nil, err
112 113 114
		}

		b := blocks.NewBlock(data)
115
		log.Debugf("BlockPut key: '%q'", b.Key())
116 117 118

		k, err := n.Blocks.AddBlock(b)
		if err != nil {
119
			return nil, err
120 121
		}

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