@cryptotaxi247 / kubo / commits / 041e55d70

coreapi unixfs: options for RawLeaves / Inline

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Sep 20, 2018 at 16:19 UTC 041e55d7066c78f6d209460548c4eb4b858c6f60
3 files changed +46 -1
core/coreapi/interface/options/unixfs.go
+15
@@ -52,3 +52,18 @@ func (unixfsOpts) Hash(mhtype uint64) UnixfsAddOption {
52 return nil
53 }
54 }
55 +
56 +func (unixfsOpts) RawLeaves(enable bool) UnixfsAddOption {
57 + return func(settings *UnixfsAddSettings) error {
58 + settings.RawLeaves = enable
59 + settings.RawLeavesSet = true
60 + return nil
61 + }
62 +}
63 +
64 +func (unixfsOpts) InlineLimit(limit int) UnixfsAddOption {
65 + return func(settings *UnixfsAddSettings) error {
66 + settings.InlineLimit = limit
67 + return nil
68 + }
69 +}
core/coreapi/unixfs.go
+9 -1
@@ -12,7 +12,8 @@ import (
12
13 cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
14 mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
15 - files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
15 + cidutil "gx/ipfs/QmQJSeE3CX4zos9qeaG8EhecEK9zvrTEfTG84J8C5NVRwt/go-cidutil"
16 + "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
17 uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
18 dag "gx/ipfs/QmcBoNcAP6qDjgRBew7yjvCqHq7p5jMstE44jPUBWBxzsV/go-merkledag"
19 ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
@@ -79,6 +80,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.
80 //fileAdder.Name = pathName
81 fileAdder.CidBuilder = prefix
82
83 + if settings.InlineLimit > 0 {
84 + fileAdder.CidBuilder = cidutil.InlineBuilder{
85 + Builder: fileAdder.CidBuilder,
86 + Limit: settings.InlineLimit,
87 + }
88 + }
89 +
90 err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
91 if err != nil {
92 return nil, err
core/coreapi/unixfs_test.go
+22
@@ -140,6 +140,7 @@ func TestAdd(t *testing.T) {
140 err string
141 opts []options.UnixfsAddOption
142 }{
143 + // Simple cases
144 {
145 name: "simpleAdd",
146 data: helloStr,
@@ -151,12 +152,20 @@ func TestAdd(t *testing.T) {
152 data: "",
153 path: "/ipfs/QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH",
154 },
155 + // CIDv1 version / rawLeaves
156 {
157 name: "addCidV1",
158 data: helloStr,
159 path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
160 opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1)},
161 },
162 + {
163 + name: "addCidV1NoLeaves",
164 + data: helloStr,
165 + path: "/ipfs/zdj7WY4GbN8NDbTW1dfCShAQNVovams2xhq9hVCx5vXcjvT8g",
166 + opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(1), options.Unixfs.RawLeaves(false)},
167 + },
168 + // Non sha256 hash vs CID
169 {
170 name: "addCidSha3",
171 data: helloStr,
@@ -169,6 +178,19 @@ func TestAdd(t *testing.T) {
178 err: "CIDv0 only supports sha2-256",
179 opts: []options.UnixfsAddOption{options.Unixfs.CidVersion(0), options.Unixfs.Hash(mh.SHA3_256)},
180 },
181 + // Inline
182 + {
183 + name: "addInline",
184 + data: helloStr,
185 + path: "/ipfs/zaYomJdLndMku8P9LHngHB5w2CQ7NenLbv",
186 + opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32)},
187 + },
188 + { //TODO: after coreapi add is used in `ipfs add`, consider making this default for inline
189 + name: "addInlineRaw",
190 + data: helloStr,
191 + path: "/ipfs/zj7Gr8AcBreqGEfrnR5kPFe",
192 + opts: []options.UnixfsAddOption{options.Unixfs.InlineLimit(32), options.Unixfs.RawLeaves(true)},
193 + },
194 }
195
196 for _, testCase := range cases {