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-11-20 06:10:04 +01:00
|
|
|
"github.com/miniflux/miniflux2/reader/url"
|
|
|
|
"github.com/miniflux/miniflux2/server/route"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImageProxyFilter rewrites image tag URLs without HTTPS to local proxy URL
|
|
|
|
func ImageProxyFilter(r *mux.Router, data string) string {
|
|
|
|
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-11-28 06:30:04 +01:00
|
|
|
path := route.Path(r, "proxy", "encodedURL", base64.StdEncoding.EncodeToString([]byte(srcAttr)))
|
2017-11-20 06:10:04 +01:00
|
|
|
img.SetAttr("src", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
output, _ := doc.Find("body").First().Html()
|
|
|
|
return output
|
|
|
|
}
|