1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

Proxify articles crawled manually

This commit is contained in:
Frédéric Guillot 2020-10-07 22:21:45 -07:00
parent 997006cdd7
commit 6117a2f3cd
6 changed files with 347 additions and 305 deletions

View file

@ -5,7 +5,6 @@
package template // import "miniflux.app/template"
import (
"encoding/base64"
"fmt"
"html/template"
"math"
@ -18,11 +17,11 @@ import (
"miniflux.app/http/route"
"miniflux.app/locale"
"miniflux.app/model"
"miniflux.app/proxy"
"miniflux.app/reader/sanitizer"
"miniflux.app/timezone"
"miniflux.app/url"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
"github.com/rylans/getlang"
)
@ -58,13 +57,13 @@ func (f *funcMap) Map() template.FuncMap {
return template.HTML(str)
},
"proxyFilter": func(data string) string {
return imageProxyFilter(f.router, data)
return proxy.ImageProxyRewriter(f.router, data)
},
"proxyURL": func(link string) string {
proxyImages := config.Opts.ProxyImages()
if proxyImages == "all" || (proxyImages != "none" && !url.IsHTTPS(link)) {
return proxify(f.router, link)
return proxy.ProxifyURL(f.router, link)
}
return link
@ -183,71 +182,6 @@ func elapsedTime(printer *locale.Printer, tz string, t time.Time) string {
}
}
func imageProxyFilter(router *mux.Router, 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 srcAttr, ok := img.Attr("src"); ok {
if proxyImages == "all" || !url.IsHTTPS(srcAttr) {
img.SetAttr("src", proxify(router, srcAttr))
}
}
if srcsetAttr, ok := img.Attr("srcset"); ok {
if proxyImages == "all" || !url.IsHTTPS(srcsetAttr) {
proxifySourceSet(img, router, srcsetAttr)
}
}
})
doc.Find("picture source").Each(func(i int, sourceElement *goquery.Selection) {
if srcsetAttr, ok := sourceElement.Attr("srcset"); ok {
if proxyImages == "all" || !url.IsHTTPS(srcsetAttr) {
proxifySourceSet(sourceElement, router, srcsetAttr)
}
}
})
output, _ := doc.Find("body").First().Html()
return output
}
func proxifySourceSet(element *goquery.Selection, router *mux.Router, attributeValue string) {
var proxifiedSources []string
for _, source := range strings.Split(attributeValue, ",") {
parts := strings.Split(strings.TrimSpace(source), " ")
nbParts := len(parts)
if nbParts > 0 {
source = proxify(router, parts[0])
if nbParts > 1 {
source += " " + parts[1]
}
proxifiedSources = append(proxifiedSources, source)
}
}
if len(proxifiedSources) > 0 {
element.SetAttr("srcset", strings.Join(proxifiedSources, ", "))
}
}
func proxify(router *mux.Router, link string) string {
// We use base64 url encoding to avoid slash in the URL.
return route.Path(router, "proxy", "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
}
func formatFileSize(b int64) string {
const unit = 1024
if b < unit {