mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 02:21:48 +08:00
coreapi unixfs: cleanup options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -1,7 +1,12 @@
|
||||
package options
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
|
||||
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
|
||||
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
|
||||
)
|
||||
|
||||
type Layout int
|
||||
@ -29,7 +34,7 @@ type UnixfsAddSettings struct {
|
||||
|
||||
type UnixfsAddOption func(*UnixfsAddSettings) error
|
||||
|
||||
func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
|
||||
func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, error) {
|
||||
options := &UnixfsAddSettings{
|
||||
CidVersion: -1,
|
||||
MhType: mh.SHA2_256,
|
||||
@ -49,11 +54,41 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
|
||||
for _, opt := range opts {
|
||||
err := opt(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, cid.Prefix{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return options, nil
|
||||
// (hash != "sha2-256") -> CIDv1
|
||||
if options.MhType != mh.SHA2_256 {
|
||||
switch options.CidVersion {
|
||||
case 0:
|
||||
return nil, cid.Prefix{}, errors.New("CIDv0 only supports sha2-256")
|
||||
case 1, -1:
|
||||
options.CidVersion = 1
|
||||
default:
|
||||
return nil, cid.Prefix{}, fmt.Errorf("unknown CID version: %d", options.CidVersion)
|
||||
}
|
||||
} else {
|
||||
if options.CidVersion < 0 {
|
||||
// Default to CIDv0
|
||||
options.CidVersion = 0
|
||||
}
|
||||
}
|
||||
|
||||
// cidV1 -> raw blocks (by default)
|
||||
if options.CidVersion > 0 && !options.RawLeavesSet {
|
||||
options.RawLeaves = true
|
||||
}
|
||||
|
||||
prefix, err := dag.PrefixForCidVersion(options.CidVersion)
|
||||
if err != nil {
|
||||
return nil, cid.Prefix{}, err
|
||||
}
|
||||
|
||||
prefix.MhType = options.MhType
|
||||
prefix.MhLength = -1
|
||||
|
||||
return options, prefix, nil
|
||||
}
|
||||
|
||||
type unixfsOpts struct{}
|
||||
|
@ -2,7 +2,6 @@ package coreapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/ipfs/go-ipfs/core"
|
||||
"io"
|
||||
@ -11,7 +10,6 @@ import (
|
||||
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
||||
"github.com/ipfs/go-ipfs/core/coreunix"
|
||||
|
||||
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
|
||||
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
|
||||
offline "gx/ipfs/QmR5miWuikPxWyUrzMYJVmFUcD44pGdtc98h9Qsbp4YcJw/go-ipfs-exchange-offline"
|
||||
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
|
||||
@ -30,42 +28,11 @@ type UnixfsAPI CoreAPI
|
||||
// Add builds a merkledag node from a reader, adds it to the blockstore,
|
||||
// and returns the key representing that node.
|
||||
func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) {
|
||||
settings, err := options.UnixfsAddOptions(opts...)
|
||||
settings, prefix, err := options.UnixfsAddOptions(opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: move to options
|
||||
// (hash != "sha2-256") -> CIDv1
|
||||
if settings.MhType != mh.SHA2_256 {
|
||||
switch settings.CidVersion {
|
||||
case 0:
|
||||
return nil, errors.New("CIDv0 only supports sha2-256")
|
||||
case 1, -1:
|
||||
settings.CidVersion = 1
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown CID version: %d", settings.CidVersion)
|
||||
}
|
||||
} else {
|
||||
if settings.CidVersion < 0 {
|
||||
// Default to CIDv0
|
||||
settings.CidVersion = 0
|
||||
}
|
||||
}
|
||||
|
||||
// cidV1 -> raw blocks (by default)
|
||||
if settings.CidVersion > 0 && !settings.RawLeavesSet {
|
||||
settings.RawLeaves = true
|
||||
}
|
||||
|
||||
prefix, err := dag.PrefixForCidVersion(settings.CidVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
prefix.MhType = settings.MhType
|
||||
prefix.MhLength = -1
|
||||
|
||||
n := api.node
|
||||
if settings.OnlyHash {
|
||||
nilnode, err := core.NewNode(ctx, &core.BuildCfg{
|
||||
|
Reference in New Issue
Block a user