forked from Mereithhh/van-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (69 loc) · 1.46 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
package main
import (
"crypto/tls"
"database/sql"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func checkErr(err error) {
if err != nil {
fmt.Println("捕获到错误:", err)
}
}
func checkTxErr(err error, tx *sql.Tx) {
if err != nil {
fmt.Println("出现事务异常,回滚事务:", err)
err2 := tx.Rollback()
checkErr(err2)
}
}
func in(target string, str_array []string) bool {
for _, element := range str_array {
if target == element {
return true
}
}
return false
}
func getImgBase64FromUrl(url string) string {
imgUrl := url
//获取远端图片
req, err := http.NewRequest("GET", imgUrl, nil)
if err != nil {
checkErr(err)
return ""
}
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36")
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
res, err := client.Do(req)
if err != nil {
checkErr(err)
return ""
}
defer res.Body.Close()
// 读取获取的[]byte数据
data, _ := ioutil.ReadAll(res.Body)
imageBase64 := base64.StdEncoding.EncodeToString(data)
return imageBase64
}
func getSuffixFromUrl(url string) string {
suffix := url[strings.LastIndex(url, "."):]
return suffix
}
func getMIME(suffix string) string {
var t string = "image/x-icon"
if suffix == ".svg" {
t = "image/svg+xml"
}
if suffix == ".png" {
t = "image/png"
}
return t
}