From 5d063b0f8f7dee735cc5eeb8e21c37a9f453bacd Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Tue, 24 Apr 2018 21:12:44 -0700 Subject: wip img proxy --- models/item/item.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'models') diff --git a/models/item/item.go b/models/item/item.go index 77ee02c..1e7e86a 100644 --- a/models/item/item.go +++ b/models/item/item.go @@ -3,11 +3,14 @@ package item import ( "adammathes.com/neko/models" "adammathes.com/neko/vlog" + "encoding/base64" "fmt" + "github.com/PuerkitoBio/goquery" "github.com/advancedlogic/GoOse" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday" "log" + "strings" ) type Item struct { @@ -176,6 +179,7 @@ func Filter(max_id int64, feed_id int64, category string, unread_only bool, star // but still may need to adjust rules i.Title = p.Sanitize(i.Title) i.Description = p.Sanitize(i.Description) + i.Description = rewriteImages(i.Description) i.Url = p.Sanitize(i.Url) i.FeedTitle = p.Sanitize(i.FeedTitle) i.FeedUrl = p.Sanitize(i.FeedUrl) @@ -196,3 +200,23 @@ func (i *Item) CleanHeaderImage() { i.HeaderImage = "" } } + +func rewriteImages(s string) string { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(s)) + if err != nil { + panic("can not parse doc to rewrite") + } + + doc.Find("img").Each(func(i int, img *goquery.Selection) { + if src, ok := img.Attr("src"); ok { + img.SetAttr("src", proxyURL(src)) + } + }) + + output, _ := doc.Html() + return output +} + +func proxyURL(url string) string { + return "/image/" + base64.URLEncoding.EncodeToString([]byte(url)) +} -- cgit v1.2.3