-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
248 lines (204 loc) · 4.37 KB
/
utils.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"crypto/sha1"
"encoding/base64"
"math"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/net/idna"
"golang.org/x/net/publicsuffix"
"golang.org/x/text/language"
)
const (
checksumEmpty = "000000000000000000000000"
)
var sanitizeTitleRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
var sanitizeMoreSpaceRegex = regexp.MustCompile(`[\s]+`)
var sanitizeNameRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{1,31}$`)
var entityIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_\/]{1,63}$`)
var checksumReplaceRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
var cursorTimeLayout = "20060102030405.000"
var stripTagger = bluemonday.StripTagsPolicy()
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}
func intMinMax(v int, min int, max int) int {
if v < min {
return min
} else if v > max {
return max
}
return v
}
func uint16FromString(s string) uint16 {
i, err := strconv.Atoi(s)
if err != nil {
return 0
}
return uint16(i)
}
func boolUint8(v bool) uint8 {
if v {
return 1
}
return 0
}
func getURLString(u *url.URL) string {
if u == nil {
return ""
}
return u.String()
}
func isValidURL(urlString string) bool {
if urlString == "" {
return false
}
_, err := url.Parse(urlString)
return err == nil
}
func getURLPath(u *url.URL) string {
if u == nil {
return ""
}
r, e := regexp.Compile(regexp.QuoteMeta(u.Host) + `(.*)$`)
if e != nil {
return ""
}
if r.MatchString(u.String()) {
matched := r.FindStringSubmatch(u.String())
return matched[1]
}
return ""
}
func getURL(urlString string) *url.URL {
if urlString == "" {
return nil
}
u, err := url.Parse(urlString)
if err == nil {
return u
}
return nil
}
func sanitizeURL(urlString string) string {
if urlString == "" {
return ""
}
u, err := url.Parse(urlString)
if err == nil {
return u.String()
}
return ""
}
func checksum(str string) string {
if strings.TrimSpace(str) == "" {
return checksumEmpty
}
h := sha1.New()
h.Write([]byte(str))
return checksumReplaceRegex.ReplaceAllString(base64.StdEncoding.EncodeToString(h.Sum(nil)), "0")[0:24]
}
func sanitizeTitle(str string) string {
s := sanitizeTitleRegex.ReplaceAllString(str, " ")
s = sanitizeMoreSpaceRegex.ReplaceAllString(s, " ")
return strings.ToLower(strings.TrimSpace(s))
}
func normalizeHostname(hostname string) string {
_, isICN := publicsuffix.PublicSuffix(hostname)
if isICN {
p := idna.New(idna.ValidateForRegistration())
encodedHostname, err := p.ToUnicode(hostname)
if err == nil {
return encodedHostname
}
}
return hostname
}
func getSecondDomainLevel(u *url.URL) string {
hostname := u.Hostname()
ip := net.ParseIP(hostname)
if ip != nil {
return ip.String()
}
hostname = normalizeHostname(hostname)
parts := strings.Split(hostname, ".")
if len(parts) >= 2 {
tld := parts[len(parts)-1]
domain := parts[len(parts)-2]
return domain + "." + tld
}
return hostname
}
func getDomain(u *url.URL) string {
hostname := u.Hostname()
ip := net.ParseIP(hostname)
if ip != nil {
return ip.String()
}
return normalizeHostname(hostname)
}
func sanitizeName(name string) string {
if ok := sanitizeNameRegex.MatchString(name); ok {
return strings.ToLower(name)
}
return ""
}
func sanitizeEntityID(id string) string {
if ok := entityIDRegex.MatchString(id); ok {
return id
}
return ""
}
func sanitizeLanguage(locale string) string {
tag, err := language.Parse(locale)
if err != nil {
return ""
}
base, _ := tag.Base()
return base.String()
}
func sanitizeText(t string) string {
return stripTagger.Sanitize(t)
}
func sanitizeEntityTaxonomyID(id string) uint16 {
v, vErr := strconv.ParseUint(id, 10, 16)
if vErr == nil {
return uint16(v)
}
return 0
}
func parseKeywords(inpKeywords string) []string {
r := []string{}
if inpKeywords == "" {
return r
}
ks := strings.Split(inpKeywords, ",")
for _, k := range ks {
v := sanitizeText(strings.TrimSpace(k))
if len(v) >= 1 {
r = append(r, v)
}
}
if len(r) > 10 {
return r[0:10]
}
return r
}
func getCursorID() (uint64, error) {
n := time.Now().UTC().Format(cursorTimeLayout)
n = strings.ReplaceAll(n, ".", "")
ui, err := strconv.ParseUint(n, 10, 64)
if err != nil {
return 0, err
}
return ui, nil
}