提交 c68ab562 作者: Łukasz Magiera

coreapi unixfs: cid prefix options

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 795d1ea5
...@@ -9,6 +9,8 @@ type UnixfsAddSettings struct { ...@@ -9,6 +9,8 @@ type UnixfsAddSettings struct {
MhType uint64 MhType uint64
InlineLimit int InlineLimit int
RawLeaves bool
RawLeavesSet bool
} }
type UnixfsAddOption func(*UnixfsAddSettings) error type UnixfsAddOption func(*UnixfsAddSettings) error
...@@ -19,6 +21,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) { ...@@ -19,6 +21,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
MhType: mh.SHA2_256, MhType: mh.SHA2_256,
InlineLimit: 0, InlineLimit: 0,
RawLeaves: false,
RawLeavesSet: false,
} }
for _, opt := range opts { for _, opt := range opts {
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
) )
// UnixfsAPI is the basic interface to immutable files in IPFS // 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 { type UnixfsAPI interface {
// Add imports the data from the reader into merkledag file // Add imports the data from the reader into merkledag file
Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error) Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error)
......
...@@ -2,15 +2,19 @@ package coreapi ...@@ -2,15 +2,19 @@ package coreapi
import ( import (
"context" "context"
"errors"
"fmt"
"io" "io"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
options "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/coreunix"
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files" files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io" uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
) )
...@@ -19,11 +23,42 @@ type UnixfsAPI CoreAPI ...@@ -19,11 +23,42 @@ type UnixfsAPI CoreAPI
// Add builds a merkledag node from a reader, adds it to the blockstore, // Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node. // and returns the key representing that node.
func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) { 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 { if err != nil {
return nil, err 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) outChan := make(chan interface{}, 1)
fileAdder, err := coreunix.NewAdder(ctx, api.node.Pinning, api.node.Blockstore, api.node.DAG) 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. ...@@ -32,6 +67,17 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
} }
fileAdder.Out = outChan 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)) err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
if err != nil { if err != nil {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论