From a4cb06027a5e0b3fe8a7d8a847c2f86a5d3dd0f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 15 Oct 2018 15:13:50 +0200 Subject: [PATCH] coreapi unixfs: use session for resolving too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/coreapi.go | 4 +++- core/coreapi/path.go | 12 +++++++++--- core/coreapi/unixfs.go | 9 +++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index ba6082626..434e164e2 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -18,17 +18,19 @@ import ( coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" + ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" ) var log = logging.Logger("core/coreapi") type CoreAPI struct { node *core.IpfsNode + dag ipld.DAGService } // NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node. func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI { - api := &CoreAPI{n} + api := &CoreAPI{n, n.DAG} return api } diff --git a/core/coreapi/path.go b/core/coreapi/path.go index 1fbc7fda8..4ff835cbe 100644 --- a/core/coreapi/path.go +++ b/core/coreapi/path.go @@ -7,12 +7,13 @@ import ( "github.com/ipfs/go-ipfs/core" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" - uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io" - ipfspath "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" - "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path/resolver" "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid" + dag "gx/ipfs/QmVvNkTCx8V9Zei8xuTYTBdUXmbnDRS4iNuw1SztYyhQwQ/go-merkledag" + uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" + ipfspath "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path" + "gx/ipfs/QmdrpbDgeYH3VxkCciQCJY5LkDYdXtig6unDzQmMxFtWEw/go-path/resolver" ) // 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 return coreiface.NewResolvedPath(ipath, node, root, gopath.Join(rest...)), nil } + +func (api *CoreAPI) getSession(ctx context.Context) *CoreAPI { + ng := dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag)) + return &CoreAPI{api.node, ng} +} diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 0b725a591..f6f2bd22a 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -134,13 +134,14 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options } func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (coreiface.UnixfsFile, error) { - nd, err := api.core().ResolveNode(ctx, p) + ses := api.core().getSession(ctx) + + nd, err := ses.ResolveNode(ctx, p) if err != nil { return nil, err } - ses := dag.NewReadOnlyDagService(dag.NewSession(ctx, api.node.DAG)) - return newUnixfsFile(ctx, ses, nd, "", nil) + return newUnixfsFile(ctx, ses.dag, nd, "", nil) } // 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 return links, nil } -func (api *UnixfsAPI) core() coreiface.CoreAPI { +func (api *UnixfsAPI) core() *CoreAPI { return (*CoreAPI)(api) }