From d08a8f67822fe0b49ffabbedab04f77dc97efe7b Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Fri, 29 Mar 2024 16:45:23 -0400 Subject: [PATCH] Digital PCT265 story MVAPCT-48 - Caches root directory, file protection for encryption, atomicWrite API deprecation. --- .../OtherHandlers/MVMCoreCache+Extension.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift index ff7fda5..fdbff0b 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift @@ -49,7 +49,7 @@ public class CachedData: Codable { @objc public class PersistentCacheManager: NSObject { @objc public static let shared = PersistentCacheManager() private let fileManager = FileManager.default - private lazy var documentsDirectory = { fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! }() + private lazy var cacheDirectory = { fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("Atomic")}() private override init() {} @@ -57,8 +57,9 @@ public class CachedData: Codable { let cachedData = CachedData(data: data, expirationDate: expirationDate) let filePath = self.filePath(forKey: key) do { + try FileManager.default.createDirectory(atPath: self.cacheDirectory.relativePath, withIntermediateDirectories: true, attributes: [.protectionKey: FileProtectionType.complete]) let dataToSave = try JSONEncoder().encode(cachedData) - try dataToSave.write(to: filePath, options: .atomicWrite) + try dataToSave.write(to: filePath, options: [.atomic, .completeFileProtection]) } catch is EncodingError { throw CacheError.serializationFailed } catch { @@ -97,16 +98,18 @@ public class CachedData: Codable { } @objc public func removeAll() throws { - let fileURLs = try fileManager.contentsOfDirectory(at: documentsDirectory, - includingPropertiesForKeys: nil, - options: .skipsHiddenFiles) - + let fileURLs = try fileManager.contentsOfDirectory( + at: cacheDirectory, + includingPropertiesForKeys: nil, + options: .skipsHiddenFiles + ) + for fileURL in fileURLs where fileURL.pathExtension == "json" { try FileManager.default.removeItem(at: fileURL) } } private func filePath(forKey key: String) -> URL { - return documentsDirectory.appendingPathComponent("\(key).json") + return cacheDirectory.appendingPathComponent("\(key).json") } }