// // KMCloudDocumentTools.swift // PDF Master // // Created by tangchao on 2023/2/10. // import Cocoa class KMCloudDocumentTools: NSObject { class public func canOpenDocument(_ file: KMServicesCloudFile) -> Bool { if (file.filetype != .file) { return false } let fileName = file.fileName let exe: String = (fileName?.components(separatedBy: ".").last)! if (exe.lowercased() != "pdf") { return false } return true } /// 下载文档 class public func downloadDocument(_ server: KMCloudServer, _ file: KMServicesCloudFile, callback: @escaping (Bool, URL)->()) { let localPath = self.getTempPath(file.fileName) if (localPath == nil) { return } // print(localPath) if (FileManager.default.fileExists(atPath: localPath!)) { try?FileManager.default.removeItem(atPath: localPath!) } server.downloadCloudPath(file, localPath: URL(fileURLWithPath: localPath!)) { cloudFile, progress in KMPrint(progress) } completion: { cloudFile, finished in callback(finished, URL(fileURLWithPath: localPath!)) } } /// 上传文档 class public func uploadDocument(_ server: KMCloudServer, _ file: KMServicesCloudFile, callback: @escaping (Bool, URL)->()) { let localPath = self.getTempPath(file.fileName) if (localPath == nil) { return } server.uploadCloudPath(file, localPath: URL(fileURLWithPath: localPath!)) { cloudFile, progress in } completion: { cloudFile, finished in callback(finished, URL(fileURLWithPath: localPath!)) } } /// 获取临时路径 class public func getTempPath(_ fileName: String) -> String? { let folderPath: String = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("cloudDocuments"))! if (!FileManager.default.fileExists(atPath: folderPath)) { let create: ()? = try?FileManager.default.createDirectory(atPath: folderPath, withIntermediateDirectories: false) if (create == nil) { return nil } } return folderPath.stringByAppendingPathComponent(fileName) } class public func getFileType(_ server: KMCloudServer, _ file: KMServicesCloudFile) -> String { var fileType: String = file.fileName.components(separatedBy: ".").last! if (server.serverType == KMGoogleDrive) { if (file.mimeType! == "application/vnd.google-apps.presentation") { fileType = "pptx" } else if (file.mimeType == "application/vnd.google-apps.document") { fileType = "docx" } else if (file.mimeType == "application/vnd.google-apps.spreadsheet") { fileType = "xlsx" } else if (file.mimeType == "application/vnd.google-apps.form" || file.mimeType == "application/vnd.google-apps.site") { fileType = "pptx" } else if (file.mimeType == "application/vnd.google-apps.map") { fileType = "kmz" } else if (file.mimeType == "application/vnd.google-apps.drawing") { fileType = "jpg" } } return fileType } class public func parseDate(_ date: Date) -> String { let currentDate = Date() let calendar = Calendar.init(identifier: .gregorian) let hour = calendar.component(.hour, from: currentDate) let minute = calendar.component(.minute, from: currentDate) let second = calendar.component(.second, from: currentDate) let weekday = calendar.component(.weekday, from: currentDate) let weekdayOrdinal = calendar.component(.weekdayOrdinal, from: currentDate) if (hour * 60 * 60 + minute * 60 + second + Int(date.timeIntervalSinceNow) > 0) { /// 当天 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "hh:mm" return NSLocalizedString("Today", comment: "").appending(", ").appending(dateFormatter.string(from: date)) } if (weekdayOrdinal * 24 * 60 * 60 + hour * 60 * 60 + minute * 60 + second + Int(date.timeIntervalSinceNow) > 0) { /// 当周 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "hh:mm" // dateFormatter.locale = Locale(identifier: "NL") var weekdayString: String = "" if (weekdayOrdinal == 1) { weekdayString = "Mon" } else if (weekdayOrdinal == 2) { weekdayString = "Tue" } else if (weekdayOrdinal == 3) { weekdayString = "Wed" } else if (weekdayOrdinal == 4) { weekdayString = "Thu" } else if (weekdayOrdinal == 5) { weekdayString = "Fri" } else if (weekdayOrdinal == 6) { weekdayString = "Sat" } else if (weekdayOrdinal == 7) { weekdayString = "Sun" } return NSLocalizedString(weekdayString, comment: "").appending(", ").appending(dateFormatter.string(from: date)) } let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy/MM/dd" return dateFormatter.string(from: date) } /// 获取标记 class private func tagString() -> String { var result: String = "" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyMMddHHmmss" result.append(dateFormatter.string(from: Date())) result = result.appendingFormat("%04d", arc4random()%10000) return result } }