-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
221 lines (187 loc) · 4.65 KB
/
util.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
package main
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"github.com/microcosm-cc/bluemonday"
"golang.org/x/net/publicsuffix"
"golang.org/x/text/language"
)
const singlePixelPNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="
var stripTagger = bluemonday.StripTagsPolicy()
var temporaryPath = "/tmp"
var rtlLanguages = []string{"ar", "dv", "fa", "he", "ps", "ur", "yi"}
var rtlLanguagesMap map[string]bool
func init() {
TEMPORARY_PATH := os.Getenv("ASM_URL_INFO_TEMPORARY_PATH")
if TEMPORARY_PATH != "" {
temporaryPath = TEMPORARY_PATH
}
rtlLanguagesMap = make(map[string]bool)
for _, v := range rtlLanguages {
rtlLanguagesMap[v] = true
}
}
func validPublicSuffix(u *url.URL) bool {
host := u.Hostname()
eTLD, icann := publicsuffix.PublicSuffix(host)
if icann {
return true
} else if strings.IndexByte(eTLD, '.') >= 0 {
return true
}
return false
}
func urlWithoutPathAndQuery(u *url.URL) *url.URL {
un, _ := url.Parse(u.String())
un.RawQuery = ""
un.Fragment = ""
un.RawPath = ""
un.Path = ""
uns, _ := url.Parse(un.String())
return uns
}
func execute(command string, arg ...string) ([]byte, int, error) {
cmd := exec.Command(command, arg...)
var stdOut bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdOut
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, cmd.Process.Pid, errors.New(err.Error() + ": " + stderr.String())
}
return stdOut.Bytes(), cmd.Process.Pid, nil
}
func sanitizeLanguage(locale string) string {
tag, err := language.Parse(locale)
if err != nil {
return ""
}
base, _ := tag.Base()
return base.String()
}
func getLanguageDirection(lang string) string {
if _, ok := rtlLanguagesMap[lang]; ok {
return "rtl"
}
return "ltr"
}
func faviconConvert(u *url.URL) string {
faviconURL := urlWithoutPathAndQuery(u)
faviconURL.Path = "/favicon.ico"
url := faviconURL.String()
cacheFile := cachePath(url, "txt")
if fileExist(cacheFile) {
b, bErr := os.ReadFile(cacheFile)
if bErr != nil {
panic(bErr)
}
return string(b)
}
base64Image := singlePixelPNG
cacheFileICO := cachePath(url, "ico")
_, _, curlErr := execute("curl", "-Lsk", "-o", cacheFileICO, faviconURL.String())
if curlErr == nil {
cacheFilePNG := cachePath(url, "png")
_, _, convertErr := execute("convert", cacheFileICO, "-thumbnail", "16x16", "-alpha", "on", "-flatten", "-strip", cacheFilePNG)
if convertErr == nil {
f, fErr := os.Open(cacheFilePNG)
if fErr != nil {
panic(fErr)
}
reader := bufio.NewReader(f)
content, contentErr := io.ReadAll(reader)
if contentErr != nil {
panic(contentErr)
}
base64Image = base64.StdEncoding.EncodeToString(content)
}
}
writeErr := os.WriteFile(cacheFile, []byte(base64Image), 0666)
if writeErr != nil {
panic(writeErr)
}
return base64Image
}
func imageConvert(u *url.URL, processParams urlInfoProcess) string {
url := u.String()
quality := processParams.ImageQuality
if quality < 10 {
quality = 10
} else if quality > 95 {
quality = 95
}
qualityString := strconv.Itoa(quality)
cacheFile := cachePath(url+":"+processParams.ImageResize+":"+qualityString, "jpg")
if fileExist(cacheFile) {
return cacheFile
}
// download
cacheFileImage := cachePath(url, "")
if !fileExist(cacheFileImage) {
_, _, curlErr := execute("curl", "-Lsk", "-o", cacheFileImage, url)
if curlErr != nil {
return ""
}
}
// optimize
_, _, convertErr := execute("convert", cacheFileImage, "-resize", processParams.ImageResize, "-sampling-factor", "4:2:0", "-quality", qualityString, "-interlace", "JPEG", "-colorspace", "sRGB", "-flatten", "-strip", cacheFile)
if convertErr == nil && fileExist(cacheFile) {
return cacheFile
}
return ""
}
func hash(s string) string {
h := md5.New()
h.Write([]byte(s))
return fmt.Sprintf("%x", h.Sum(nil))
}
func sanitizeString(str string) string {
return strings.TrimSpace(str)
}
func sanitizeText(t string) string {
return sanitizeString(stripTagger.Sanitize(t))
}
func sanitizeIntPointer(s string) *int {
i, e := strconv.Atoi(s)
if e == nil && i > 0 {
return &i
}
return nil
}
func parseKeywords(inpKeywords string) []string {
r := []string{}
if inpKeywords == "" {
return r
}
ks := strings.Split(inpKeywords, ",")
for _, k := range ks {
v := sanitizeText(k)
if len(v) >= 1 {
r = append(r, v)
}
}
if len(r) > 10 {
return r[0:10]
}
return r
}
func cachePath(s string, suffix string) string {
return fmt.Sprintf("%s/%s.%s", temporaryPath, hash(s), suffix)
}
func fileExist(p string) bool {
if st, err := os.Stat(p); err == nil {
return !st.IsDir()
}
return false
}