coreapi unixfs: use fileAdder directly
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Sep 20, 2018 at 15:00 UTC
795d1ea5400eb3bb18d8b248e8494251ea8db359
6 files changed
+98
-13
core/coreapi/interface/options/unixfs.go
new
+50
@@ -0,0 +1,50 @@
1
+package options
2
+
3
+import (
4
+ mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
5
+)
6
+
7
+type UnixfsAddSettings struct {
8
+ CidVersion int
9
+ MhType uint64
10
+
11
+ InlineLimit int
12
+}
13
+
14
+type UnixfsAddOption func(*UnixfsAddSettings) error
15
+
16
+func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, error) {
17
+ options := &UnixfsAddSettings{
18
+ CidVersion: -1,
19
+ MhType: mh.SHA2_256,
20
+
21
+ InlineLimit: 0,
22
+ }
23
+
24
+ for _, opt := range opts {
25
+ err := opt(options)
26
+ if err != nil {
27
+ return nil, err
28
+ }
29
+ }
30
+
31
+ return options, nil
32
+}
33
+
34
+type unixfsOpts struct{}
35
+
36
+var Unixfs unixfsOpts
37
+
38
+func (unixfsOpts) CidVersion(version int) UnixfsAddOption {
39
+ return func(settings *UnixfsAddSettings) error {
40
+ settings.CidVersion = version
41
+ return nil
42
+ }
43
+}
44
+
45
+func (unixfsOpts) Hash(mhtype uint64) UnixfsAddOption {
46
+ return func(settings *UnixfsAddSettings) error {
47
+ settings.MhType = mhtype
48
+ return nil
49
+ }
50
+}
core/coreapi/interface/unixfs.go
+3
-1
@@ -4,13 +4,15 @@ import (
4
"context"
5
"io"
6
7
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8
+
9
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
10
)
11
12
// UnixfsAPI is the basic interface to immutable files in IPFS
13
type UnixfsAPI interface {
14
// Add imports the data from the reader into merkledag file
13
- Add(context.Context, io.Reader) (ResolvedPath, error)
15
+ Add(context.Context, io.ReadCloser, ...options.UnixfsAddOption) (ResolvedPath, error)
16
17
// Cat returns a reader for the file
18
Cat(context.Context, Path) (Reader, error)
core/coreapi/name_test.go
+2
-1
@@ -3,6 +3,7 @@ package coreapi_test
3
import (
4
"context"
5
"io"
6
+ "io/ioutil"
7
"math/rand"
8
"testing"
9
"time"
@@ -16,7 +17,7 @@ import (
17
var rnd = rand.New(rand.NewSource(0x62796532303137))
18
19
func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
19
- return api.Unixfs().Add(ctx, &io.LimitedReader{R: rnd, N: 4092})
20
+ return api.Unixfs().Add(ctx, ioutil.NopCloser(&io.LimitedReader{R: rnd, N: 4092}))
21
}
22
23
func TestBasicPublishResolve(t *testing.T) {
core/coreapi/pin_test.go
+5
-4
@@ -2,6 +2,7 @@ package coreapi_test
2
3
import (
4
"context"
5
+ "io/ioutil"
6
"strings"
7
"testing"
8
@@ -15,7 +16,7 @@ func TestPinAdd(t *testing.T) {
16
t.Error(err)
17
}
18
18
- p, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
19
+ p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(strings.NewReader("foo")))
20
if err != nil {
21
t.Error(err)
22
}
@@ -33,7 +34,7 @@ func TestPinSimple(t *testing.T) {
34
t.Error(err)
35
}
36
36
- p, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
37
+ p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(strings.NewReader("foo")))
38
if err != nil {
39
t.Error(err)
40
}
@@ -82,12 +83,12 @@ func TestPinRecursive(t *testing.T) {
83
t.Error(err)
84
}
85
85
- p0, err := api.Unixfs().Add(ctx, strings.NewReader("foo"))
86
+ p0, err := api.Unixfs().Add(ctx, ioutil.NopCloser(strings.NewReader("foo")))
87
if err != nil {
88
t.Error(err)
89
}
90
90
- p1, err := api.Unixfs().Add(ctx, strings.NewReader("bar"))
91
+ p1, err := api.Unixfs().Add(ctx, ioutil.NopCloser(strings.NewReader("bar")))
92
if err != nil {
93
t.Error(err)
94
}
core/coreapi/unixfs.go
+35
-5
@@ -5,10 +5,12 @@ import (
5
"io"
6
7
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8
+ options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
coreunix "github.com/ipfs/go-ipfs/core/coreunix"
9
- uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
10
11
cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
12
+ files "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit/files"
13
+ uio "gx/ipfs/QmU4x3742bvgfxJsByEDpBnifJqjJdV6x528co4hwKCn46/go-unixfs/io"
14
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
15
)
16
@@ -16,16 +18,44 @@ type UnixfsAPI CoreAPI
18
19
// Add builds a merkledag node from a reader, adds it to the blockstore,
20
// and returns the key representing that node.
19
-func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.ResolvedPath, error) {
20
- k, err := coreunix.AddWithContext(ctx, api.node, r)
21
+func (api *UnixfsAPI) Add(ctx context.Context, r io.ReadCloser, opts ...options.UnixfsAddOption) (coreiface.ResolvedPath, error) {
22
+ _, err := options.UnixfsAddOptions(opts...)
23
+ if err != nil {
24
+ return nil, err
25
+ }
26
+
27
+ outChan := make(chan interface{}, 1)
28
+
29
+ fileAdder, err := coreunix.NewAdder(ctx, api.node.Pinning, api.node.Blockstore, api.node.DAG)
30
if err != nil {
31
return nil, err
32
}
24
- c, err := cid.Decode(k)
33
+
34
+ fileAdder.Out = outChan
35
+
36
+ err = fileAdder.AddFile(files.NewReaderFile("", "", r, nil))
37
if err != nil {
38
return nil, err
39
}
28
- return coreiface.IpfsPath(c), nil
40
+
41
+ if _, err = fileAdder.Finalize(); err != nil {
42
+ return nil, err
43
+ }
44
+
45
+ for {
46
+ select {
47
+ case r := <-outChan:
48
+ output := r.(*coreunix.AddedObject)
49
+ if output.Hash != "" {
50
+ c, err := cid.Parse(output.Hash)
51
+ if err != nil {
52
+ return nil, err
53
+ }
54
+
55
+ return coreiface.IpfsPath(c), err
56
+ }
57
+ }
58
+ }
59
}
60
61
// Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
core/coreapi/unixfs_test.go
+3
-2
@@ -6,6 +6,7 @@ import (
6
"encoding/base64"
7
"fmt"
8
"io"
9
+ "io/ioutil"
10
"math"
11
"strings"
12
"testing"
@@ -133,7 +134,7 @@ func TestAdd(t *testing.T) {
134
}
135
136
str := strings.NewReader(helloStr)
136
- p, err := api.Unixfs().Add(ctx, str)
137
+ p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(str))
138
if err != nil {
139
t.Error(err)
140
}
@@ -165,7 +166,7 @@ func TestAddEmptyFile(t *testing.T) {
166
}
167
168
str := strings.NewReader("")
168
- p, err := api.Unixfs().Add(ctx, str)
169
+ p, err := api.Unixfs().Add(ctx, ioutil.NopCloser(str))
170
if err != nil {
171
t.Error(err)
172
}