-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
66 lines (56 loc) · 1.22 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
package main
import (
"bytes"
"compress/gzip"
"encoding/base64"
"io"
"os"
"path/filepath"
"strings"
)
// 解压缩 Gzip
func UncompressGzip(data []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
defer reader.Close()
uncompressed, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
return uncompressed, nil
}
func isTextMimeType(mimeType string) bool {
textTypes := []string{
"text/",
"application/json",
"application/javascript",
"application/xml",
"application/xhtml+xml",
}
for _, prefix := range textTypes {
if strings.HasPrefix(mimeType, prefix) {
return true
}
}
return false
}
func findFavicon(dir string) string {
pattern := filepath.Join(dir, "favicon.*")
matches, err := filepath.Glob(pattern)
if err != nil {
return ""
}
for _, match := range matches {
if strings.HasSuffix(match, ".ico") || strings.HasSuffix(match, ".png") || strings.HasSuffix(match, ".jpg") || strings.HasSuffix(match, ".jpeg") || strings.HasSuffix(match, ".svg") {
data, err := os.ReadFile(match)
if err != nil {
continue
}
base64String := base64.StdEncoding.EncodeToString(data)
return base64String
}
}
return ""
}