From 261d48b58b0e329c2cae9f2d508aa9aac4f94e21 Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Wed, 25 Oct 2023 19:31:39 -0400 Subject: [PATCH] only update icon_url if successful --- internal/model/feed.go | 8 ++++---- internal/model/icon.go | 1 + internal/reader/handler/handler.go | 33 +++++++++++++++--------------- internal/reader/icon/finder.go | 1 + internal/storage/feed.go | 12 ++++------- internal/storage/icon.go | 4 ++++ 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/internal/model/feed.go b/internal/model/feed.go index 6a76411e9c7..b91f1056c24 100644 --- a/internal/model/feed.go +++ b/internal/model/feed.go @@ -51,16 +51,16 @@ type Feed struct { FetchViaProxy bool `json:"fetch_via_proxy"` HideGlobally bool `json:"hide_globally"` AppriseServiceURLs string `json:"apprise_service_urls"` + IconURL string `json:"icon_url"` // Non persisted attributes Category *Category `json:"category,omitempty"` Icon *FeedIcon `json:"icon"` Entries Entries `json:"entries,omitempty"` - TTL int `json:"-"` - IconURL string `json:"-"` - UnreadCount int `json:"-"` - ReadCount int `json:"-"` + TTL int `json:"-"` + UnreadCount int `json:"-"` + ReadCount int `json:"-"` } type FeedCounters struct { diff --git a/internal/model/icon.go b/internal/model/icon.go index 7a38b75c90f..51257106c1d 100644 --- a/internal/model/icon.go +++ b/internal/model/icon.go @@ -14,6 +14,7 @@ type Icon struct { Hash string `json:"hash"` MimeType string `json:"mime_type"` Content []byte `json:"-"` + URL string `json:"-"` } // DataURL returns the data URL of the icon. diff --git a/internal/reader/handler/handler.go b/internal/reader/handler/handler.go index fd4b11846e7..351e33455c9 100644 --- a/internal/reader/handler/handler.go +++ b/internal/reader/handler/handler.go @@ -98,6 +98,7 @@ func CreateFeedFromSubscriptionDiscovery(store *storage.Storage, userID int64, f subscription.ID, subscription.SiteURL, subscription.IconURL, + false, ) return subscription, nil @@ -190,6 +191,7 @@ func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model subscription.ID, subscription.SiteURL, subscription.IconURL, + false, ) return subscription, nil } @@ -254,8 +256,6 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool return localizedError } - refreshIcon := forceRefresh - if originalFeed.IgnoreHTTPCache || responseHandler.IsModified(originalFeed.EtagHeader, originalFeed.LastModifiedHeader) { slog.Debug("Feed modified", slog.Int64("user_id", userID), @@ -334,7 +334,7 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool if updatedFeed.IconURL != originalFeed.IconURL { originalFeed.IconURL = updatedFeed.IconURL - refreshIcon = true + forceRefresh = true } } else { slog.Debug("Feed not modified", @@ -343,22 +343,13 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool ) } - if refreshIcon { - if err := store.RemoveFeedIcon(feedID); err != nil { - slog.Debug("Unable to remove Feed Icon", - slog.Int64("feed_id", feedID), - ) - } - } - checkFeedIcon( store, + requestBuilder, originalFeed.ID, originalFeed.SiteURL, originalFeed.IconURL, - originalFeed.UserAgent, - originalFeed.FetchViaProxy, - originalFeed.AllowSelfSignedCertificates, + forceRefresh, ) originalFeed.ResetErrorCounter() @@ -373,8 +364,8 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool return nil } -func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string) { - if !store.HasIcon(feedID) { +func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilder, feedID int64, websiteURL, feedIconURL string, forceRefresh bool) { + if !store.HasIcon(feedID) || forceRefresh { iconFinder := icon.NewIconFinder(requestBuilder, websiteURL, feedIconURL) if icon, err := iconFinder.FindIcon(); err != nil { slog.Debug("Unable to find feed icon", @@ -390,6 +381,16 @@ func checkFeedIcon(store *storage.Storage, requestBuilder *fetcher.RequestBuilde slog.String("feed_icon_url", feedIconURL), ) } else { + if icon.URL == "" { + icon.URL = feedIconURL + } + if forceRefresh { + if err := store.RemoveFeedIcon(feedID); err != nil { + slog.Debug("Unable to remove Feed Icon", + slog.Int64("feed_id", feedID), + ) + } + } if err := store.CreateFeedIcon(feedID, icon); err != nil { slog.Error("Unable to store feed icon", slog.Int64("feed_id", feedID), diff --git a/internal/reader/icon/finder.go b/internal/reader/icon/finder.go index 91a74d142db..d66cb9f1611 100644 --- a/internal/reader/icon/finder.go +++ b/internal/reader/icon/finder.go @@ -173,6 +173,7 @@ func (f *IconFinder) DownloadIcon(iconURL string) (*model.Icon, error) { Hash: crypto.HashFromBytes(responseBody), MimeType: responseHandler.ContentType(), Content: responseBody, + URL: iconURL, } return icon, nil diff --git a/internal/storage/feed.go b/internal/storage/feed.go index 67e4f485797..f365591ef16 100644 --- a/internal/storage/feed.go +++ b/internal/storage/feed.go @@ -229,11 +229,10 @@ func (s *Storage) CreateFeed(feed *model.Feed) error { hide_globally, url_rewrite_rules, no_media_player, - apprise_service_urls, - icon_url + apprise_service_urls ) VALUES - ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25) + ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24) RETURNING id ` @@ -263,7 +262,6 @@ func (s *Storage) CreateFeed(feed *model.Feed) error { feed.UrlRewriteRules, feed.NoMediaPlayer, feed.AppriseServiceURLs, - feed.IconURL, ).Scan(&feed.ID) if err != nil { return fmt.Errorf(`store: unable to create feed %q: %v`, feed.FeedURL, err) @@ -335,10 +333,9 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) { hide_globally=$24, url_rewrite_rules=$25, no_media_player=$26, - apprise_service_urls=$27, - icon_url=$28 + apprise_service_urls=$27 WHERE - id=$29 AND user_id=$30 + id=$28 AND user_id=$29 ` _, err = s.db.Exec(query, feed.FeedURL, @@ -368,7 +365,6 @@ func (s *Storage) UpdateFeed(feed *model.Feed) (err error) { feed.UrlRewriteRules, feed.NoMediaPlayer, feed.AppriseServiceURLs, - feed.IconURL, feed.ID, feed.UserID, ) diff --git a/internal/storage/icon.go b/internal/storage/icon.go index 6ef760a808d..689d5432b2f 100644 --- a/internal/storage/icon.go +++ b/internal/storage/icon.go @@ -111,6 +111,10 @@ func (s *Storage) CreateFeedIcon(feedID int64, icon *model.Icon) error { if err != nil { return fmt.Errorf(`store: unable to create feed icon: %v`, err) } + _, err = s.db.Exec(`UPDATE feed SET icon_url=$1 WHERE ID=$2`, icon.URL, feedID) + if err != nil { + return fmt.Errorf(`store: unable to save icon_url: %v`, err) + } return nil }