aboutsummaryrefslogtreecommitdiff
path: root/internal/feed
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-06-02 00:12:14 +0200
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-06-02 00:12:14 +0200
commitdc19440decf28afe245226108137f8e663d490be (patch)
tree76f68e1762bd850a308e8906be3143b50cda9afc /internal/feed
parent376eee6450f1645762d62f68f7f642a9769f138d (diff)
downloadfeed2imap-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')
-rw-r--r--internal/feed/feed.go20
-rw-r--r--internal/feed/mail.go75
2 files changed, 55 insertions, 40 deletions
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