coreapi.go 2.38 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
}

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

func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
	return (*UnixfsAPI)(api)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
28
func (api *CoreAPI) Dag() coreiface.DagAPI {
29
	return &DagAPI{api, nil}
Łukasz Magiera's avatar
Łukasz Magiera committed
30 31
}

Łukasz Magiera's avatar
Łukasz Magiera committed
32
func (api *CoreAPI) Name() coreiface.NameAPI {
33 34 35 36
	return &NameAPI{
		api,
		nil,
	}
Łukasz Magiera's avatar
Łukasz Magiera committed
37 38
}

39
func (api *CoreAPI) Key() coreiface.KeyAPI {
40 41 42 43
	return &KeyAPI{
		api,
		nil,
	}
44 45
}

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 59 60 61 62 63 64
	node, err := api.node.DAG.Get(ctx, p.Cid())
	if err != nil {
		return nil, err
	}
	return node, nil
}

// 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
65 66 67 68 69
	r := &ipfspath.Resolver{
		DAG:         api.node.DAG,
		ResolveOnce: uio.ResolveUnixfsOnce,
	}

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

	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
}

func ParsePath(p string) (coreiface.Path, error) {
	pp, err := ipfspath.ParsePath(p)
	if err != nil {
		return nil, err
	}
	return &path{path: pp}, nil
99
}
100 101 102 103 104 105 106 107 108 109 110 111 112

func ParseCid(c *cid.Cid) coreiface.Path {
	return &path{path: ipfspath.FromCid(c), cid: c, root: c}
}

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 }