-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
73 lines (62 loc) · 1.6 KB
/
functions.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
71
72
73
package ipfs
import (
"fmt"
"math/rand"
"net/url"
"regexp"
"strings"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"
)
// Hash - separate IPFS hash from link
func Hash(link string) (string, error) {
hash := FindAllLinks([]byte(link))
if len(hash) != 1 {
return "", errors.Errorf("invalid IPFS link: %s", link)
}
_, err := cid.Decode(hash[0])
return hash[0], err
}
// Link - get gateway link
func Link(gateway, hash string) string {
return fmt.Sprintf("%s/ipfs/%s", gateway, hash)
}
// Path - get path without protocol
func Path(link string) string {
return strings.TrimPrefix(link, "ipfs://")
}
var ipfsURL = regexp.MustCompile(`ipfs:\/\/(?P<hash>(baf[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{56})|Qm[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{44})`)
// FindAllLinks -
func FindAllLinks(data []byte) []string {
matches := ipfsURL.FindAllSubmatch(data, -1)
if len(matches) == 0 {
return nil
}
res := make([]string, 0)
for i := range matches {
if len(matches[i]) != 2 {
continue
}
res = append(res, string(matches[i][1]))
}
return res
}
// ShuffleGateways - shuffle gateways for different request order for different files
func ShuffleGateways(gateways []string) []string {
if len(gateways) < 2 {
return gateways
}
shuffled := make([]string, len(gateways))
copy(shuffled, gateways)
rand.Shuffle(len(shuffled), func(i, j int) { shuffled[i], shuffled[j] = shuffled[j], shuffled[i] })
return shuffled
}
// Is -
func Is(link string) bool {
u, err := url.Parse(link)
if err != nil {
return false
}
_, err = cid.Decode(u.Host)
return err == nil
}