coreapi.go 3 KB
Newer Older
1 2 3 4 5 6 7
package coreapi

import (
	"context"

	core "github.com/ipfs/go-ipfs/core"
	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
8
	ipfspath "github.com/ipfs/go-ipfs/path"
Jeromy's avatar
Jeromy committed
9
	uio "github.com/ipfs/go-ipfs/unixfs/io"
10

Steven Allen's avatar
Steven Allen committed
11
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
12 13
)

14 15 16 17
type CoreAPI struct {
	node *core.IpfsNode
}

ForrestWeston's avatar
ForrestWeston committed
18
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
19 20 21 22 23
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
	api := &CoreAPI{n}
	return api
}

ForrestWeston's avatar
ForrestWeston committed
24
// Unixfs returns the UnixfsAPI interface backed by the go-ipfs node
25 26 27 28
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
	return (*UnixfsAPI)(api)
}

ForrestWeston's avatar
ForrestWeston committed
29
// Dag returns the DagAPI interface backed by the go-ipfs node
Łukasz Magiera's avatar
Łukasz Magiera committed
30
func (api *CoreAPI) Dag() coreiface.DagAPI {
31
	return &DagAPI{api, nil}
Łukasz Magiera's avatar
Łukasz Magiera committed
32 33
}

ForrestWeston's avatar
ForrestWeston committed
34
// Name returns the NameAPI interface backed by the go-ipfs node
Łukasz Magiera's avatar
Łukasz Magiera committed
35
func (api *CoreAPI) Name() coreiface.NameAPI {
36
	return &NameAPI{api, nil}
Łukasz Magiera's avatar
Łukasz Magiera committed
37 38
}

ForrestWeston's avatar
ForrestWeston committed
39
// Key returns the KeyAPI interface backed by the go-ipfs node
40
func (api *CoreAPI) Key() coreiface.KeyAPI {
41
	return &KeyAPI{api, nil}
42 43
}

ForrestWeston's avatar
ForrestWeston committed
44 45
// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
// resolved Node.
46 47
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
	p, err := api.ResolvePath(ctx, p)
48 49 50 51
	if err != nil {
		return nil, err
	}

52 53 54 55 56 57 58
	node, err := api.node.DAG.Get(ctx, p.Cid())
	if err != nil {
		return nil, err
	}
	return node, nil
}

ForrestWeston's avatar
ForrestWeston committed
59 60
// ResolvePath resolves the path `p` using Unixfs resolver, returns the
// resolved path.
61 62 63 64 65 66
// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path
func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) {
	if p.Resolved() {
		return p, nil
	}

Jeromy's avatar
Jeromy committed
67 68 69 70 71
	r := &ipfspath.Resolver{
		DAG:         api.node.DAG,
		ResolveOnce: uio.ResolveUnixfsOnce,
	}

72
	p2 := ipfspath.FromString(p.String())
Jeromy's avatar
Jeromy committed
73
	node, err := core.Resolve(ctx, api.node.Namesys, r, p2)
74 75 76 77 78
	if err == core.ErrNoNamesys {
		return nil, coreiface.ErrOffline
	} else if err != nil {
		return nil, err
	}
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	var root *cid.Cid
	if p2.IsJustAKey() {
		root = node.Cid()
	}

	return ResolvedPath(p.String(), node.Cid(), root), nil
}

// Implements coreiface.Path
type path struct {
	path ipfspath.Path
	cid  *cid.Cid
	root *cid.Cid
}

ForrestWeston's avatar
ForrestWeston committed
95
// ParsePath parses path `p` using ipfspath parser, returns the parsed path.
96 97 98 99 100 101
func ParsePath(p string) (coreiface.Path, error) {
	pp, err := ipfspath.ParsePath(p)
	if err != nil {
		return nil, err
	}
	return &path{path: pp}, nil
102
}
103

ForrestWeston's avatar
ForrestWeston committed
104
// ParseCid parses the path from `c`, retruns the parsed path.
105 106 107 108
func ParseCid(c *cid.Cid) coreiface.Path {
	return &path{path: ipfspath.FromCid(c), cid: c, root: c}
}

ForrestWeston's avatar
ForrestWeston committed
109
// ResolvePath parses path from string `p`, returns parsed path.
110 111 112 113 114 115 116 117
func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path {
	return &path{path: ipfspath.FromString(p), cid: c, root: r}
}

func (p *path) String() string { return p.path.String() }
func (p *path) Cid() *cid.Cid  { return p.cid }
func (p *path) Root() *cid.Cid { return p.root }
func (p *path) Resolved() bool { return p.cid != nil }