-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathcoreapi.go
106 lines (85 loc) · 2.36 KB
/
coreapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package coreapi
import (
"context"
core "github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
ipfspath "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"
cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
)
type CoreAPI struct {
node *core.IpfsNode
}
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node
func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
api := &CoreAPI{n}
return api
}
func (api *CoreAPI) Unixfs() coreiface.UnixfsAPI {
return (*UnixfsAPI)(api)
}
func (api *CoreAPI) Dag() coreiface.DagAPI {
return &DagAPI{api, nil}
}
func (api *CoreAPI) Name() coreiface.NameAPI {
return &NameAPI{api, nil}
}
func (api *CoreAPI) Key() coreiface.KeyAPI {
return &KeyAPI{api, nil}
}
func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) {
p, err := api.ResolvePath(ctx, p)
if err != nil {
return nil, err
}
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
}
r := &ipfspath.Resolver{
DAG: api.node.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}
p2 := ipfspath.FromString(p.String())
node, err := core.Resolve(ctx, api.node.Namesys, r, p2)
if err == core.ErrNoNamesys {
return nil, coreiface.ErrOffline
} else if err != nil {
return nil, err
}
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
}
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 }