/
pathresolver.go
75 lines (62 loc) · 2.26 KB
/
pathresolver.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
package core
import (
"context"
"errors"
"strings"
namesys "github.com/ipfs/go-ipfs/namesys"
ipld "gx/ipfs/QmR7TcHkR9nxkUorfi8XMTAMLUK7GiP64TWWBzY3aacc1o/go-ipld-format"
path "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path"
resolver "gx/ipfs/QmT3rzed1ppXefourpmoZ7tyVQfsGPQZ1pHDngLmCvXxd3/go-path/resolver"
logging "gx/ipfs/QmZChCsSt8DctjceaL56Eibc29CVQq4dGKRXC5JRZ6Ppae/go-log"
)
// ErrNoNamesys is an explicit error for when an IPFS node doesn't
// (yet) have a name system
var ErrNoNamesys = errors.New(
"core/resolve: no Namesys on IpfsNode - can't resolve ipns entry")
// ResolveIPNS resolves /ipns paths
func ResolveIPNS(ctx context.Context, nsys namesys.NameSystem, p path.Path) (path.Path, error) {
if strings.HasPrefix(p.String(), "/ipns/") {
evt := log.EventBegin(ctx, "resolveIpnsPath")
defer evt.Done()
// resolve ipns paths
// TODO(cryptix): we should be able to query the local cache for the path
if nsys == nil {
evt.Append(logging.LoggableMap{"error": ErrNoNamesys.Error()})
return "", ErrNoNamesys
}
seg := p.Segments()
if len(seg) < 2 || seg[1] == "" { // just "/<protocol/>" without further segments
evt.Append(logging.LoggableMap{"error": path.ErrNoComponents.Error()})
return "", path.ErrNoComponents
}
extensions := seg[2:]
resolvable, err := path.FromSegments("/", seg[0], seg[1])
if err != nil {
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
respath, err := nsys.Resolve(ctx, resolvable.String())
if err != nil {
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
segments := append(respath.Segments(), extensions...)
p, err = path.FromSegments("/", segments...)
if err != nil {
evt.Append(logging.LoggableMap{"error": err.Error()})
return "", err
}
}
return p, nil
}
// Resolve resolves the given path by parsing out protocol-specific
// entries (e.g. /ipns/<node-key>) and then going through the /ipfs/
// entries and returning the final node.
func Resolve(ctx context.Context, nsys namesys.NameSystem, r *resolver.Resolver, p path.Path) (ipld.Node, error) {
p, err := ResolveIPNS(ctx, nsys, p)
if err != nil {
return nil, err
}
// ok, we have an IPFS path now (or what we'll treat as one)
return r.ResolvePath(ctx, p)
}