1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 19:19:06 +08:00

coreapi unixfs: wrap option

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera
2018-10-03 22:30:45 +02:00
parent c6f8e7e2a6
commit 18fec6eb5f
3 changed files with 77 additions and 17 deletions

View File

@ -31,6 +31,8 @@ type UnixfsAddSettings struct {
Pin bool Pin bool
OnlyHash bool OnlyHash bool
Local bool Local bool
Wrap bool
} }
type UnixfsAddOption func(*UnixfsAddSettings) error type UnixfsAddOption func(*UnixfsAddSettings) error
@ -51,6 +53,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
Pin: false, Pin: false,
OnlyHash: false, OnlyHash: false,
Local: false, Local: false,
Wrap: false,
} }
for _, opt := range opts { for _, opt := range opts {
@ -202,3 +206,12 @@ func (unixfsOpts) Local(local bool) UnixfsAddOption {
return nil return nil
} }
} }
// Wrap tells the adder to wrap the added file structure with an additional
// directory.
func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.Wrap = wrap
return nil
}
}

View File

@ -66,7 +66,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
fileAdder.Chunker = settings.Chunker fileAdder.Chunker = settings.Chunker
//fileAdder.Progress = progress //fileAdder.Progress = progress
//fileAdder.Hidden = hidden //fileAdder.Hidden = hidden
//fileAdder.Wrap = wrap fileAdder.Wrap = settings.Wrap
fileAdder.Pin = settings.Pin && !settings.OnlyHash fileAdder.Pin = settings.Pin && !settings.OnlyHash
fileAdder.Silent = true fileAdder.Silent = true
fileAdder.RawLeaves = settings.RawLeaves fileAdder.RawLeaves = settings.RawLeaves

View File

@ -134,6 +134,18 @@ func strFile(data string) func() files.File {
} }
} }
func twoLevelDir() func() files.File {
return func() files.File {
return files.NewSliceFile("t", "t", []files.File{
files.NewSliceFile("t/abc", "t/abc", []files.File{
files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil),
}),
files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
})
}
}
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
ctx := context.Background() ctx := context.Background()
_, api, err := makeAPI(ctx) _, api, err := makeAPI(ctx)
@ -144,9 +156,13 @@ func TestAdd(t *testing.T) {
cases := []struct { cases := []struct {
name string name string
data func() files.File data func() files.File
path string path string
err string err string
recursive bool recursive bool
wrapped bool
opts []options.UnixfsAddOption opts []options.UnixfsAddOption
}{ }{
// Simple cases // Simple cases
@ -232,6 +248,12 @@ func TestAdd(t *testing.T) {
path: hello, path: hello,
opts: []options.UnixfsAddOption{options.Unixfs.Local(true)}, opts: []options.UnixfsAddOption{options.Unixfs.Local(true)},
}, },
{
name: "hashOnly", // test (non)fetchability
data: strFile(helloStr),
path: hello,
opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)},
},
// multi file // multi file
{ {
name: "simpleDir", name: "simpleDir",
@ -246,18 +268,36 @@ func TestAdd(t *testing.T) {
}, },
{ {
name: "twoLevelDir", name: "twoLevelDir",
data: func() files.File { data: twoLevelDir(),
return files.NewSliceFile("t", "t", []files.File{
files.NewSliceFile("t/abc", "t/abc", []files.File{
files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil),
}),
files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
})
},
recursive: true, recursive: true,
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
}, },
// wrapped
{
name: "addWrapped",
path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE",
data: func() files.File {
return files.NewReaderFile("foo", "foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil)
},
wrapped: true,
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "twoLevelDirWrapped",
data: twoLevelDir(),
recursive: true,
wrapped: true,
path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
},
{
name: "twoLevelInlineHash",
data: twoLevelDir(),
recursive: true,
wrapped: true,
path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi",
opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
},
} }
for _, testCase := range cases { for _, testCase := range cases {
@ -338,7 +378,14 @@ func TestAdd(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
cmpFile(testCase.data(), f) orig := testCase.data()
if testCase.wrapped {
orig = files.NewSliceFile("", "", []files.File{
orig,
})
}
cmpFile(orig, f)
}) })
} }
} }