-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_cids.go
70 lines (60 loc) · 2.15 KB
/
find_cids.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dls
import (
"fmt"
"regexp"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
)
var logger = logging.Logger("dls")
var baseCharsets = map[string][]string{
"base2": {"0", "01"},
"base8": {"7", "01234567"},
"base10": {"9", "0123456789"},
"base16": {"f", "0123456789abcdef"},
"base32hex": {"v", "0123456789abcdefghijklmnopqrstuv"},
"base32": {"b", "abcdefghijklmnopqrstuvwxyz234567"},
"base32z": {"h", "ybndrfg8ejkmcpqxot1uwisza345h769"},
"base36": {"k", "0123456789abcdefghijklmnopqrstuvwxyz"},
"base36upper": {"K", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
"base58flickr": {"Z", "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"},
"base58btc": {"z", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"},
"cidv0": {"Qm", "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"},
"base64": {"m", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"},
"base64url": {"u", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"},
}
var cidRegexps = compileRegexps()
func compileRegexps() []*regexp.Regexp {
var exps []*regexp.Regexp
for base, charset := range baseCharsets {
l := "6," // find multibase strings of at least 6 chars
if base == "cidv0" {
l = "44" // cidv0s have 44 char length, fixed.
}
regexpStr := fmt.Sprintf("%s[%s]{%s}", charset[0], charset[1], l)
exps = append(exps, regexp.MustCompile(regexpStr))
}
return exps
}
// FindCIDs extracts CIDs from text. Returns a deduplicated list of CID
// strings as they are found in the text. The list is deduplicated based on
// their multihash, with later appereances of a CID triumphing over previous
// ones containing the same multihash.
func FindCIDs(s string) []string {
cids := make(map[string]string)
for _, exp := range cidRegexps {
cidStrs := exp.FindAllString(s, -1)
for _, cStr := range cidStrs {
c, err := cid.Decode(cStr)
if err != nil {
logger.Warnf("could not decode %s as cid", cStr)
continue
}
cids[string(c.Hash())] = cStr
}
}
var results []string
for _, v := range cids {
results = append(results, v)
}
return results
}