Commit 1f98f4b9 authored by Łukasz Magiera's avatar Łukasz Magiera Committed by Steven Allen

coreapi: block: move option logic to options package

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 9b925e0e
...@@ -4,7 +4,6 @@ import ( ...@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
...@@ -12,7 +11,6 @@ import ( ...@@ -12,7 +11,6 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format" blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format"
cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid" cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
) )
...@@ -25,7 +23,7 @@ type BlockStat struct { ...@@ -25,7 +23,7 @@ type BlockStat struct {
} }
func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) { func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.BlockPutOption) (coreiface.BlockStat, error) {
settings, err := caopts.BlockPutOptions(opts...) _, pref, err := caopts.BlockPutOptions(opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -35,37 +33,6 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc ...@@ -35,37 +33,6 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err return nil, err
} }
var pref cid.Prefix
pref.Version = 1
if settings.Codec == "" {
if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
settings.Codec = "protobuf"
} else {
settings.Codec = "v0"
}
}
formatval, ok := cid.Codecs[settings.Codec]
if !ok {
return nil, fmt.Errorf("unrecognized format: %s", settings.Codec)
}
if settings.Codec == "v0" && settings.MhType == mh.SHA2_256 {
pref.Version = 0
}
if settings.Codec == "v0" {
if settings.MhType != mh.SHA2_256 || (settings.MhLength != -1 && settings.MhLength != 32) {
return nil, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
}
}
pref.Codec = formatval
pref.MhType = settings.MhType
pref.MhLength = settings.MhLength
bcid, err := pref.Sum(data) bcid, err := pref.Sum(data)
if err != nil { if err != nil {
return nil, err return nil, err
......
package options package options
import ( import (
"gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" "fmt"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
cid "gx/ipfs/QmZFbDTY9jfSBms2MchvYM9oYRbAF19K7Pby47yDBfpPrb/go-cid"
) )
type BlockPutSettings struct { type BlockPutSettings struct {
...@@ -17,20 +19,52 @@ type BlockRmSettings struct { ...@@ -17,20 +19,52 @@ type BlockRmSettings struct {
type BlockPutOption func(*BlockPutSettings) error type BlockPutOption func(*BlockPutSettings) error
type BlockRmOption func(*BlockRmSettings) error type BlockRmOption func(*BlockRmSettings) error
func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, error) { func BlockPutOptions(opts ...BlockPutOption) (*BlockPutSettings, cid.Prefix, error) {
options := &BlockPutSettings{ options := &BlockPutSettings{
Codec: "", Codec: "",
MhType: multihash.SHA2_256, MhType: mh.SHA2_256,
MhLength: -1, MhLength: -1,
} }
for _, opt := range opts { for _, opt := range opts {
err := opt(options) err := opt(options)
if err != nil { if err != nil {
return nil, err return nil, cid.Prefix{}, err
} }
} }
return options, nil
var pref cid.Prefix
pref.Version = 1
if options.Codec == "" {
if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
options.Codec = "protobuf"
} else {
options.Codec = "v0"
}
}
if options.Codec == "v0" && options.MhType == mh.SHA2_256 {
pref.Version = 0
}
formatval, ok := cid.Codecs[options.Codec]
if !ok {
return nil, cid.Prefix{}, fmt.Errorf("unrecognized format: %s", options.Codec)
}
if options.Codec == "v0" {
if options.MhType != mh.SHA2_256 || (options.MhLength != -1 && options.MhLength != 32) {
return nil, cid.Prefix{}, fmt.Errorf("only sha2-255-32 is allowed with CIDv0")
}
}
pref.Codec = formatval
pref.MhType = options.MhType
pref.MhLength = options.MhLength
return options, pref, nil
} }
func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) { func BlockRmOptions(opts ...BlockRmOption) (*BlockRmSettings, error) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment