提交 041e55d7 作者: Łukasz Magiera

coreapi unixfs: options for RawLeaves / Inline

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 06a4218a
......@@ -52,3 +52,18 @@ func (unixfsOpts) Hash(mhtype uint64) UnixfsAddOption {
return nil
}
}
func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.RawLeaves = enable
settings.RawLeavesSet = true
return nil
}
}
func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.InlineLimit = limit
return nil
}
}
......@@ -12,7 +12,8 @@ import (
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
"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"
......@@ -79,6 +80,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
//fileAdder.Name = pathName
fileAdder.CidBuilder = prefix
if settings.InlineLimit > 0 {
fileAdder.CidBuilder = cidutil.InlineBuilder{
Builder: fileAdder.CidBuilder,
Limit: settings.InlineLimit,
}
}
err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
if err != nil {
return nil, err
......
......@@ -140,6 +140,7 @@ func TestAdd(t *testing.T) {
err string
opts []options.UnixfsAddOption
}{
// Simple cases
{
name: "simpleAdd",
data: helloStr,
......@@ -151,6 +152,7 @@ func TestAdd(t *testing.T) {
data: "",
path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
},
// CIDv1 version / rawLeaves
{
name: "addCidV1",
data: helloStr,
......@@ -158,6 +160,13 @@ func TestAdd(t *testing.T) {
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)},
},
{
name: "addCidV1NoLeaves",
data: helloStr,
path: "/ipfs/zdj7WY4GbN8NDbTW1dfCShAQNVovams2xhq9hVCx5vXcjvT8g",
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1), options.Unixfs.RawLeaves(false)},
},
// Non sha256 hash vs CID
{
name: "addCidSha3",
data: helloStr,
path: "/ipfs/zb2wwnYtXBxpndNABjtYxWAPt3cwWNRnc11iT63fvkYV78iRb",
......@@ -169,6 +178,19 @@ func TestAdd(t *testing.T) {
err: "CIDv0 only supports sha2-256",
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_256)},
},
// Inline
{
name: "addInline",
data: helloStr,
path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32)},
},
{ //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
name: "addInlineRaw",
data: helloStr,
path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)},
},
}
for _, testCase := range cases {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论