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: https://github.com/ipfs/go-ipfs/pull/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@ecf364c9898d78034e3232574aae70d1bf85357c
Marcin Rataj committed
Apr 21, 2022 at 22:18 UTC
fd209019f27c7a16f94bfc41ebc7b815daee9903
1 file changed
+20
-6
client/httpapi/block.go
+20
-6
@@ -7,9 +7,10 @@ import (
7
"io"
8
9
"github.com/ipfs/go-cid"
10
- "github.com/ipfs/interface-go-ipfs-core"
10
+ iface "github.com/ipfs/interface-go-ipfs-core"
11
caopts "github.com/ipfs/interface-go-ipfs-core/options"
12
"github.com/ipfs/interface-go-ipfs-core/path"
13
+ mc "github.com/multiformats/go-multicodec"
14
mh "github.com/multiformats/go-multihash"
15
)
16
@@ -31,20 +32,33 @@ func (s *blockStat) Path() path.Resolved {
32
}
33
34
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
37
if err != nil {
38
return nil, err
39
}
40
39
- mht, ok := mh.Codes[options.MhType]
41
+ mht, ok := mh.Codes[px.MhType]
42
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()
56
}
57
58
req := api.core().Request("block/put").
59
Option("mhtype", mht).
46
- Option("mhlen", options.MhLength).
47
- Option("format", options.Codec).
60
+ Option("mhlen", px.MhLength).
61
+ Option(cidOptKey, cidOptVal).
62
Option("pin", options.Pin).
63
FileBody(r)
64