import Foundation
/// Capture outputs are written to the temporary directory as `physarum-*` and
/// only egress via Share/Save/Export. Nothing deleted them. Sweep orphaned ones
/// at launch (prior-session leftovers, aborted sheets).
enum CaptureCleanup {
static let prefix = "physarum-"
static let captureExtensions: Set<String> = ["png", "heic", "mov", "mp4"]
static func staleCaptureFiles(in dir: URL, now: Date, olderThan: TimeInterval,
fm: FileManager = .default) -> [URL] {
guard let items = try? fm.contentsOfDirectory(
at: dir, includingPropertiesForKeys: [.contentModificationDateKey]) else { return [] }
return items.filter { url in
guard url.lastPathComponent.hasPrefix(prefix),
captureExtensions.contains(url.pathExtension.lowercased()) else { return false }
let mod = (try? url.resourceValues(forKeys: [.contentModificationDateKey])
.contentModificationDate) ?? .distantPast
return now.timeIntervalSince(mod) > olderThan
}
}
static func sweep(now: Date = Date(), olderThan: TimeInterval = 3600,
dir: URL = FileManager.default.temporaryDirectory, fm: FileManager = .default) {
for url in staleCaptureFiles(in: dir, now: now, olderThan: olderThan, fm: fm) {
try? fm.removeItem(at: url)
}
}
}