import Foundation import TelegramCore public func groupCallLogsPath(account: Account) -> String { return account.basePath + "/group-calls" } func cleanupGroupCallLogs(account: Account) { let path = groupCallLogsPath(account: account) let fileManager = FileManager.default if !fileManager.fileExists(atPath: path, isDirectory: nil) { try? fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil) } var oldest: [(URL, Date)] = [] var count = 0 if let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: [.contentModificationDateKey], options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants], errorHandler: nil) { for url in enumerator { if let url = url as? URL { if let date = (try? url.resourceValues(forKeys: Set([.contentModificationDateKey])))?.contentModificationDate { oldest.append((url, date)) count += 1 } } } } let callLogsLimit = 20 if count > callLogsLimit { oldest.sort(by: { $0.1 > $1.1 }) while oldest.count > callLogsLimit { try? fileManager.removeItem(atPath: oldest[oldest.count - 1].0.path) oldest.removeLast() } } } public func allocateCallLogPath(account: Account) -> String { let path = groupCallLogsPath(account: account) let _ = try? FileManager.default.createDirectory(at: URL(fileURLWithPath: path), withIntermediateDirectories: true, attributes: nil) let name = "log-\(Date())".replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: ":", with: "_") return "\(path)/\(name).log" }