From 8209ba6156fcbe3cd33549a93b419b713a224029 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 17 Jan 2019 15:42:09 -0500 Subject: [PATCH] Don't use ParsePath in extractCidString. ParsePath does not preserve the multibase. License: MIT Signed-off-by: Kevin Atkinson --- core/commands/cmdenv/cidbase.go | 22 +++++++--------------- core/commands/cmdenv/cidbase_test.go | 21 ++++++++++----------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/core/commands/cmdenv/cidbase.go b/core/commands/cmdenv/cidbase.go index 29926ed08..edb7d5532 100644 --- a/core/commands/cmdenv/cidbase.go +++ b/core/commands/cmdenv/cidbase.go @@ -1,9 +1,8 @@ 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 +func extractCidString(str string) string { + parts := strings.Split(str, "/") + if len(parts) > 2 && (parts[1] == "ipfs" || parts[1] == "ipld") { + 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 { diff --git a/core/commands/cmdenv/cidbase_test.go b/core/commands/cmdenv/cidbase_test.go index bda6e0abc..889fa6a96 100644 --- a/core/commands/cmdenv/cidbase_test.go +++ b/core/commands/cmdenv/cidbase_test.go @@ -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") }