Skip to content

Commit 87688df

Browse files
committedJan 17, 2019
Don't use ParsePath in extractCidString.
ParsePath does not preserve the multibase. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
1 parent ccca905 commit 87688df

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed
 

‎core/commands/cmdenv/cidbase.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package cmdenv
22

33
import (
4-
"errors"
4+
"strings"
55

6-
path "gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
76
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
87
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
98
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
@@ -64,10 +63,7 @@ func CidBaseDefined(req *cmds.Request) bool {
6463
// the base encoder is returned. If you don't care about the error
6564
// condition, it is safe to ignore the error returned.
6665
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)
7167
if cidVer(v) == 0 {
7268
return cidenc.Encoder{Base: enc.Base, Upgrade: false}, nil
7369
}
@@ -78,16 +74,12 @@ func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
7874
return cidenc.Encoder{Base: e, Upgrade: true}, nil
7975
}
8076

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]
8981
}
90-
return "", errors.New("no CID found")
82+
return str
9183
}
9284

9385
func cidVer(v string) int {

‎core/commands/cmdenv/cidbase_test.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ import (
66

77
func TestExtractCidString(t *testing.T) {
88
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)
1812
}
1913
}
2014
p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
2115
test(p, p)
2216
test("/ipfs/"+p, p)
23-
testFailure("/ipns/" + p)
2417

2518
p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
2619
test(p, p)
2720
test("/ipfs/"+p, p)
2821
test("/ipld/"+p, p)
2922

30-
testFailure("/ipfs")
23+
p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm"
24+
test(p, p)
25+
test("/ipfs/"+p, p)
26+
test("/ipld/"+p, p)
3127
}

0 commit comments

Comments
 (0)