block.go 2.7 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 24
package commands

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

var blockGetCmd = &cmds.Command{
35 36 37
	Description: "Get a raw IPFS block",
	Help:        `'ipfs block get' is a plumbing command for retreiving raw ipfs blocks.`,

38
	Arguments: []cmds.Argument{
39
		cmds.Argument{"key", cmds.ArgString, true, false, "The base58 multihash of an existing block to get"},
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	},
	Run: func(res cmds.Response, req cmds.Request) {
		n := req.Context().Node

		key, ok := req.Arguments()[0].(string)
		if !ok {
			res.SetError(errors.New("cast error"), cmds.ErrNormal)
			return
		}

		if !u.IsValidHash(key) {
			res.SetError(errors.New("Not a valid hash"), cmds.ErrClient)
			return
		}

		h, err := mh.FromB58String(key)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

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

		res.SetOutput(bytes.NewReader(b.Data))
	},
}

var blockPutCmd = &cmds.Command{
74 75 76 77
	Description: "Stores input as an IPFS block",
	Help: `'ipfs block put' is a plumbing command for storing raw ipfs blocks.
It outputs the key of the stored block.`,

78
	Arguments: []cmds.Argument{
79
		cmds.Argument{"data", cmds.ArgFile, true, false, "The data to be stored as an IPFS block"},
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	},
	Run: func(res cmds.Response, req cmds.Request) {
		n := req.Context().Node

		in, ok := req.Arguments()[0].(io.Reader)
		if !ok {
			res.SetError(errors.New("cast error"), cmds.ErrNormal)
			return
		}

		data, err := ioutil.ReadAll(in)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		b := blocks.NewBlock(data)

		k, err := n.Blocks.AddBlock(b)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		res.SetOutput(&Block{
			Key:    k.String(),
			Length: len(data),
		})
	},
	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
		},
	},
}