-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage_proxy_filter.go
50 lines (41 loc) · 1.01 KB
/
image_proxy_filter.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
package pipeline
import (
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
// ImageProxyFilter DEPRECATED, use ImageURLFilter replace img src for use image proxy
type ImageProxyFilter struct {
// IgnoreHosts, Host list that will ignore, ["your-host.com", "your-assets.com"]
IgnoreHosts []string
// Formatter method with
Formatter func(src string) string
}
// Call render
func (f ImageProxyFilter) Call(doc *goquery.Document) (err error) {
doc.Find("img").Each(func(i int, node *goquery.Selection) {
src := node.AttrOr("src", "")
if !f.IsIgnoreHost(src) {
newSrc := f.Formatter(src)
node.SetAttr("src", newSrc)
}
})
return
}
func (f ImageProxyFilter) IsIgnoreHost(src string) bool {
if src == "" {
return false
}
src = strings.ToLower(src)
srcURL, err := url.Parse(src)
if err != nil {
return false
}
for _, host := range f.IgnoreHosts {
host = strings.Replace(host, "*.", "", 1)
if strings.HasSuffix(srcURL.Hostname(), strings.ToLower(host)) {
return true
}
}
return false
}