From d481a93bd908f49d3245efa9c5fb54fc18c0a9bd Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 31 Oct 2026 14:41:33 -0700 Subject: [PATCH] =?UTF-8?q?Read=20and=20save=20the=20scaled=20copy=20for?= =?UTF-8?q?=20favicons,=20so=20they=20aren=E2=80=99t=20rescaled=20from=20t?= =?UTF-8?q?he=20original=20each=20time.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Images/SingleFaviconDownloader.swift | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift index c7a35b6e1..a0aa719fd 100644 --- a/Modules/Images/Sources/Images/SingleFaviconDownloader.swift +++ b/Modules/Images/Sources/Images/SingleFaviconDownloader.swift @@ -62,10 +62,22 @@ private extension SingleFaviconDownloader { func findFavicon() async { let kind = ActivityKind.downloadFavicon(faviconURL: faviconURL) lastFailureWasTransient = false + if let scaledData = await DownloadedImageCache.shared.derivedImageData(for: faviconURL, suffix: DownloadedImageCache.scaledIconSuffix), let image = await Self.image(scaledData) { + DownloadedImageCache.shared.imageWasUsed(faviconURL, checksForUpdates: true) + iconImage = IconImage(image) + ImageDatabase.shared.clearFailure(url: faviconURL) + postDidLoadFaviconNotification() + return + } do throws(ImageDownloadError) { let storedData = try await DownloadedImageCache.shared.imageData(for: faviconURL, downloadPriority: nil, checksForUpdates: true, activityOwner: .faviconDownloader, activityKind: kind, activityDetail: homePageURL) + let scaledData = await Self.scaledImageData(storedData, maxPixelSize: RSImage.maxIconPixelSize) // DownloadedImageCache records download failures, but can’t know whether the data decodes as an icon. - if let image = await Self.iconImage(storedData, maxPixelSize: RSImage.maxIconPixelSize) { + if let image = await Self.image(scaledData) { + // An image that’s already small enough needs no second file. + if scaledData != storedData { + await DownloadedImageCache.shared.saveDerivedImageData(scaledData, for: faviconURL, suffix: DownloadedImageCache.scaledIconSuffix) + } iconImage = IconImage(image) ImageDatabase.shared.clearFailure(url: faviconURL) } else { @@ -78,9 +90,12 @@ private extension SingleFaviconDownloader { postDidLoadFaviconNotification() } - @concurrent static func iconImage(_ data: Data, maxPixelSize: Int) async -> RSImage? { - let scaledData = RSImage.scaledImageData(data, maxPixelSize: maxPixelSize) ?? data - return RSImage(data: scaledData) + @concurrent static func scaledImageData(_ data: Data, maxPixelSize: Int) async -> Data { + RSImage.scaledImageData(data, maxPixelSize: maxPixelSize) ?? data + } + + @concurrent static func image(_ data: Data) async -> RSImage? { + RSImage(data: data) } func postDidLoadFaviconNotification() {