Commit 8209ba61 authored by Kevin Atkinson's avatar Kevin Atkinson

Don't use ParsePath in extractCidString.

ParsePath does not preserve the multibase.

License: MIT
Signed-off-by: default avatarKevin Atkinson <k@kevina.org>
parent b22275fb
package cmdenv
import (
"errors"
"strings"
path "gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
......@@ -64,10 +63,7 @@ func CidBaseDefined(req *cmds.Request) bool {
// the base encoder is returned. If you don't care about the error
// condition, it is safe to ignore the error returned.
func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
v, err := extractCidString(p)
if err != nil {
return enc, err
}
v := extractCidString(p)
if cidVer(v) == 0 {
return cidenc.Encoder{Base: enc.Base, Upgrade: false}, nil
}
......@@ -78,16 +74,12 @@ func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
return cidenc.Encoder{Base: e, Upgrade: true}, nil
}
func extractCidString(str string) (string, error) {
p, err := path.ParsePath(str)
if err != nil {
return "", err
}
segs := p.Segments()
if segs[0] == "ipfs" || segs[0] == "ipld" {
return segs[1], nil
func extractCidString(str string) string {
parts := strings.Split(str, "/")
if len(parts) > 2 && (parts[1] == "ipfs" || parts[1] == "ipld") {
return parts[2]
}
return "", errors.New("no CID found")
return str
}
func cidVer(v string) int {
......
......@@ -6,26 +6,25 @@ import (
func TestExtractCidString(t *testing.T) {
test := func(path string, cid string) {
res, err := extractCidString(path)
if err != nil || res != cid {
t.Errorf("extractCidString(%s) failed", path)
}
}
testFailure := func(path string) {
_, err := extractCidString(path)
if err == nil {
t.Errorf("extractCidString(%s) should of failed", path)
res := extractCidString(path)
if res != cid {
t.Errorf("extractCidString(%s) failed: expected '%s' but got '%s'", path, cid, res)
}
}
p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
test(p, p)
test("/ipfs/"+p, p)
testFailure("/ipns/" + p)
p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
test(p, p)
test("/ipfs/"+p, p)
test("/ipld/"+p, p)
testFailure("/ipfs")
p = "bafyreifrcnyjokuw4i4ggkzg534tjlc25lqgt3ttznflmyv5fftdgu52hm"
test(p, p)
test("/ipfs/"+p, p)
test("/ipld/"+p, p)
// an error is also acceptable in future versions of extractCidString
test("/ipfs", "/ipfs")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment