miniflux-v2/server/ui/filter/image_proxy_filter.go

41 lines
1.0 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package filter
import (
"encoding/base64"
2017-11-28 06:30:04 +01:00
"strings"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/server/route"
"github.com/miniflux/miniflux/url"
2017-11-20 06:10:04 +01:00
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
)
// ImageProxyFilter rewrites image tag URLs without HTTPS to local proxy URL
2017-12-02 07:29:18 +01:00
func ImageProxyFilter(router *mux.Router, data string) string {
2017-11-20 06:10:04 +01:00
doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
if err != nil {
return data
}
doc.Find("img").Each(func(i int, img *goquery.Selection) {
if srcAttr, ok := img.Attr("src"); ok {
if !url.IsHTTPS(srcAttr) {
2017-12-02 07:29:18 +01:00
img.SetAttr("src", Proxify(router, srcAttr))
2017-11-20 06:10:04 +01:00
}
}
})
output, _ := doc.Find("body").First().Html()
return output
}
2017-12-02 07:29:18 +01:00
// Proxify returns a proxified link.
func Proxify(router *mux.Router, link string) string {
return route.Path(router, "proxy", "encodedURL", base64.StdEncoding.EncodeToString([]byte(link)))
}