diff options
author | 2023-02-25 09:36:19 +0100 | |
---|---|---|
committer | 2023-02-25 15:57:59 -0800 | |
commit | 2c2700a31d7349f67016a3786125597f9ee38c56 (patch) | |
tree | 9a9cef0c5d6a17946be70e709cf1d0349d05bc77 /proxy/image_proxy.go | |
parent | 8f9ccc6540be9d637b812985936f064bada8fcf3 (diff) | |
download | v2-2c2700a31d7349f67016a3786125597f9ee38c56.tar.gz v2-2c2700a31d7349f67016a3786125597f9ee38c56.tar.zst v2-2c2700a31d7349f67016a3786125597f9ee38c56.zip |
Proxy support for several media types
closes #615
closes #635
Diffstat (limited to 'proxy/image_proxy.go')
-rw-r--r-- | proxy/image_proxy.go | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/proxy/image_proxy.go b/proxy/image_proxy.go deleted file mode 100644 index 581a824e..00000000 --- a/proxy/image_proxy.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2020 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 proxy // import "miniflux.app/proxy" - -import ( - "strings" - - "miniflux.app/config" - "miniflux.app/reader/sanitizer" - "miniflux.app/url" - - "github.com/PuerkitoBio/goquery" - "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 - } - - doc, err := goquery.NewDocumentFromReader(strings.NewReader(data)) - if err != nil { - return data - } - - 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", proxifyFunction(router, srcAttrValue)) - } - } - - if srcsetAttrValue, ok := img.Attr("srcset"); ok { - 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, proxifyFunction, proxyImages, srcsetAttrValue) - } - }) - - output, err := doc.Find("body").First().Html() - if err != nil { - return data - } - - return output -} - -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 = proxifyFunction(router, imageCandidate.ImageURL) - } - } - - element.SetAttr("srcset", imageCandidates.String()) -} - -func isDataURL(s string) bool { - return strings.HasPrefix(s, "data:") -} |