Skip to content

Commit

Permalink
clean older tmp files
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed May 15, 2024
1 parent b129e58 commit 07d7eee
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sources/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}

0 comments on commit 07d7eee

Please sign in to comment.