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 package cmdenv
import ( import (
"errors" "strings"
path "gx/ipfs/QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8/go-path"
cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds" cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds"
cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc" cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
...@@ -64,10 +63,7 @@ func CidBaseDefined(req *cmds.Request) bool { ...@@ -64,10 +63,7 @@ func CidBaseDefined(req *cmds.Request) bool {
// the base encoder is returned. If you don't care about the error // the base encoder is returned. If you don't care about the error
// condition, it is safe to ignore the error returned. // condition, it is safe to ignore the error returned.
func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) { func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
v, err := extractCidString(p) v := extractCidString(p)
if err != nil {
return enc, err
}
if cidVer(v) == 0 { if cidVer(v) == 0 {
return cidenc.Encoder{Base: enc.Base, Upgrade: false}, nil return cidenc.Encoder{Base: enc.Base, Upgrade: false}, nil
} }
...@@ -78,16 +74,12 @@ func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) { ...@@ -78,16 +74,12 @@ func CidEncoderFromPath(enc cidenc.Encoder, p string) (cidenc.Encoder, error) {
return cidenc.Encoder{Base: e, Upgrade: true}, nil return cidenc.Encoder{Base: e, Upgrade: true}, nil
} }
func extractCidString(str string) (string, error) { func extractCidString(str string) string {
p, err := path.ParsePath(str) parts := strings.Split(str, "/")
if err != nil { if len(parts) > 2 && (parts[1] == "ipfs" || parts[1] == "ipld") {
return "", err return parts[2]
}
segs := p.Segments()
if segs[0] == "ipfs" || segs[0] == "ipld" {
return segs[1], nil
} }
return "", errors.New("no CID found") return str
} }
func cidVer(v string) int { func cidVer(v string) int {
......
...@@ -6,26 +6,25 @@ import ( ...@@ -6,26 +6,25 @@ import (
func TestExtractCidString(t *testing.T) { func TestExtractCidString(t *testing.T) {
test := func(path string, cid string) { test := func(path string, cid string) {
res, err := extractCidString(path) res := extractCidString(path)
if err != nil || res != cid { if res != cid {
t.Errorf("extractCidString(%s) failed", path) t.Errorf("extractCidString(%s) failed: expected '%s' but got '%s'", path, cid, res)
}
}
testFailure := func(path string) {
_, err := extractCidString(path)
if err == nil {
t.Errorf("extractCidString(%s) should of failed", path)
} }
} }
p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu" p := "QmRqVG8VGdKZ7KARqR96MV7VNHgWvEQifk94br5HpURpfu"
test(p, p) test(p, p)
test("/ipfs/"+p, p) test("/ipfs/"+p, p)
testFailure("/ipns/" + p)
p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk" p = "zb2rhfkM4FjkMLaUnygwhuqkETzbYXnUDf1P9MSmdNjW1w1Lk"
test(p, p) test(p, p)
test("/ipfs/"+p, p) test("/ipfs/"+p, p)
test("/ipld/"+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