提交 338e90e9 作者: Łukasz Magiera

coreapi: expand public path api

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 3218703f
...@@ -65,7 +65,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc ...@@ -65,7 +65,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
return nil, err return nil, err
} }
return ParseCid(b.Cid()), nil return api.ParseCid(b.Cid()), nil
} }
func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) { func (api *BlockAPI) Get(ctx context.Context, p coreiface.Path) (io.Reader, error) {
...@@ -117,7 +117,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc ...@@ -117,7 +117,7 @@ func (api *BlockAPI) Stat(ctx context.Context, p coreiface.Path) (coreiface.Bloc
} }
return &BlockStat{ return &BlockStat{
path: ParseCid(b.Cid()), path: api.ParseCid(b.Cid()),
size: len(b.RawData()), size: len(b.RawData()),
}, nil }, nil
} }
......
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
core "github.com/ipfs/go-ipfs/core" 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"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
namesys "github.com/ipfs/go-ipfs/namesys" namesys "github.com/ipfs/go-ipfs/namesys"
ipfspath "github.com/ipfs/go-ipfs/path" ipfspath "github.com/ipfs/go-ipfs/path"
resolver "github.com/ipfs/go-ipfs/path/resolver" resolver "github.com/ipfs/go-ipfs/path/resolver"
...@@ -29,11 +30,12 @@ import ( ...@@ -29,11 +30,12 @@ import (
type CoreAPI struct { type CoreAPI struct {
node *core.IpfsNode node *core.IpfsNode
*caopts.ApiOptions
} }
// 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, nil}
return api return api
} }
...@@ -132,16 +134,26 @@ type path struct { ...@@ -132,16 +134,26 @@ type path struct {
} }
// ParsePath parses path `p` using ipfspath parser, returns the parsed path. // ParsePath parses path `p` using ipfspath parser, returns the parsed path.
func ParsePath(p string) (coreiface.Path, error) { func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) {
options, err := caopts.ParsePathOptions(opts...)
if err != nil {
return nil, err
}
pp, err := ipfspath.ParsePath(p) pp, err := ipfspath.ParsePath(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &path{path: pp}, nil
res := &path{path: pp}
if options.Resolve {
return api.ResolvePath(ctx, res)
}
return res, nil
} }
// ParseCid parses the path from `c`, returns the parsed path. // ParseCid parses the path from `c`, returns the parsed path.
func ParseCid(c *cid.Cid) coreiface.Path { func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path {
return &path{path: ipfspath.FromCid(c), cid: c, root: c} return &path{path: ipfspath.FromCid(c), cid: c, root: c}
} }
......
...@@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut ...@@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
return nil, err return nil, err
} }
return ParseCid(nds[0].Cid()), nil return api.ParseCid(nds[0].Cid()), nil
} }
// Get resolves `path` using Unixfs resolver, returns the resolved Node. // Get resolves `path` using Unixfs resolver, returns the resolved Node.
...@@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da ...@@ -66,7 +66,7 @@ func (api *DagAPI) Tree(ctx context.Context, p coreiface.Path, opts ...caopts.Da
paths := n.Tree("", settings.Depth) paths := n.Tree("", settings.Depth)
out := make([]coreiface.Path, len(paths)) out := make([]coreiface.Path, len(paths))
for n, p2 := range paths { for n, p2 := range paths {
out[n], err = ParsePath(gopath.Join(p.String(), p2)) out[n], err = api.ParsePath(ctx, gopath.Join(p.String(), p2))
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -6,8 +6,6 @@ import ( ...@@ -6,8 +6,6 @@ import (
"strings" "strings"
"testing" "testing"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options" opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
...@@ -74,7 +72,7 @@ func TestPath(t *testing.T) { ...@@ -74,7 +72,7 @@ func TestPath(t *testing.T) {
t.Error(err) t.Error(err)
} }
p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk")) p, err := api.ParsePath(ctx, path.Join(res.Cid().String(), "lnk"))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
......
...@@ -5,6 +5,9 @@ package iface ...@@ -5,6 +5,9 @@ package iface
import ( import (
"context" "context"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format" ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
) )
...@@ -37,4 +40,14 @@ type CoreAPI interface { ...@@ -37,4 +40,14 @@ type CoreAPI interface {
// ResolveNode resolves the path (if not resolved already) using Unixfs // ResolveNode resolves the path (if not resolved already) using Unixfs
// resolver, gets and returns the resolved Node // resolver, gets and returns the resolved Node
ResolveNode(context.Context, Path) (ipld.Node, error) ResolveNode(context.Context, Path) (ipld.Node, error)
// ParsePath parses string path to a Path
ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)
// WithResolve is an option for ParsePath which when set to true tells
// ParsePath to also resolve the path
WithResolve(bool) options.ParsePathOption
// ParseCid creates new path from the provided CID
ParseCid(*cid.Cid) Path
} }
package options
type ParsePathSettings struct {
Resolve bool
}
type ParsePathOption func(*ParsePathSettings) error
func ParsePathOptions(opts ...ParsePathOption) (*ParsePathSettings, error) {
options := &ParsePathSettings{
Resolve: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type ApiOptions struct{}
func (api *ApiOptions) WithResolve(r bool) ParsePathOption {
return func(settings *ParsePathSettings) error {
settings.Resolve = r
return nil
}
}
...@@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj ...@@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
return nil, err return nil, err
} }
return ParseCid(dagnode.Cid()), nil return api.ParseCid(dagnode.Cid()), nil
} }
func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) { func (api *ObjectAPI) Get(ctx context.Context, path coreiface.Path) (ipld.Node, error) {
...@@ -218,7 +218,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str ...@@ -218,7 +218,7 @@ func (api *ObjectAPI) AddLink(ctx context.Context, base coreiface.Path, name str
return nil, err return nil, err
} }
return ParseCid(nnode.Cid()), nil return api.ParseCid(nnode.Cid()), nil
} }
func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) { func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link string) (coreiface.Path, error) {
...@@ -244,7 +244,7 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri ...@@ -244,7 +244,7 @@ func (api *ObjectAPI) RmLink(ctx context.Context, base coreiface.Path, link stri
return nil, err return nil, err
} }
return ParseCid(nnode.Cid()), nil return api.ParseCid(nnode.Cid()), nil
} }
func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) { func (api *ObjectAPI) AppendData(ctx context.Context, path coreiface.Path, r io.Reader) (coreiface.Path, error) {
...@@ -281,7 +281,7 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R ...@@ -281,7 +281,7 @@ func (api *ObjectAPI) patchData(ctx context.Context, path coreiface.Path, r io.R
return nil, err return nil, err
} }
return ParseCid(pbnd.Cid()), nil return api.ParseCid(pbnd.Cid()), nil
} }
func (api *ObjectAPI) core() coreiface.CoreAPI { func (api *ObjectAPI) core() coreiface.CoreAPI {
......
...@@ -9,11 +9,9 @@ import ( ...@@ -9,11 +9,9 @@ import (
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
corerepo "github.com/ipfs/go-ipfs/core/corerepo" corerepo "github.com/ipfs/go-ipfs/core/corerepo"
merkledag "github.com/ipfs/go-ipfs/merkledag" merkledag "github.com/ipfs/go-ipfs/merkledag"
pin "github.com/ipfs/go-ipfs/pin"
offline "gx/ipfs/QmShbyKV9P7QuFecDHXsgrQ4rxxm71MUkGVpwedT4VQ8Bf/go-ipfs-exchange-offline" offline "gx/ipfs/QmShbyKV9P7QuFecDHXsgrQ4rxxm71MUkGVpwedT4VQ8Bf/go-ipfs-exchange-offline"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid" cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
) )
type PinAPI CoreAPI type PinAPI CoreAPI
...@@ -46,7 +44,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreif ...@@ -46,7 +44,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreif
return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type) return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type)
} }
return pinLsAll(settings.Type, ctx, api.node.Pinning, api.node.DAG) return api.pinLsAll(settings.Type, ctx)
} }
func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error { func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error {
...@@ -75,8 +73,8 @@ type pinStatus struct { ...@@ -75,8 +73,8 @@ type pinStatus struct {
// BadNode is used in PinVerifyRes // BadNode is used in PinVerifyRes
type badNode struct { type badNode struct {
cid *cid.Cid path coreiface.Path
err error err error
} }
func (s *pinStatus) Ok() bool { func (s *pinStatus) Ok() bool {
...@@ -88,7 +86,7 @@ func (s *pinStatus) BadNodes() []coreiface.BadPinNode { ...@@ -88,7 +86,7 @@ func (s *pinStatus) BadNodes() []coreiface.BadPinNode {
} }
func (n *badNode) Path() coreiface.Path { func (n *badNode) Path() coreiface.Path {
return ParseCid(n.cid) return n.path
} }
func (n *badNode) Err() error { func (n *badNode) Err() error {
...@@ -112,7 +110,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro ...@@ -112,7 +110,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
links, err := getLinks(ctx, root) links, err := getLinks(ctx, root)
if err != nil { if err != nil {
status := &pinStatus{ok: false, cid: root} status := &pinStatus{ok: false, cid: root}
status.badNodes = []coreiface.BadPinNode{&badNode{cid: root, err: err}} status.badNodes = []coreiface.BadPinNode{&badNode{path: api.ParseCid(root), err: err}}
visited[key] = status visited[key] = status
return status return status
} }
...@@ -143,18 +141,18 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro ...@@ -143,18 +141,18 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
type pinInfo struct { type pinInfo struct {
pinType string pinType string
object *cid.Cid object coreiface.Path
} }
func (p *pinInfo) Path() coreiface.Path { func (p *pinInfo) Path() coreiface.Path {
return ParseCid(p.object) return p.object
} }
func (p *pinInfo) Type() string { func (p *pinInfo) Type() string {
return p.pinType return p.pinType
} }
func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.DAGService) ([]coreiface.Pin, error) { func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pin, error) {
keys := make(map[string]*pinInfo) keys := make(map[string]*pinInfo)
...@@ -162,18 +160,18 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld. ...@@ -162,18 +160,18 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.
for _, c := range keyList { for _, c := range keyList {
keys[c.String()] = &pinInfo{ keys[c.String()] = &pinInfo{
pinType: typeStr, pinType: typeStr,
object: c, object: api.ParseCid(c),
} }
} }
} }
if typeStr == "direct" || typeStr == "all" { if typeStr == "direct" || typeStr == "all" {
AddToResultKeys(pinning.DirectKeys(), "direct") AddToResultKeys(api.node.Pinning.DirectKeys(), "direct")
} }
if typeStr == "indirect" || typeStr == "all" { if typeStr == "indirect" || typeStr == "all" {
set := cid.NewSet() set := cid.NewSet()
for _, k := range pinning.RecursiveKeys() { for _, k := range api.node.Pinning.RecursiveKeys() {
err := merkledag.EnumerateChildren(ctx, merkledag.GetLinksWithDAG(dag), k, set.Visit) err := merkledag.EnumerateChildren(ctx, merkledag.GetLinksWithDAG(api.node.DAG), k, set.Visit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -181,7 +179,7 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld. ...@@ -181,7 +179,7 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.
AddToResultKeys(set.Keys(), "indirect") AddToResultKeys(set.Keys(), "indirect")
} }
if typeStr == "recursive" || typeStr == "all" { if typeStr == "recursive" || typeStr == "all" {
AddToResultKeys(pinning.RecursiveKeys(), "recursive") AddToResultKeys(api.node.Pinning.RecursiveKeys(), "recursive")
} }
out := make([]coreiface.Pin, 0, len(keys)) out := make([]coreiface.Pin, 0, len(keys))
......
...@@ -25,7 +25,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, err ...@@ -25,7 +25,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ParseCid(c), nil return api.core().ParseCid(c), nil
} }
// Cat returns the data contained by an IPFS or IPNS object(s) at path `p`. // Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
......
...@@ -206,7 +206,7 @@ func TestCatDir(t *testing.T) { ...@@ -206,7 +206,7 @@ func TestCatDir(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
p := coreapi.ParseCid(edir.Cid()) p := api.ParseCid(edir.Cid())
if p.String() != emptyDir.String() { if p.String() != emptyDir.String() {
t.Fatalf("expected path %s, got: %s", emptyDir, p) t.Fatalf("expected path %s, got: %s", emptyDir, p)
...@@ -231,7 +231,7 @@ func TestCatNonUnixfs(t *testing.T) { ...@@ -231,7 +231,7 @@ func TestCatNonUnixfs(t *testing.T) {
t.Error(err) t.Error(err)
} }
_, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(nd.Cid())) _, err = api.Unixfs().Cat(ctx, api.ParseCid(nd.Cid()))
if !strings.Contains(err.Error(), "proto: required field") { if !strings.Contains(err.Error(), "proto: required field") {
t.Fatalf("expected protobuf error, got: %s", err) t.Fatalf("expected protobuf error, got: %s", err)
} }
...@@ -327,7 +327,7 @@ func TestLsNonUnixfs(t *testing.T) { ...@@ -327,7 +327,7 @@ func TestLsNonUnixfs(t *testing.T) {
t.Error(err) t.Error(err)
} }
links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(nd.Cid())) links, err := api.Unixfs().Ls(ctx, api.ParseCid(nd.Cid()))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
......
...@@ -13,7 +13,6 @@ import ( ...@@ -13,7 +13,6 @@ import (
"time" "time"
core "github.com/ipfs/go-ipfs/core" core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/importer" "github.com/ipfs/go-ipfs/importer"
dag "github.com/ipfs/go-ipfs/merkledag" dag "github.com/ipfs/go-ipfs/merkledag"
...@@ -160,7 +159,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -160,7 +159,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
ipnsHostname = true ipnsHostname = true
} }
parsedPath, err := coreapi.ParsePath(urlPath) parsedPath, err := i.api.ParsePath(ctx, urlPath)
if err != nil { if err != nil {
webError(w, "invalid ipfs path", err, http.StatusBadRequest) webError(w, "invalid ipfs path", err, http.StatusBadRequest)
return return
...@@ -288,7 +287,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr ...@@ -288,7 +287,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
return return
} }
dr, err := i.api.Unixfs().Cat(ctx, coreapi.ParseCid(ixnd.Cid())) dr, err := i.api.Unixfs().Cat(ctx, i.api.ParseCid(ixnd.Cid()))
if err != nil { if err != nil {
internalWebError(w, err) internalWebError(w, err)
return return
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论