coreapi unixfs: use session for resolving too
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 15, 2018 at 15:13 UTC
a4cb06027a5e0b3fe8a7d8a847c2f86a5d3dd0f7
3 files changed
+17
-8
core/coreapi/coreapi.go
+3
-1
@@ -18,17 +18,19 @@ import (
18
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
19
20
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
21
+ ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
22
)
23
24
var log = logging.Logger("core/coreapi")
25
26
type CoreAPI struct {
27
node *core.IpfsNode
28
+ dag ipld.DAGService
29
}
30
31
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
32
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
31
- api := &CoreAPI{n}
33
+ api := &CoreAPI{n, n.DAG}
34
return api
35
}
36
core/coreapi/path.go
+9
-3
@@ -7,12 +7,13 @@ import (
7
8
"github.com/ipfs/go-ipfs/core"
9
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10
- uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io"
11
- ipfspath "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path"
12
- "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path/resolver"
10
11
"gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
12
+ dag "gx/ipfs/QmVvNkTCx8V9Zei8xuTYTBdUXmbnDRS4iNuw1SztYyhQwQ/go-merkledag"
13
+ uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io"
14
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
15
+ ipfspath "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path"
16
+ "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path/resolver"
17
)
18
19
// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
@@ -73,3 +74,8 @@ func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreifac
74
75
return coreiface.NewResolvedPath(ipath, node, root, gopath.Join(rest...)), nil
76
}
77
+
78
+func (api *CoreAPI) getSession(ctx context.Context) *CoreAPI {
79
+ ng := dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag))
80
+ return &CoreAPI{api.node, ng}
81
+}
core/coreapi/unixfs.go
+5
-4
@@ -134,13 +134,14 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
134
}
135
136
func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (coreiface.UnixfsFile, error) {
137
- nd, err := api.core().ResolveNode(ctx, p)
137
+ ses := api.core().getSession(ctx)
138
+
139
+ nd, err := ses.ResolveNode(ctx, p)
140
if err != nil {
141
return nil, err
142
}
143
142
- ses := dag.NewReadOnlyDagService(dag.NewSession(ctx, api.node.DAG))
143
- return newUnixfsFile(ctx, ses, nd, "", nil)
144
+ return newUnixfsFile(ctx, ses.dag, nd, "", nil)
145
}
146
147
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format:
@@ -173,6 +174,6 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, e
174
return links, nil
175
}
176
176
-func (api *UnixfsAPI) core() coreiface.CoreAPI {
177
+func (api *UnixfsAPI) core() *CoreAPI {
178
return (*CoreAPI)(api)
179
}