Don't use ParsePath in extractCidString.
ParsePath does not preserve the multibase. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Jan 17, 2019 at 15:42 UTC
8209ba6156fcbe3cd33549a93b419b713a224029
2 files changed
+17
-26
core/commands/cmdenv/cidbase.go
+7
-15
@@ -1,9 +1,8 @@
1
package cmdenv
2
3
import (
4
- "errors"
4
+ "strings"
5
6
- path "gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
6
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
7
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
8
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
@@ -64,10 +63,7 @@ func CidBaseDefined(req *cmds.Request) bool {
63
// the base encoder is returned. If you don't care about the error
64
// condition, it is safe to ignore the error returned.
65
func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
67
- v, err := extractCidString(p)
68
- if err != nil {
69
- return enc, err
70
- }
66
+ v := extractCidString(p)
67
if cidVer(v) == 0 {
68
return cidenc.Encoder{Base: enc.Base, Upgrade: false}, nil
69
}
@@ -78,16 +74,12 @@ func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
74
return cidenc.Encoder{Base: e, Upgrade: true}, nil
75
}
76
81
-func extractCidString(str string) (string, error) {
82
- p, err := path.ParsePath(str)
83
- if err != nil {
84
- return "", err
85
- }
86
- segs := p.Segments()
87
- if segs[0] == "ipfs" || segs[0] == "ipld" {
88
- return segs[1], nil
77
+func extractCidString(str string) string {
78
+ parts := strings.Split(str, "/")
79
+ if len(parts) > 2 && (parts[1] == "ipfs" || parts[1] == "ipld") {
80
+ return parts[2]
81
}
90
- return "", errors.New("no CID found")
82
+ return str
83
}
84
85
func cidVer(v string) int {
core/commands/cmdenv/cidbase_test.go
+10
-11
@@ -6,26 +6,25 @@ import (
6
7
func TestExtractCidString(t *testing.T) {
8
test := func(path string, cid string) {
9
- res, err := extractCidString(path)
10
- if err != nil || res != cid {
11
- t.Errorf("extractCidString(%s) failed", path)
12
- }
13
- }
14
- testFailure := func(path string) {
15
- _, err := extractCidString(path)
16
- if err == nil {
17
- t.Errorf("extractCidString(%s) should of failed", path)
9
+ res := extractCidString(path)
10
+ if res != cid {
11
+ t.Errorf("extractCidString(%s) failed: expected '%s' but got '%s'", path, cid, res)
12
}
13
}
14
p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
15
test(p, p)
16
test("/ipfs/"+p, p)
23
- testFailure("/ipns/" + p)
17
18
p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
19
test(p, p)
20
test("/ipfs/"+p, p)
21
test("/ipld/"+p, p)
22
30
- testFailure("/ipfs")
23
+ p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm"
24
+ test(p, p)
25
+ test("/ipfs/"+p, p)
26
+ test("/ipld/"+p, p)
27
+
28
+ // an error is also acceptable in future versions of extractCidString
29
+ test("/ipfs", "/ipfs")
30
}