@cryptotaxi247 / kubo / commits / 338e90e9c

coreapi: expand public path api

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Feb 2, 2018 at 16:00 UTC 338e90e9c8391828b362b84624811c764d14313b
11 files changed +87 -37
core/coreapi/block.go
+2 -2
@@ -65,7 +65,7 @@ func (api *BlockAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Bloc
65 return nil, err
66 }
67
68 - return ParseCid(b.Cid()), nil
68 + return api.ParseCid(b.Cid()), nil
69 }
70
71 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 }
118
119 return &BlockStat{
120 - path: ParseCid(b.Cid()),
120 + path: api.ParseCid(b.Cid()),
121 size: len(b.RawData()),
122 }, nil
123 }
core/coreapi/coreapi.go
+16 -4
@@ -18,6 +18,7 @@ import (
18
19 core "github.com/ipfs/go-ipfs/core"
20 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
21 + caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
22 namesys "github.com/ipfs/go-ipfs/namesys"
23 ipfspath "github.com/ipfs/go-ipfs/path"
24 resolver "github.com/ipfs/go-ipfs/path/resolver"
@@ -29,11 +30,12 @@ import (
30
31 type CoreAPI struct {
32 node *core.IpfsNode
33 + *caopts.ApiOptions
34 }
35
36 // NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
37 func NewCoreAPI(n *core.IpfsNode) coreiface.CoreAPI {
36 - api := &CoreAPI{n}
38 + api := &CoreAPI{n, nil}
39 return api
40 }
41
@@ -132,16 +134,26 @@ type path struct {
134 }
135
136 // ParsePath parses path `p` using ipfspath parser, returns the parsed path.
135 -func ParsePath(p string) (coreiface.Path, error) {
137 +func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) {
138 + options, err := caopts.ParsePathOptions(opts...)
139 + if err != nil {
140 + return nil, err
141 + }
142 +
143 pp, err := ipfspath.ParsePath(p)
144 if err != nil {
145 return nil, err
146 }
140 - return &path{path: pp}, nil
147 +
148 + res := &path{path: pp}
149 + if options.Resolve {
150 + return api.ResolvePath(ctx, res)
151 + }
152 + return res, nil
153 }
154
155 // ParseCid parses the path from `c`, returns the parsed path.
144 -func ParseCid(c *cid.Cid) coreiface.Path {
156 +func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path {
157 return &path{path: ipfspath.FromCid(c), cid: c, root: c}
158 }
159
core/coreapi/dag.go
+2 -2
@@ -44,7 +44,7 @@ func (api *DagAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.DagPut
44 return nil, err
45 }
46
47 - return ParseCid(nds[0].Cid()), nil
47 + return api.ParseCid(nds[0].Cid()), nil
48 }
49
50 // 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 paths := n.Tree("", settings.Depth)
67 out := make([]coreiface.Path, len(paths))
68 for n, p2 := range paths {
69 - out[n], err = ParsePath(gopath.Join(p.String(), p2))
69 + out[n], err = api.ParsePath(ctx, gopath.Join(p.String(), p2))
70 if err != nil {
71 return nil, err
72 }
core/coreapi/dag_test.go
+1 -3
@@ -6,8 +6,6 @@ import (
6 "strings"
7 "testing"
8
9 - coreapi "github.com/ipfs/go-ipfs/core/coreapi"
10 -
9 mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
10
11 opt "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -74,7 +72,7 @@ func TestPath(t *testing.T) {
72 t.Error(err)
73 }
74
77 - p, err := coreapi.ParsePath(path.Join(res.Cid().String(), "lnk"))
75 + p, err := api.ParsePath(ctx, path.Join(res.Cid().String(), "lnk"))
76 if err != nil {
77 t.Error(err)
78 }
core/coreapi/interface/coreapi.go
+13
@@ -5,6 +5,9 @@ package iface
5 import (
6 "context"
7
8 + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9 +
10 + cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
11 ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
12 )
13
@@ -37,4 +40,14 @@ type CoreAPI interface {
40 // ResolveNode resolves the path (if not resolved already) using Unixfs
41 // resolver, gets and returns the resolved Node
42 ResolveNode(context.Context, Path) (ipld.Node, error)
43 +
44 + // ParsePath parses string path to a Path
45 + ParsePath(context.Context, string, ...options.ParsePathOption) (Path, error)
46 +
47 + // WithResolve is an option for ParsePath which when set to true tells
48 + // ParsePath to also resolve the path
49 + WithResolve(bool) options.ParsePathOption
50 +
51 + // ParseCid creates new path from the provided CID
52 + ParseCid(*cid.Cid) Path
53 }
core/coreapi/interface/options/path.go new
+30
@@ -0,0 +1,30 @@
1 +package options
2 +
3 +type ParsePathSettings struct {
4 + Resolve bool
5 +}
6 +
7 +type ParsePathOption func(*ParsePathSettings) error
8 +
9 +func ParsePathOptions(opts ...ParsePathOption) (*ParsePathSettings, error) {
10 + options := &ParsePathSettings{
11 + Resolve: false,
12 + }
13 +
14 + for _, opt := range opts {
15 + err := opt(options)
16 + if err != nil {
17 + return nil, err
18 + }
19 + }
20 + return options, nil
21 +}
22 +
23 +type ApiOptions struct{}
24 +
25 +func (api *ApiOptions) WithResolve(r bool) ParsePathOption {
26 + return func(settings *ParsePathSettings) error {
27 + settings.Resolve = r
28 + return nil
29 + }
30 +}
core/coreapi/object.go
+4 -4
@@ -121,7 +121,7 @@ func (api *ObjectAPI) Put(ctx context.Context, src io.Reader, opts ...caopts.Obj
121 return nil, err
122 }
123
124 - return ParseCid(dagnode.Cid()), nil
124 + return api.ParseCid(dagnode.Cid()), nil
125 }
126
127 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 return nil, err
219 }
220
221 - return ParseCid(nnode.Cid()), nil
221 + return api.ParseCid(nnode.Cid()), nil
222 }
223
224 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 return nil, err
245 }
246
247 - return ParseCid(nnode.Cid()), nil
247 + return api.ParseCid(nnode.Cid()), nil
248 }
249
250 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 return nil, err
282 }
283
284 - return ParseCid(pbnd.Cid()), nil
284 + return api.ParseCid(pbnd.Cid()), nil
285 }
286
287 func (api *ObjectAPI) core() coreiface.CoreAPI {
core/coreapi/pin.go
+13 -15
@@ -9,11 +9,9 @@ import (
9 caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
10 corerepo "github.com/ipfs/go-ipfs/core/corerepo"
11 merkledag "github.com/ipfs/go-ipfs/merkledag"
12 - pin "github.com/ipfs/go-ipfs/pin"
12
13 offline "gx/ipfs/QmShbyKV9P7QuFecDHXsgrQ4rxxm71MUkGVpwedT4VQ8Bf/go-ipfs-exchange-offline"
14 cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
16 - ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
15 )
16
17 type PinAPI CoreAPI
@@ -46,7 +44,7 @@ func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]coreif
44 return nil, fmt.Errorf("invalid type '%s', must be one of {direct, indirect, recursive, all}", settings.Type)
45 }
46
49 - return pinLsAll(settings.Type, ctx, api.node.Pinning, api.node.DAG)
47 + return api.pinLsAll(settings.Type, ctx)
48 }
49
50 func (api *PinAPI) Rm(ctx context.Context, p coreiface.Path) error {
@@ -75,8 +73,8 @@ type pinStatus struct {
73
74 // BadNode is used in PinVerifyRes
75 type badNode struct {
78 - cid *cid.Cid
79 - err error
76 + path coreiface.Path
77 + err error
78 }
79
80 func (s *pinStatus) Ok() bool {
@@ -88,7 +86,7 @@ func (s *pinStatus) BadNodes() []coreiface.BadPinNode {
86 }
87
88 func (n *badNode) Path() coreiface.Path {
91 - return ParseCid(n.cid)
89 + return n.path
90 }
91
92 func (n *badNode) Err() error {
@@ -112,7 +110,7 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
110 links, err := getLinks(ctx, root)
111 if err != nil {
112 status := &pinStatus{ok: false, cid: root}
115 - status.badNodes = []coreiface.BadPinNode{&badNode{cid: root, err: err}}
113 + status.badNodes = []coreiface.BadPinNode{&badNode{path: api.ParseCid(root), err: err}}
114 visited[key] = status
115 return status
116 }
@@ -143,18 +141,18 @@ func (api *PinAPI) Verify(ctx context.Context) (<-chan coreiface.PinStatus, erro
141
142 type pinInfo struct {
143 pinType string
146 - object *cid.Cid
144 + object coreiface.Path
145 }
146
147 func (p *pinInfo) Path() coreiface.Path {
150 - return ParseCid(p.object)
148 + return p.object
149 }
150
151 func (p *pinInfo) Type() string {
152 return p.pinType
153 }
154
157 -func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.DAGService) ([]coreiface.Pin, error) {
155 +func (api *PinAPI) pinLsAll(typeStr string, ctx context.Context) ([]coreiface.Pin, error) {
156
157 keys := make(map[string]*pinInfo)
158
@@ -162,18 +160,18 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.
160 for _, c := range keyList {
161 keys[c.String()] = &pinInfo{
162 pinType: typeStr,
165 - object: c,
163 + object: api.ParseCid(c),
164 }
165 }
166 }
167
168 if typeStr == "direct" || typeStr == "all" {
171 - AddToResultKeys(pinning.DirectKeys(), "direct")
169 + AddToResultKeys(api.node.Pinning.DirectKeys(), "direct")
170 }
171 if typeStr == "indirect" || typeStr == "all" {
172 set := cid.NewSet()
175 - for _, k := range pinning.RecursiveKeys() {
176 - err := merkledag.EnumerateChildren(ctx, merkledag.GetLinksWithDAG(dag), k, set.Visit)
173 + for _, k := range api.node.Pinning.RecursiveKeys() {
174 + err := merkledag.EnumerateChildren(ctx, merkledag.GetLinksWithDAG(api.node.DAG), k, set.Visit)
175 if err != nil {
176 return nil, err
177 }
@@ -181,7 +179,7 @@ func pinLsAll(typeStr string, ctx context.Context, pinning pin.Pinner, dag ipld.
179 AddToResultKeys(set.Keys(), "indirect")
180 }
181 if typeStr == "recursive" || typeStr == "all" {
184 - AddToResultKeys(pinning.RecursiveKeys(), "recursive")
182 + AddToResultKeys(api.node.Pinning.RecursiveKeys(), "recursive")
183 }
184
185 out := make([]coreiface.Pin, 0, len(keys))
core/coreapi/unixfs.go
+1 -1
@@ -25,7 +25,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.Path, err
25 if err != nil {
26 return nil, err
27 }
28 - return ParseCid(c), nil
28 + return api.core().ParseCid(c), nil
29 }
30
31 // Cat returns the data contained by an IPFS or IPNS object(s) at path `p`.
core/coreapi/unixfs_test.go
+3 -3
@@ -206,7 +206,7 @@ func TestCatDir(t *testing.T) {
206 if err != nil {
207 t.Error(err)
208 }
209 - p := coreapi.ParseCid(edir.Cid())
209 + p := api.ParseCid(edir.Cid())
210
211 if p.String() != emptyDir.String() {
212 t.Fatalf("expected path %s, got: %s", emptyDir, p)
@@ -231,7 +231,7 @@ func TestCatNonUnixfs(t *testing.T) {
231 t.Error(err)
232 }
233
234 - _, err = api.Unixfs().Cat(ctx, coreapi.ParseCid(nd.Cid()))
234 + _, err = api.Unixfs().Cat(ctx, api.ParseCid(nd.Cid()))
235 if !strings.Contains(err.Error(), "proto: required field") {
236 t.Fatalf("expected protobuf error, got: %s", err)
237 }
@@ -327,7 +327,7 @@ func TestLsNonUnixfs(t *testing.T) {
327 t.Error(err)
328 }
329
330 - links, err := api.Unixfs().Ls(ctx, coreapi.ParseCid(nd.Cid()))
330 + links, err := api.Unixfs().Ls(ctx, api.ParseCid(nd.Cid()))
331 if err != nil {
332 t.Error(err)
333 }
core/corehttp/gateway_handler.go
+2 -3
@@ -13,7 +13,6 @@ import (
13 "time"
14
15 core "github.com/ipfs/go-ipfs/core"
16 - coreapi "github.com/ipfs/go-ipfs/core/coreapi"
16 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
17 "github.com/ipfs/go-ipfs/importer"
18 dag "github.com/ipfs/go-ipfs/merkledag"
@@ -160,7 +159,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
159 ipnsHostname = true
160 }
161
163 - parsedPath, err := coreapi.ParsePath(urlPath)
162 + parsedPath, err := i.api.ParsePath(ctx, urlPath)
163 if err != nil {
164 webError(w, "invalid ipfs path", err, http.StatusBadRequest)
165 return
@@ -288,7 +287,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
287 return
288 }
289
291 - dr, err := i.api.Unixfs().Cat(ctx, coreapi.ParseCid(ixnd.Cid()))
290 + dr, err := i.api.Unixfs().Cat(ctx, i.api.ParseCid(ixnd.Cid()))
291 if err != nil {
292 internalWebError(w, err)
293 return