diff options
author | 2018-01-02 22:04:48 -0800 | |
---|---|---|
committer | 2018-01-02 22:04:48 -0800 | |
commit | 320d1b016747ba4501da9417d9ce5f99368a5768 (patch) | |
tree | 1054d96afde6022951b76cc4a09b78e1e3f05058 /filter | |
parent | c39f2e1a8d2de6d412bcc673d29eb0f7a2d1f5f7 (diff) |
Refactor packages to have more idiomatic code base
Diffstat (limited to 'filter')
-rw-r--r-- | filter/image_proxy_filter.go | 41 | ||||
-rw-r--r-- | filter/image_proxy_filter_test.go | 38 |
2 files changed, 79 insertions, 0 deletions
diff --git a/filter/image_proxy_filter.go b/filter/image_proxy_filter.go new file mode 100644 index 0000000..ef6d397 --- /dev/null +++ b/filter/image_proxy_filter.go @@ -0,0 +1,41 @@ +// 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" + "strings" + + "github.com/miniflux/miniflux/http/route" + "github.com/miniflux/miniflux/url" + + "github.com/PuerkitoBio/goquery" + "github.com/gorilla/mux" +) + +// ImageProxyFilter rewrites image tag URLs without HTTPS to local proxy URL +func ImageProxyFilter(router *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) { + img.SetAttr("src", Proxify(router, srcAttr)) + } + } + }) + + output, _ := doc.Find("body").First().Html() + return output +} + +// Proxify returns a proxified link. +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))) +} diff --git a/filter/image_proxy_filter_test.go b/filter/image_proxy_filter_test.go new file mode 100644 index 0000000..992516e --- /dev/null +++ b/filter/image_proxy_filter_test.go @@ -0,0 +1,38 @@ +// 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 ( + "net/http" + "testing" + + "github.com/gorilla/mux" +) + +func TestProxyFilterWithHttp(t *testing.T) { + r := mux.NewRouter() + r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") + + input := `<p><img src="http://website/folder/image.png" alt="Test"/></p>` + output := ImageProxyFilter(r, input) + expected := `<p><img src="/proxy/aHR0cDovL3dlYnNpdGUvZm9sZGVyL2ltYWdlLnBuZw==" alt="Test"/></p>` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestProxyFilterWithHttps(t *testing.T) { + r := mux.NewRouter() + r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") + + input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>` + output := ImageProxyFilter(r, input) + expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} |