|
1 | 1 | package coreapi |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - |
6 | 4 | core "github.com/ipfs/go-ipfs/core" |
7 | 5 | coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" |
8 | 6 | caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options" |
9 | | - namesys "github.com/ipfs/go-ipfs/namesys" |
10 | | - ipfspath "github.com/ipfs/go-ipfs/path" |
11 | | - uio "github.com/ipfs/go-ipfs/unixfs/io" |
12 | | - |
13 | | - cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid" |
14 | | - ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format" |
15 | 7 | ) |
16 | 8 |
|
17 | 9 | type CoreAPI struct { |
@@ -57,96 +49,3 @@ func (api *CoreAPI) Object() coreiface.ObjectAPI { |
57 | 49 | func (api *CoreAPI) Pin() coreiface.PinAPI { |
58 | 50 | return &PinAPI{api, nil} |
59 | 51 | } |
60 | | - |
61 | | -// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the |
62 | | -// resolved Node. |
63 | | -func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (coreiface.Node, error) { |
64 | | - return resolveNode(ctx, api.node.DAG, api.node.Namesys, p) |
65 | | -} |
66 | | - |
67 | | -func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Node, error) { |
68 | | - p, err := resolvePath(ctx, ng, nsys, p) |
69 | | - if err != nil { |
70 | | - return nil, err |
71 | | - } |
72 | | - |
73 | | - node, err := ng.Get(ctx, p.Cid()) |
74 | | - if err != nil { |
75 | | - return nil, err |
76 | | - } |
77 | | - return node, nil |
78 | | -} |
79 | | - |
80 | | -// ResolvePath resolves the path `p` using Unixfs resolver, returns the |
81 | | -// resolved path. |
82 | | -// TODO: store all of ipfspath.Resolver.ResolvePathComponents() in Path |
83 | | -func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.Path, error) { |
84 | | - return resolvePath(ctx, api.node.DAG, api.node.Namesys, p) |
85 | | -} |
86 | | - |
87 | | -func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.Path, error) { |
88 | | - if p.Resolved() { |
89 | | - return p, nil |
90 | | - } |
91 | | - |
92 | | - r := &ipfspath.Resolver{ |
93 | | - DAG: ng, |
94 | | - ResolveOnce: uio.ResolveUnixfsOnce, |
95 | | - } |
96 | | - |
97 | | - p2 := ipfspath.FromString(p.String()) |
98 | | - node, err := core.Resolve(ctx, nsys, r, p2) |
99 | | - if err == core.ErrNoNamesys { |
100 | | - return nil, coreiface.ErrOffline |
101 | | - } else if err != nil { |
102 | | - return nil, err |
103 | | - } |
104 | | - |
105 | | - var root *cid.Cid |
106 | | - if p2.IsJustAKey() { |
107 | | - root = node.Cid() |
108 | | - } |
109 | | - |
110 | | - return ResolvedPath(p.String(), node.Cid(), root), nil |
111 | | -} |
112 | | - |
113 | | -// Implements coreiface.Path |
114 | | -type path struct { |
115 | | - path ipfspath.Path |
116 | | - cid *cid.Cid |
117 | | - root *cid.Cid |
118 | | -} |
119 | | - |
120 | | -// ParsePath parses path `p` using ipfspath parser, returns the parsed path. |
121 | | -func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) { |
122 | | - options, err := caopts.ParsePathOptions(opts...) |
123 | | - if err != nil { |
124 | | - return nil, err |
125 | | - } |
126 | | - |
127 | | - pp, err := ipfspath.ParsePath(p) |
128 | | - if err != nil { |
129 | | - return nil, err |
130 | | - } |
131 | | - |
132 | | - res := &path{path: pp} |
133 | | - if options.Resolve { |
134 | | - return api.ResolvePath(ctx, res) |
135 | | - } |
136 | | - return res, nil |
137 | | -} |
138 | | - |
139 | | -// ParseCid parses the path from `c`, retruns the parsed path. |
140 | | -func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path { |
141 | | - return &path{path: ipfspath.FromCid(c), cid: c, root: c} |
142 | | -} |
143 | | - |
144 | | -// ResolvePath parses path from string `p`, returns parsed path. |
145 | | -func ResolvedPath(p string, c *cid.Cid, r *cid.Cid) coreiface.Path { |
146 | | - return &path{path: ipfspath.FromString(p), cid: c, root: r} |
147 | | -} |
148 | | - |
149 | | -func (p *path) String() string { return p.path.String() } |
150 | | -func (p *path) Cid() *cid.Cid { return p.cid } |
151 | | -func (p *path) Root() *cid.Cid { return p.root } |
152 | | -func (p *path) Resolved() bool { return p.cid != nil } |
0 commit comments