mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-03 07:18:12 +08:00
coreapi unixfs: cid prefix options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
parent
795d1ea540
commit
c68ab5620a
@ -8,7 +8,9 @@ type UnixfsAddSettings struct {
|
||||
CidVersion int
|
||||
MhType uint64
|
||||
|
||||
InlineLimit int
|
||||
InlineLimit int
|
||||
RawLeaves bool
|
||||
RawLeavesSet bool
|
||||
}
|
||||
|
||||
type UnixfsAddOption func(*UnixfsAddSettings) error
|
||||
@ -18,7 +20,9 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
|
||||
CidVersion: -1,
|
||||
MhType: mh.SHA2_256,
|
||||
|
||||
InlineLimit: 0,
|
||||
InlineLimit: 0,
|
||||
RawLeaves: false,
|
||||
RawLeavesSet: false,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
|
||||
@ -10,6 +10,7 @@ import (
|
||||
)
|
||||
|
||||
// UnixfsAPI is the basic interface to immutable files in IPFS
|
||||
// NOTE: This API is heavily WIP, things are guaranteed to break frequently
|
||||
type UnixfsAPI interface {
|
||||
// Add imports the data from the reader into merkledag file
|
||||
Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error)
|
||||
|
||||
@ -2,15 +2,19 @@ package coreapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
||||
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
||||
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
|
||||
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
|
||||
"github.com/ipfs/go-ipfs/core/coreunix"
|
||||
|
||||
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
|
||||
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
|
||||
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
|
||||
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
|
||||
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
|
||||
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
|
||||
)
|
||||
|
||||
@ -19,11 +23,42 @@ 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) {
|
||||
_, err := options.UnixfsAddOptions(opts...)
|
||||
settings, 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
|
||||
|
||||
outChan := make(chan interface{}, 1)
|
||||
|
||||
fileAdder, err := coreunix.NewAdder(ctx, api.node.Pinning, api.node.Blockstore, api.node.DAG)
|
||||
@ -32,6 +67,17 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
|
||||
}
|
||||
|
||||
fileAdder.Out = outChan
|
||||
//fileAdder.Chunker = chunker
|
||||
//fileAdder.Progress = progress
|
||||
//fileAdder.Hidden = hidden
|
||||
//fileAdder.Trickle = trickle
|
||||
//fileAdder.Wrap = wrap
|
||||
//fileAdder.Pin = dopin
|
||||
fileAdder.Silent = false
|
||||
fileAdder.RawLeaves = settings.RawLeaves
|
||||
//fileAdder.NoCopy = nocopy
|
||||
//fileAdder.Name = pathName
|
||||
fileAdder.CidBuilder = prefix
|
||||
|
||||
err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
|
||||
if err != nil {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user