mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-22 21:21:43 +08:00
coreapi unixfs: options for RawLeaves / Inline
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
@ -52,3 +52,18 @@ func (unixfsOpts) Hash(mhtype uint64) UnixfsAddOption {
|
|||||||
return nil
|
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"
|
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
|
||||||
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
|
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"
|
uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
|
||||||
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
|
dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
|
||||||
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
|
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.Name = pathName
|
||||||
fileAdder.CidBuilder = prefix
|
fileAdder.CidBuilder = prefix
|
||||||
|
|
||||||
|
if settings.InlineLimit > 0 {
|
||||||
|
fileAdder.CidBuilder = cidutil.InlineBuilder{
|
||||||
|
Builder: fileAdder.CidBuilder,
|
||||||
|
Limit: settings.InlineLimit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
|
err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -140,6 +140,7 @@ func TestAdd(t *testing.T) {
|
|||||||
err string
|
err string
|
||||||
opts []options.UnixfsAddOption
|
opts []options.UnixfsAddOption
|
||||||
}{
|
}{
|
||||||
|
// Simple cases
|
||||||
{
|
{
|
||||||
name: "simpleAdd",
|
name: "simpleAdd",
|
||||||
data: helloStr,
|
data: helloStr,
|
||||||
@ -151,12 +152,20 @@ func TestAdd(t *testing.T) {
|
|||||||
data: "",
|
data: "",
|
||||||
path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
|
path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
|
||||||
},
|
},
|
||||||
|
// CIDv1 version / rawLeaves
|
||||||
{
|
{
|
||||||
name: "addCidV1",
|
name: "addCidV1",
|
||||||
data: helloStr,
|
data: helloStr,
|
||||||
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
|
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
|
||||||
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)},
|
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",
|
name: "addCidSha3",
|
||||||
data: helloStr,
|
data: helloStr,
|
||||||
@ -169,6 +178,19 @@ func TestAdd(t *testing.T) {
|
|||||||
err: "CIDv0 only supports sha2-256",
|
err: "CIDv0 only supports sha2-256",
|
||||||
opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_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 {
|
for _, testCase := range cases {
|
||||||
|
Reference in New Issue
Block a user