@cryptotaxi247 / kubo / commits / f68e1843f

allow cid format selection in block put command

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Oct 18, 2016 at 17:05 UTC f68e1843f4a50bafd88b73e018cc3d51a6be20b2
2 files changed +57 -6
core/commands/block.go
+42 -6
@@ -2,7 +2,6 @@ package commands
2
3 import (
4 "bytes"
5 - "errors"
5 "fmt"
6 "io"
7 "io/ioutil"
@@ -11,7 +10,9 @@ import (
10 "github.com/ipfs/go-ipfs/blocks"
11 util "github.com/ipfs/go-ipfs/blocks/blockstore/util"
12 cmds "github.com/ipfs/go-ipfs/commands"
13 +
14 cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid"
15 + mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
16 u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
17 )
18
@@ -114,6 +115,9 @@ It reads from stdin, and <key> is a base58 encoded multihash.
115 Arguments: []cmds.Argument{
116 cmds.FileArg("data", true, false, "The data to be stored as an IPFS block.").EnableStdin(),
117 },
118 + Options: []cmds.Option{
119 + cmds.StringOption("format", "f", "cid format for blocks to be created with.").Default("v0"),
120 + },
121 Run: func(req cmds.Request, res cmds.Response) {
122 n, err := req.InvocContext().GetNode()
123 if err != nil {
@@ -139,7 +143,39 @@ It reads from stdin, and <key> is a base58 encoded multihash.
143 return
144 }
145
142 - b := blocks.NewBlock(data)
146 + format, _, _ := req.Option("format").String()
147 + var pref cid.Prefix
148 + pref.MhType = mh.SHA2_256
149 + pref.MhLength = -1
150 + pref.Version = 1
151 + switch format {
152 + case "cbor":
153 + pref.Codec = cid.CBOR
154 + case "json":
155 + pref.Codec = cid.JSON
156 + case "protobuf":
157 + pref.Codec = cid.Protobuf
158 + case "raw":
159 + pref.Codec = cid.Raw
160 + case "v0":
161 + pref.Version = 0
162 + pref.Codec = cid.Protobuf
163 + default:
164 + res.SetError(fmt.Errorf("unrecognized format: %s", format), cmds.ErrNormal)
165 + return
166 + }
167 +
168 + bcid, err := pref.Sum(data)
169 + if err != nil {
170 + res.SetError(err, cmds.ErrNormal)
171 + return
172 + }
173 +
174 + b, err := blocks.NewBlockWithCid(data, bcid)
175 + if err != nil {
176 + res.SetError(err, cmds.ErrNormal)
177 + return
178 + }
179 log.Debugf("BlockPut key: '%q'", b.Cid())
180
181 k, err := n.Blocks.AddBlock(b)
@@ -163,15 +199,15 @@ It reads from stdin, and <key> is a base58 encoded multihash.
199 }
200
201 func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
202 + if len(skey) == 0 {
203 + return nil, fmt.Errorf("zero length cid invalid")
204 + }
205 +
206 n, err := req.InvocContext().GetNode()
207 if err != nil {
208 return nil, err
209 }
210
171 - if !u.IsValidHash(skey) {
172 - return nil, errors.New("Not a valid hash")
173 - }
174 -
211 c, err := cid.Decode(skey)
212 if err != nil {
213 return nil, err
test/sharness/t0050-block.sh
+15
@@ -169,6 +169,21 @@ test_expect_success "multi-block 'ipfs block rm -q' produces no output" '
169 test ! -s block_rm_out
170 '
171
172 +test_expect_success "can set cid format on block put" '
173 + HASH=$(ipfs block put --format=protobuf ../t0051-object-data/testPut.pb)
174 +'
175 +
176 +test_expect_success "created an object correctly!" '
177 + ipfs object get $HASH > obj_out &&
178 + echo "{\"Links\":[],\"Data\":\"test json for sharness test\"}" > obj_exp &&
179 + test_cmp obj_out obj_exp
180 +'
181 +
182 +test_expect_success "block get output looks right" '
183 + ipfs block get $HASH > pb_block_out &&
184 + test_cmp pb_block_out ../t0051-object-data/testPut.pb
185 +'
186 +
187 #
188 # Misc tests
189 #