Skip to content

Commit

Permalink
fix: interop with 'block put' from go-ipfs 0.13 (#158)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lidel authored Apr 21, 2022
1 parent ebb2807 commit fd20901
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions client/httpapi/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"io"

"github.com/ipfs/go-cid"
"github.com/ipfs/interface-go-ipfs-core"
iface "github.com/ipfs/interface-go-ipfs-core"
caopts "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
mc "github.com/multiformats/go-multicodec"
mh "github.com/multiformats/go-multihash"
)

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

func (api *BlockAPI) Put(ctx context.Context, r io.Reader, opts ...caopts.BlockPutOption) (iface.BlockStat, error) {
options, _, err := caopts.BlockPutOptions(opts...)
options, err := caopts.BlockPutOptions(opts...)
px := options.CidPrefix
if err != nil {
return nil, err
}

mht, ok := mh.Codes[options.MhType]
mht, ok := mh.Codes[px.MhType]
if !ok {
return nil, fmt.Errorf("unknowm mhType %d", options.MhType)
return nil, fmt.Errorf("unknowm mhType %d", px.MhType)
}

var cidOptKey, cidOptVal string
switch {
case px.Version == 0 && px.Codec == cid.DagProtobuf:
// ensure legacy --format=v0 passes as BlockPutOption still works
cidOptKey = "format"
cidOptVal = "v0"
default:
// pass codec as string
cidOptKey = "cid-codec"
cidOptVal = mc.Code(px.Codec).String()
}

req := api.core().Request("block/put").
Option("mhtype", mht).
Option("mhlen", options.MhLength).
Option("format", options.Codec).
Option("mhlen", px.MhLength).
Option(cidOptKey, cidOptVal).
Option("pin", options.Pin).
FileBody(r)

Expand Down

0 comments on commit fd20901

Please sign in to comment.