diff --git a/core/coreapi/interface/options/unixfs.go b/core/coreapi/interface/options/unixfs.go index ffed75577..3c46ed086 100644 --- a/core/coreapi/interface/options/unixfs.go +++ b/core/coreapi/interface/options/unixfs.go @@ -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 + } +} diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index afa66f0ae..19e4fc90c 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -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 diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index f8f63b909..c5d483a08 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -140,6 +140,7 @@ func TestAdd(t *testing.T) { err string opts []options.UnixfsAddOption }{ + // Simple cases { name: "simpleAdd", data: helloStr, @@ -151,12 +152,20 @@ func TestAdd(t *testing.T) { data: "", path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH", }, + // CIDv1 version / rawLeaves { name: "addCidV1", data: helloStr, path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", 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, @@ -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 {