Move row to next thumbnail source when image URL returns something that isn’t an actual image.

This commit is contained in:
Brent Simmons committed 2026-10-30 22:28:38 -07:00
1 parent 6d6b3f3123
commit 65962b8cf4
2 files changed
+32 -6

No files matched your search

@@ -14,6 +14,7 @@ import ImageIO
import Articles
import RSCore
import RSParser
import RSWeb
import ActivityLog
extension Notification.Name {
@@ -95,6 +96,7 @@ extension Notification.Name {
// Not flushed on appDidGoToBackground, which would make thumbnails flash on resume.
NotificationCenter.default.addObserver(self, selector: #selector(handleLowMemory(_:)), name: .lowMemory, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleNetworkStatusDidChange(_:)), name: .networkStatusDidChange, object: nil)
}
/// False when offline or in Low Data Mode, when thumbnails not already on disk aren’t downloaded.
@@ -191,6 +193,16 @@ extension Notification.Name {
}
}
func thumbnailDidFail(url: String, error: ImageDownloadError) {
if error.decodingFailed || error.rejectedPixelSize != nil {
urlsThatFailedToDecode.insert(url)
}
// A transient decoding failure counts, since the row would otherwise keep waiting for an image that isn’t coming.
if !error.isTransient || error.decodingFailed {
thumbnailURLDidFail(url)
}
}
/// Forgets the chosen thumbnail URL of each article that used `url`, and posts `.articleThumbnailDownloadDidFail`.
func thumbnailURLDidFail(_ url: String) {
var articleIDs = Set<String>()
@@ -209,6 +221,11 @@ extension Notification.Name {
@objc func handleLowMemory(_ notification: Notification) {
imageCache.removeAllObjects()
}
/// What failed to decode may have been a captive portal’s login page, not the image.
@objc func handleNetworkStatusDidChange(_ notification: Notification) {
urlsThatFailedToDecode.removeAll()
}
}
// MARK: - Private
@@ -279,12 +296,7 @@ private extension ArticleThumbnailFinder {
imageCache.setObject(decodedImage.image, forKey: cacheKey, cost: decodedImage.byteCount)
NotificationCenter.default.post(name: .articleThumbnailDidBecomeAvailable, object: self, userInfo: [Self.urlKey: url])
} catch {
if error.decodingFailed || error.rejectedPixelSize != nil {
urlsThatFailedToDecode.insert(url)
}
if !error.isTransient {
thumbnailURLDidFail(url)
}
thumbnailDidFail(url: url, error: error)
}
}
}
@@ -11,6 +11,7 @@ import CoreGraphics
import ImageIO
import UniformTypeIdentifiers
import Articles
import RSWeb
@testable import Images
/// Swift Testing also has an `Attachment` type.
@@ -93,6 +94,19 @@ private typealias ArticleAttachment = Articles.Attachment
#expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/uploads/photo.jpg")
}
@Test func nonImageResponseMovesToNextSourceUntilNetworkChanges() {
let finder = ArticleThumbnailFinder(imageDatabase: ImageDatabase(databasePath: ":memory:"))
let article = makeArticle(imageURL: "https://example.com/image.jpg", thumbnailURL: "https://example.com/thumbnail.jpg")
#expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/thumbnail.jpg")
finder.thumbnailDidFail(url: "https://example.com/thumbnail.jpg", error: ImageDownloadError(statusCode: 200, decodingFailed: true, isTransient: true))
#expect(finder.thumbnailURL(for: article, mode: .small) == "https://example.com/image.jpg")
finder.handleNetworkStatusDidChange(Notification(name: .networkStatusDidChange))
let updatedArticle = makeArticle(imageURL: "https://example.com/image.jpg", thumbnailURL: "https://example.com/thumbnail.jpg")
#expect(finder.thumbnailURL(for: updatedArticle, mode: .small) == "https://example.com/thumbnail.jpg")
}
@Test func savedHTMLThumbnailIsLoadedWithoutDiscovery() async {
let imageDatabase = ImageDatabase(databasePath: ":memory:")
let article = makeArticle(contentHTML: #"<p><img src="/uploads/photo.jpg" width="640" height="480"></p>"#)