|
| 1 | +package cmdenv |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" |
| 8 | + cmds "gx/ipfs/QmWGm4AbZEbnmdgVTza52MSNpEmBdFVqzmAysRbjrRyGbH/go-ipfs-cmds" |
| 9 | + cidenc "gx/ipfs/QmdPQx9fvN5ExVwMhRmh7YpCQJzJrFhd1AjVBwJmRMFJeX/go-cidutil/cidenc" |
| 10 | + cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" |
| 11 | + mbase "gx/ipfs/QmekxXDhCxCJRNuzmHreuaT3BsuJcsjcXWNrtV9C8DRHtd/go-multibase" |
| 12 | +) |
| 13 | + |
| 14 | +var OptionCidBase = cmdkit.StringOption("cid-base", "Multibase encoding used for version 1 CIDs in output.") |
| 15 | +var OptionUpgradeCidV0InOutput = cmdkit.BoolOption("upgrade-cidv0-in-output", "Upgrade version 0 to version 1 CIDs in output.") |
| 16 | + |
| 17 | +// GetCidEncoder processes the `cid-base` and `output-cidv1` options and |
| 18 | +// returns a encoder to use based on those parameters. |
| 19 | +func GetCidEncoder(req *cmds.Request) (cidenc.Encoder, error) { |
| 20 | + return getCidBase(req, true) |
| 21 | +} |
| 22 | + |
| 23 | +// GetLowLevelCidEncoder is like GetCidEncoder but meant to be used by |
| 24 | +// lower level commands. It differs from GetCidEncoder in that CIDv0 |
| 25 | +// are not, by default, auto-upgraded to CIDv1. |
| 26 | +func GetLowLevelCidEncoder(req *cmds.Request) (cidenc.Encoder, error) { |
| 27 | + return getCidBase(req, false) |
| 28 | +} |
| 29 | + |
| 30 | +func getCidBase(req *cmds.Request, autoUpgrade bool) (cidenc.Encoder, error) { |
| 31 | + base, _ := req.Options[OptionCidBase.Name()].(string) |
| 32 | + upgrade, upgradeDefined := req.Options[OptionUpgradeCidV0InOutput.Name()].(bool) |
| 33 | + |
| 34 | + e := cidenc.Default() |
| 35 | + |
| 36 | + if base != "" { |
| 37 | + var err error |
| 38 | + e.Base, err = mbase.EncoderByName(base) |
| 39 | + if err != nil { |
| 40 | + return e, err |
| 41 | + } |
| 42 | + if autoUpgrade { |
| 43 | + e.Upgrade = true |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if upgradeDefined { |
| 48 | + e.Upgrade = upgrade |
| 49 | + } |
| 50 | + |
| 51 | + return e, nil |
| 52 | +} |
| 53 | + |
| 54 | +// CidBaseDefined returns true if the `cid-base` option is specified |
| 55 | +// on the command line |
| 56 | +func CidBaseDefined(req *cmds.Request) bool { |
| 57 | + base, _ := req.Options["cid-base"].(string) |
| 58 | + return base != "" |
| 59 | +} |
| 60 | + |
| 61 | +// CidEncoderFromPath creates a new encoder that is influenced from |
| 62 | +// the encoded Cid in a Path. For CidV0 the multibase from the base |
| 63 | +// encoder is used and automatic upgrades are disabled. For CidV1 the |
| 64 | +// multibase from the CID is used and upgrades are enabled. |
| 65 | +// |
| 66 | +// This logic is intentionally fuzzy and will match anything of the form |
| 67 | +// `CidLike`, `CidLike/...`, or `/namespace/CidLike/...`. |
| 68 | +// |
| 69 | +// For example: |
| 70 | +// |
| 71 | +// * Qm... |
| 72 | +// * Qm.../... |
| 73 | +// * /ipfs/Qm... |
| 74 | +// * /ipns/bafybeiahnxfi7fpmr5wtxs2imx4abnyn7fdxeiox7xxjem6zuiioqkh6zi/... |
| 75 | +// * /bzz/bafybeiahnxfi7fpmr5wtxs2imx4abnyn7fdxeiox7xxjem6zuiioqkh6zi/... |
| 76 | +func CidEncoderFromPath(p string) (cidenc.Encoder, error) { |
| 77 | + components := strings.SplitN(p, "/", 4) |
| 78 | + |
| 79 | + var maybeCid string |
| 80 | + if components[0] != "" { |
| 81 | + // No leading slash, first component is likely CID-like. |
| 82 | + maybeCid = components[0] |
| 83 | + } else if len(components) < 3 { |
| 84 | + // Not enough components to include a CID. |
| 85 | + return cidenc.Encoder{}, fmt.Errorf("no cid in path: %s", p) |
| 86 | + } else { |
| 87 | + maybeCid = components[2] |
| 88 | + } |
| 89 | + c, err := cid.Decode(maybeCid) |
| 90 | + if err != nil { |
| 91 | + // Ok, not a CID-like thing. Keep the current encoder. |
| 92 | + return cidenc.Encoder{}, fmt.Errorf("no cid in path: %s", p) |
| 93 | + } |
| 94 | + if c.Version() == 0 { |
| 95 | + // Version 0, use the base58 non-upgrading encoder. |
| 96 | + return cidenc.Default(), nil |
| 97 | + } |
| 98 | + |
| 99 | + // Version 1+, extract multibase encoding. |
| 100 | + encoding, _, err := mbase.Decode(maybeCid) |
| 101 | + if err != nil { |
| 102 | + // This should be impossible, we've already decoded the cid. |
| 103 | + panic(fmt.Sprintf("BUG: failed to get multibase decoder for CID %s", maybeCid)) |
| 104 | + } |
| 105 | + |
| 106 | + return cidenc.Encoder{Base: mbase.MustNewEncoder(encoding), Upgrade: true}, nil |
| 107 | +} |
0 commit comments