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 /internal/feed/mail.go | |
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 'internal/feed/mail.go')
-rw-r--r-- | internal/feed/mail.go | 75 |
1 files changed, 35 insertions, 40 deletions
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 |