mirror of
https://github.com/miniflux/v2.git
synced 2025-07-02 16:38:37 +00:00
Proxify images in API responses
This commit is contained in:
parent
206be5ba15
commit
3f14d08095
12 changed files with 146 additions and 19 deletions
|
@ -15,8 +15,22 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type urlProxyRewriter func(router *mux.Router, url string) string
|
||||
|
||||
// ImageProxyRewriter replaces image URLs with internal proxy URLs.
|
||||
func ImageProxyRewriter(router *mux.Router, data string) string {
|
||||
return genericImageProxyRewriter(router, ProxifyURL, data)
|
||||
}
|
||||
|
||||
// AbsoluteImageProxyRewriter do the same as ImageProxyRewriter except it uses absolute URLs.
|
||||
func AbsoluteImageProxyRewriter(router *mux.Router, host, data string) string {
|
||||
proxifyFunction := func(router *mux.Router, url string) string {
|
||||
return AbsoluteProxifyURL(router, host, url)
|
||||
}
|
||||
return genericImageProxyRewriter(router, proxifyFunction, data)
|
||||
}
|
||||
|
||||
func genericImageProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, data string) string {
|
||||
proxyImages := config.Opts.ProxyImages()
|
||||
if proxyImages == "none" {
|
||||
return data
|
||||
|
@ -30,18 +44,18 @@ func ImageProxyRewriter(router *mux.Router, data string) string {
|
|||
doc.Find("img").Each(func(i int, img *goquery.Selection) {
|
||||
if srcAttrValue, ok := img.Attr("src"); ok {
|
||||
if !isDataURL(srcAttrValue) && (proxyImages == "all" || !url.IsHTTPS(srcAttrValue)) {
|
||||
img.SetAttr("src", ProxifyURL(router, srcAttrValue))
|
||||
img.SetAttr("src", proxifyFunction(router, srcAttrValue))
|
||||
}
|
||||
}
|
||||
|
||||
if srcsetAttrValue, ok := img.Attr("srcset"); ok {
|
||||
proxifySourceSet(img, router, proxyImages, srcsetAttrValue)
|
||||
proxifySourceSet(img, router, proxifyFunction, proxyImages, srcsetAttrValue)
|
||||
}
|
||||
})
|
||||
|
||||
doc.Find("picture source").Each(func(i int, sourceElement *goquery.Selection) {
|
||||
if srcsetAttrValue, ok := sourceElement.Attr("srcset"); ok {
|
||||
proxifySourceSet(sourceElement, router, proxyImages, srcsetAttrValue)
|
||||
proxifySourceSet(sourceElement, router, proxifyFunction, proxyImages, srcsetAttrValue)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -53,12 +67,12 @@ func ImageProxyRewriter(router *mux.Router, data string) string {
|
|||
return output
|
||||
}
|
||||
|
||||
func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxyImages, srcsetAttrValue string) {
|
||||
func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyImages, srcsetAttrValue string) {
|
||||
imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
|
||||
|
||||
for _, imageCandidate := range imageCandidates {
|
||||
if !isDataURL(imageCandidate.ImageURL) && (proxyImages == "all" || !url.IsHTTPS(imageCandidate.ImageURL)) {
|
||||
imageCandidate.ImageURL = ProxifyURL(router, imageCandidate.ImageURL)
|
||||
imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue