From 07d7eeed9eed5262d5dd802daf10e8abe867348f Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Tue, 14 May 2024 22:14:00 -0400 Subject: [PATCH] clean older tmp files --- sources/Main.swift | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sources/Main.swift b/sources/Main.swift index afc04fe7c..48c49c68a 100644 --- a/sources/Main.swift +++ b/sources/Main.swift @@ -97,8 +97,29 @@ struct SquirrelApp { // finally run everything app.run() print("Squirrel is quitting...") + cleanupOldFiles(olderThan: 5) rimeAPI.finalize() } return } + + static func cleanupOldFiles(olderThan days: Int) { + let fileManager = FileManager.default + let currentDate = Date() + let calendar = Calendar.current + + do { + let fileURLs = try fileManager.contentsOfDirectory(at: fileManager.temporaryDirectory, includingPropertiesForKeys: [.creationDateKey], options: .skipsHiddenFiles) + for fileURL in fileURLs { + if let creationDate = try fileURL.resourceValues(forKeys: [.creationDateKey]).creationDate { + if let daysDifference = calendar.dateComponents([.day], from: creationDate, to: currentDate).day, daysDifference > days { + try fileManager.removeItem(at: fileURL) + // print("Deleted: \(fileURL.path)") + } + } + } + } catch { + print("Error: \(error.localizedDescription)") + } + } }