2019-11-08 19:20:21 +01:00
|
|
|
//
|
|
|
|
// CacheCleaner.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 11/8/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
2024-04-02 04:31:57 +02:00
|
|
|
import Web
|
2019-11-08 19:20:21 +01:00
|
|
|
|
|
|
|
struct CacheCleaner {
|
|
|
|
|
2024-05-04 08:03:31 +02:00
|
|
|
nonisolated(unsafe) static let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CacheCleaner")
|
2019-11-08 19:20:21 +01:00
|
|
|
|
2024-05-04 08:03:31 +02:00
|
|
|
@MainActor static func purgeIfNecessary() {
|
2020-02-03 00:55:51 +01:00
|
|
|
|
2020-07-02 03:57:36 +02:00
|
|
|
guard let flushDate = AppDefaults.shared.lastImageCacheFlushDate else {
|
|
|
|
AppDefaults.shared.lastImageCacheFlushDate = Date()
|
2020-02-03 00:55:51 +01:00
|
|
|
return
|
|
|
|
}
|
2024-04-07 04:02:11 +02:00
|
|
|
|
2019-11-08 19:20:21 +01:00
|
|
|
// If the image disk cache hasn't been flushed for 3 days and the network is available, delete it
|
2020-02-03 01:00:58 +01:00
|
|
|
if flushDate.addingTimeInterval(3600 * 24 * 3) < Date() {
|
2024-04-07 04:02:11 +02:00
|
|
|
if Reachability.internetIsReachable {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
os_log(.info, log: self.log, "Removing cache file: %@", tempItem.absoluteString)
|
|
|
|
try FileManager.default.removeItem(at: tempItem)
|
|
|
|
} catch {
|
|
|
|
os_log(.error, log: self.log, "Could not delete cache file: %@", error.localizedDescription)
|
2019-11-08 19:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-07 04:02:11 +02:00
|
|
|
|
|
|
|
AppDefaults.shared.lastImageCacheFlushDate = Date()
|
2019-11-08 19:20:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|