提交 a4cb0602 作者: Łukasz Magiera

coreapi unixfs: use session for resolving too

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 faf5230e
...@@ -18,17 +18,19 @@ import ( ...@@ -18,17 +18,19 @@ import (
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log" logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
) )
var log = logging.Logger("core/coreapi") var log = logging.Logger("core/coreapi")
type CoreAPI struct { type CoreAPI struct {
node *core.IpfsNode node *core.IpfsNode
dag ipld.DAGService
} }
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node. // NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI { func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
api := &CoreAPI{n} api := &CoreAPI{n, n.DAG}
return api return api
} }
......
...@@ -7,12 +7,13 @@ import ( ...@@ -7,12 +7,13 @@ import (
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" 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" "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" 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 // 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 ...@@ -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 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}
}
...@@ -134,13 +134,14 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options ...@@ -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) { 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 { if err != nil {
return nil, err return nil, err
} }
ses := dag.NewReadOnlyDagService(dag.NewSession(ctx, api.node.DAG)) return newUnixfsFile(ctx, ses.dag, nd, "", nil)
return newUnixfsFile(ctx, ses, nd, "", nil)
} }
// Ls returns the contents of an IPFS or IPNS object(s) at path p, with the format: // 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 ...@@ -173,6 +174,6 @@ func (api *UnixfsAPI) Ls(ctx context.Context, p coreiface.Path) ([]*ipld.Link, e
return links, nil return links, nil
} }
func (api *UnixfsAPI) core() coreiface.CoreAPI { func (api *UnixfsAPI) core() *CoreAPI {
return (*CoreAPI)(api) return (*CoreAPI)(api)
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论