block.go 2.83 KB
Newer Older
1 2 3 4
package commands

import (
	"bytes"
5
	"io"
6
	"io/ioutil"
7
	"strings"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	"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
}

23
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
		key := req.Arguments()[0]
58 59

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

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

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

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

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

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

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

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

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

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

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

121
		return &Block{
122 123
			Key:    k.String(),
			Length: len(data),
124
		}, nil
125
	},
126
	Type: Block{},
127
	Marshalers: cmds.MarshalerMap{
128
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
129
			block := res.Output().(*Block)
130
			return strings.NewReader(block.Key + "\n"), nil
131 132 133
		},
	},
}