block.go 1.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
package commands

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"time"

10
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
	"github.com/jbenet/go-ipfs/blocks"
	"github.com/jbenet/go-ipfs/core"
	u "github.com/jbenet/go-ipfs/util"
)

// BlockGet retrives a raw ipfs block from the node's BlockService
func BlockGet(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {

	if !u.IsValidHash(args[0]) {
		return fmt.Errorf("block get: not a valid hash")
	}

	h, err := mh.FromB58String(args[0])
	if err != nil {
		return fmt.Errorf("block get: %v", err)
	}

	k := u.Key(h)
31
	log.Debugf("BlockGet key: '%q'", k)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
	b, err := n.Blocks.GetBlock(ctx, k)
	if err != nil {
		return fmt.Errorf("block get: %v", err)
	}

	_, err = out.Write(b.Data)
	return err
}

// BlockPut reads everything from conn and saves the data to the nodes BlockService
func BlockPut(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	// TODO: this should read from an io.Reader arg
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	b := blocks.NewBlock(data)
51
	log.Debugf("BlockPut key: '%q'", b.Key())
52 53 54 55 56 57 58 59 60

	k, err := n.Blocks.AddBlock(b)
	if err != nil {
		return err
	}
	fmt.Fprintf(out, "added as '%s'\n", k)

	return nil
}