kubo/core/coredag/raw.go
Steven Allen 613508a0d3 add raw support to the dag put command.
`dag get` still doesn't properly support raw output but that's a bigger issue.

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
2017-10-10 19:58:23 -07:00

38 lines
864 B
Go

package coredag
import (
"io"
"io/ioutil"
"math"
"github.com/ipfs/go-ipfs/merkledag"
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
block "gx/ipfs/QmSn9Td7xgxm9EV7iEjTckpUWmWApggzPxu7eFGWkkpwin/go-block-format"
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash"
)
func rawRawParser(r io.Reader, mhType uint64, mhLen int) ([]node.Node, error) {
if mhType == math.MaxUint64 {
mhType = mh.SHA2_256
}
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
h, err := mh.Sum(data, mhType, mhLen)
if err != nil {
return nil, err
}
c := cid.NewCidV1(cid.Raw, h)
blk, err := block.NewBlockWithCid(data, c)
if err != nil {
return nil, err
}
nd := &merkledag.RawNode{Block: blk}
return []node.Node{nd}, nil
}