@cryptotaxi247 / kubo / commits / 3e754d64f

coreapi unixfs: hidden opiton

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

Łukasz Magiera committed Oct 3, 2018 at 22:49 UTC 3e754d64fbd9770a1e09af3dfb492f599b7fe21f
3 files changed +69 -14
core/coreapi/interface/options/unixfs.go
+12 -2
@@ -32,7 +32,8 @@ type UnixfsAddSettings struct {
32 OnlyHash bool
33 Local bool
34
35 - Wrap bool
35 + Wrap bool
36 + Hidden bool
37 }
38
39 type UnixfsAddOption func(*UnixfsAddSettings) error
@@ -54,7 +55,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
55 OnlyHash: false,
56 Local: false,
57
57 - Wrap: false,
58 + Wrap: false,
59 + Hidden: false,
60 }
61
62 for _, opt := range opts {
@@ -215,3 +217,11 @@ func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption {
217 return nil
218 }
219 }
220 +
221 +// Hidden enables adding of hidden files (files prefixed with '.')
222 +func (unixfsOpts) Hidden(hidden bool) UnixfsAddOption {
223 + return func(settings *UnixfsAddSettings) error {
224 + settings.Hidden = hidden
225 + return nil
226 + }
227 +}
core/coreapi/unixfs.go
+1 -1
@@ -65,7 +65,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
65
66 fileAdder.Chunker = settings.Chunker
67 //fileAdder.Progress = progress
68 - //fileAdder.Hidden = hidden
68 + fileAdder.Hidden = settings.Hidden
69 fileAdder.Wrap = settings.Wrap
70 fileAdder.Pin = settings.Pin && !settings.OnlyHash
71 fileAdder.Silent = true
core/coreapi/unixfs_test.go
+56 -11
@@ -146,6 +146,12 @@ func twoLevelDir() func() files.File {
146 }
147 }
148
149 +func wrapped(f files.File) files.File {
150 + return files.NewSliceFile("", "", []files.File{
151 + f,
152 + })
153 +}
154 +
155 func TestAdd(t *testing.T) {
156 ctx := context.Background()
157 _, api, err := makeAPI(ctx)
@@ -154,14 +160,14 @@ func TestAdd(t *testing.T) {
160 }
161
162 cases := []struct {
157 - name string
158 - data func() files.File
163 + name string
164 + data func() files.File
165 + expect func(files.File) files.File
166
167 path string
168 err string
169
170 recursive bool
164 - wrapped bool
171
172 opts []options.UnixfsAddOption
173 }{
@@ -279,14 +285,14 @@ func TestAdd(t *testing.T) {
285 data: func() files.File {
286 return files.NewReaderFile("foo", "foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil)
287 },
282 - wrapped: true,
283 - opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
288 + expect: wrapped,
289 + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
290 },
291 {
292 name: "twoLevelDirWrapped",
293 data: twoLevelDir(),
294 recursive: true,
289 - wrapped: true,
295 + expect: wrapped,
296 path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
297 opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
298 },
@@ -294,10 +300,51 @@ func TestAdd(t *testing.T) {
300 name: "twoLevelInlineHash",
301 data: twoLevelDir(),
302 recursive: true,
297 - wrapped: true,
303 + expect: wrapped,
304 path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi",
305 opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
306 },
307 + // hidden
308 + {
309 + name: "hiddenFiles",
310 + data: func() files.File {
311 + return files.NewSliceFile("t", "t", []files.File{
312 + files.NewReaderFile("t/.bar", "t/.bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
313 + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
314 + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
315 + })
316 + },
317 + recursive: true,
318 + path: "/ipfs/QmehGvpf2hY196MzDFmjL8Wy27S4jbgGDUAhBJyvXAwr3g",
319 + opts: []options.UnixfsAddOption{options.Unixfs.Hidden(true)},
320 + },
321 + {
322 + name: "hiddenFileAlwaysAdded",
323 + data: func() files.File {
324 + return files.NewReaderFile(".foo", ".foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil)
325 + },
326 + recursive: true,
327 + path: hello,
328 + },
329 + {
330 + name: "hiddenFilesNotAdded",
331 + data: func() files.File {
332 + return files.NewSliceFile("t", "t", []files.File{
333 + files.NewReaderFile("t/.bar", "t/.bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
334 + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
335 + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
336 + })
337 + },
338 + expect: func(files.File) files.File {
339 + return files.NewSliceFile("t", "t", []files.File{
340 + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
341 + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
342 + })
343 + },
344 + recursive: true,
345 + path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
346 + opts: []options.UnixfsAddOption{options.Unixfs.Hidden(false)},
347 + },
348 }
349
350 for _, testCase := range cases {
@@ -379,10 +426,8 @@ func TestAdd(t *testing.T) {
426 }
427
428 orig := testCase.data()
382 - if testCase.wrapped {
383 - orig = files.NewSliceFile("", "", []files.File{
384 - orig,
385 - })
429 + if testCase.expect != nil {
430 + orig = testCase.expect(orig)
431 }
432
433 cmpFile(orig, f)