Skip to content

Commit fd20901

Browse files
authored
fix: interop with 'block put' from go-ipfs 0.13 (#158)
* chore: interop with go-ipfs 0.13 Applies necessary changes to ensure 'block/put' works and is backward-compatible. Context: #8568 * chore: 0.3.1 bumping as patch because we bumped to 0.3.0 recently, as part of other (unreleased) go-ipfs 0.13 work This commit was moved from ipfs/go-ipfs-http-client@ecf364c
1 parent ebb2807 commit fd20901

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

client/httpapi/block.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"io"
88

99
"github.com/ipfs/go-cid"
10-
"github.com/ipfs/interface-go-ipfs-core"
10+
iface "github.com/ipfs/interface-go-ipfs-core"
1111
caopts "github.com/ipfs/interface-go-ipfs-core/options"
1212
"github.com/ipfs/interface-go-ipfs-core/path"
13+
mc "github.com/multiformats/go-multicodec"
1314
mh "github.com/multiformats/go-multihash"
1415
)
1516

@@ -31,20 +32,33 @@ func (s *blockStat) Path() path.Resolved {
3132
}
3233

3334
func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {
34-
options, _, err := caopts.BlockPutOptions(opts...)
35+
options, err := caopts.BlockPutOptions(opts...)
36+
px := options.CidPrefix
3537
if err != nil {
3638
return nil, err
3739
}
3840

39-
mht, ok := mh.Codes[options.MhType]
41+
mht, ok := mh.Codes[px.MhType]
4042
if !ok {
41-
return nil, fmt.Errorf("unknowm mhType %d", options.MhType)
43+
return nil, fmt.Errorf("unknowm mhType %d", px.MhType)
44+
}
45+
46+
var cidOptKey, cidOptVal string
47+
switch {
48+
case px.Version == 0 && px.Codec == cid.DagProtobuf:
49+
// ensure legacy --format=v0 passes as BlockPutOption still works
50+
cidOptKey = "format"
51+
cidOptVal = "v0"
52+
default:
53+
// pass codec as string
54+
cidOptKey = "cid-codec"
55+
cidOptVal = mc.Code(px.Codec).String()
4256
}
4357

4458
req := api.core().Request("block/put").
4559
Option("mhtype", mht).
46-
Option("mhlen", options.MhLength).
47-
Option("format", options.Codec).
60+
Option("mhlen", px.MhLength).
61+
Option(cidOptKey, cidOptVal).
4862
Option("pin", options.Pin).
4963
FileBody(r)
5064

0 commit comments

Comments
 (0)