From 17a602d0e7bf4778636314c0e91e410fd7381849 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Fri, 4 Nov 2022 14:48:07 -0500 Subject: [PATCH] Add a Debug menu option to clear the image caches --- Mac/AppDelegate.swift | 19 +++++++++++++++ Mac/Base.lproj/Main.storyboard | 10 ++++++-- Shared/Extensions/CacheCleaner.swift | 36 +++++++++++++++------------- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index c1b45c983..16ac35060 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -771,6 +771,25 @@ extension AppDelegate { #endif } + @IBAction func debugClearImageCaches(_ sender: Any?) { + let alert = NSAlert() + alert.alertStyle = .warning + + let localizedMessageText = NSLocalizedString("Install theme “%@” by %@?", comment: "Theme message text") + alert.messageText = NSLocalizedString("Are you sure you want to clear the image caches? This will restart NetNewsWire to begin reloading the remote images.", + comment: "Clear and restart confirmation message.") + alert.addButton(withTitle: NSLocalizedString("Clear & Restart", comment: "Clear & Restart")) + alert.addButton(withTitle: NSLocalizedString("Cancel", comment: "Cancel")) + + let userChoice = alert.runModal() + if userChoice == .alertFirstButtonReturn { + CacheCleaner.purge() + + Process.launchedProcess(launchPath: "/usr/bin/open", arguments: ["-b", Bundle.main.bundleIdentifier!]) + NSApp.terminate(self) + } + } + @IBAction func debugTestCrashReporterWindow(_ sender: Any?) { #if DEBUG crashReportWindowController = CrashReportWindowController(crashLogText: "This is a test crash log.") diff --git a/Mac/Base.lproj/Main.storyboard b/Mac/Base.lproj/Main.storyboard index 775fa877a..06967fdb1 100644 --- a/Mac/Base.lproj/Main.storyboard +++ b/Mac/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -539,6 +539,12 @@ + + + + + + diff --git a/Shared/Extensions/CacheCleaner.swift b/Shared/Extensions/CacheCleaner.swift index 2364a59fb..b80896c5d 100644 --- a/Shared/Extensions/CacheCleaner.swift +++ b/Shared/Extensions/CacheCleaner.swift @@ -23,23 +23,7 @@ struct CacheCleaner: Logging { if flushDate.addingTimeInterval(3600 * 24 * 3) < Date() { if let reachability = try? Reachability(hostname: "apple.com") { if reachability.connection != .unavailable { - - let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! - let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") - let imagesFolderURL = tempDir.appendingPathComponent("Images") - let feedURLToIconURL = tempDir.appendingPathComponent("FeedURLToIconURLCache.plist") - let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist") - let homePagesWithNoIconURL = tempDir.appendingPathComponent("HomePagesWithNoIconURLCache.plist") - - for tempItem in [faviconsFolderURL, imagesFolderURL, feedURLToIconURL, homePageToIconURL, homePagesWithNoIconURL] { - do { - CacheCleaner.logger.info("Removing cache file: \(tempItem.absoluteString, privacy: .public)") - try FileManager.default.removeItem(at: tempItem) - } catch { - CacheCleaner.logger.error("Could not delete cache file: \(error.localizedDescription, privacy: .public)") - } - } - + purge() AppDefaults.shared.lastImageCacheFlushDate = Date() } @@ -48,4 +32,22 @@ struct CacheCleaner: Logging { } + static func purge() { + let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! + let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") + let imagesFolderURL = tempDir.appendingPathComponent("Images") + let feedURLToIconURL = tempDir.appendingPathComponent("FeedURLToIconURLCache.plist") + let homePageToIconURL = tempDir.appendingPathComponent("HomePageToIconURLCache.plist") + let homePagesWithNoIconURL = tempDir.appendingPathComponent("HomePagesWithNoIconURLCache.plist") + + for tempItem in [faviconsFolderURL, imagesFolderURL, feedURLToIconURL, homePageToIconURL, homePagesWithNoIconURL] { + do { + CacheCleaner.logger.info("Removing cache file: \(tempItem.absoluteString, privacy: .public)") + try FileManager.default.removeItem(at: tempItem) + } catch { + CacheCleaner.logger.error("Could not delete cache file: \(error.localizedDescription, privacy: .public)") + } + } + } + }