diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-06-02 00:12:14 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2021-06-02 00:12:14 +0200 |
commit | dc19440decf28afe245226108137f8e663d490be (patch) | |
tree | 76f68e1762bd850a308e8906be3143b50cda9afc | |
parent | 376eee6450f1645762d62f68f7f642a9769f138d (diff) | |
download | feed2imap-go-dc19440decf28afe245226108137f8e663d490be.tar.gz feed2imap-go-dc19440decf28afe245226108137f8e663d490be.tar.bz2 feed2imap-go-dc19440decf28afe245226108137f8e663d490be.zip |
Do not try to download already embedded images
Diffstat (limited to '')
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | internal/feed/feed.go | 20 | ||||
-rw-r--r-- | internal/feed/mail.go | 75 |
3 files changed, 57 insertions, 40 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e9884a..5029164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Do not try to download already embedded images (i.e. `data:image/`). ## [1.0.0] - 2021-05-19 ### Fixed - [Issue #47](https://github.com/Necoro/feed2imap-go/issues/47): Fixed occassional deadlocks. Reason was unilateral updates from the server which were ill-handled by go-imap. diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 431eae3..3b9aba1 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -2,6 +2,7 @@ package feed import ( "fmt" + "net/url" "strings" "time" @@ -134,3 +135,22 @@ func (feed *Feed) id() string { } return feed.extID.String() } + +func (feed *Feed) url() *url.URL { + var feedUrl *url.URL + var err error + + if feed.Url != "" { + feedUrl, err = url.Parse(feed.Url) + if err != nil { + panic(fmt.Sprintf("URL '%s' of feed '%s' is not a valid URL. How have we ended up here?", feed.Url, feed.Name)) + } + } else if feed.feed.Link != "" { + feedUrl, err = url.Parse(feed.feed.Link) + if err != nil { + panic(fmt.Sprintf("Link '%s' of feed '%s' is not a valid URL.", feed.feed.Link, feed.Name)) + } + } + + return feedUrl +} diff --git a/internal/feed/mail.go b/internal/feed/mail.go index 799e36e..b6cd231 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -261,23 +261,41 @@ func getBody(content, description string, bodyCfg config.Body) string { } } -func (item *Item) buildBody() { +func (item *Item) downloadImage(src string) string { feed := item.feed - var feedUrl *url.URL - var err error - if feed.Url != "" { - feedUrl, err = url.Parse(feed.Url) - if err != nil { - panic(fmt.Sprintf("URL '%s' of feed '%s' is not a valid URL. How have we ended up here?", feed.Url, feed.Name)) - } - } else if feed.feed.Link != "" { - feedUrl, err = url.Parse(feed.feed.Link) - if err != nil { - panic(fmt.Sprintf("Link '%s' of feed '%s' is not a valid URL.", feed.feed.Link, feed.Name)) - } + imgUrl, err := url.Parse(src) + if err != nil { + log.Errorf("Feed %s: Item %s: Error parsing URL '%s' embedded in item: %s", + feed.Name, item.Link, src, err) + return "" + } + + if feedUrl := feed.url(); feedUrl != nil { + imgUrl = feedUrl.ResolveReference(imgUrl) } + img, mime, err := getImage(imgUrl.String(), feed.Global.Timeout, feed.NoTLS) + if err != nil { + log.Errorf("Feed %s: Item %s: Error fetching image: %s", + feed.Name, item.Link, err) + return "" + } + if img == nil { + return "" + } + + if feed.EmbedImages { + return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(img) + } else { + idx := item.addImage(img, mime) + return "cid:" + cidNr(idx) + } +} + +func (item *Item) buildBody() { + feed := item.feed + body := getBody(item.Content, item.Description, feed.Body) bodyNode, err := html.Parse(strings.NewReader(body)) if err != nil { @@ -310,33 +328,10 @@ func (item *Item) buildBody() { return } - imgUrl, err := url.Parse(src) - if err != nil { - log.Errorf("Feed %s: Item %s: Error parsing URL '%s' embedded in item: %s", - feed.Name, item.Link, src, err) - return - } - if feedUrl != nil { - imgUrl = feedUrl.ResolveReference(imgUrl) - } - - img, mime, err := getImage(imgUrl.String(), feed.Global.Timeout, feed.NoTLS) - if err != nil { - log.Errorf("Feed %s: Item %s: Error fetching image: %s", - feed.Name, item.Link, err) - return - } - if img == nil { - return - } - - if feed.EmbedImages { - imgStr := "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(img) - selection.SetAttr(attr, imgStr) - } else { - idx := item.addImage(img, mime) - cid := "cid:" + cidNr(idx) - selection.SetAttr(attr, cid) + if !strings.HasPrefix(src, "data:") { + if imgStr := item.downloadImage(src); imgStr != "" { + selection.SetAttr(attr, imgStr) + } } // srcset overrides src and would reload all the images |