// // KMFile.swift // PDF Reader Pro // // Created by lizhe on 2024/1/11. // import Cocoa class KMFile: NSObject { var filePath: String = "" { didSet { self.url = NSURL(fileURLWithPath: filePath) as URL reloadData() } } var url: URL? var showPath: String = "" var pdfDocument: CPDFDocument? var systemPDFDocument: PDFDocument? var password: String = "" var isLocked: Bool = false var info: KMFileInfo = KMFileInfo() var attributes: [FileAttributeKey : Any]? var image: NSImage = NSImage() func reloadData() { if filePath.count != 0 { let attribe = try?FileManager.default.attributesOfItem(atPath: filePath) if attribe != nil { let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath)) document?.unlock(withPassword: self.password) let dateFormatter: DateFormatter = DateFormatter.init() let fileDate: Date = attribe![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date var fileTime: String = "" // if fileDate.isToday() { // dateFormatter.dateFormat = "HH:mm" // } else if self.isDateInCurrentWeek(fileDate) { // dateFormatter.dateFormat = "EEE, HH:mm" // } else { dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // } let fileType = "" let sizeFloat: Float = attribe![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0 let fileSize = fileSizeString(sizeFloat).isEmpty ? "" : fileSizeString(sizeFloat) let lastTime = dateFormatter.string(from: fileDate) // if fileDate.isToday() { // fileTime = String(format: "%@, %@", NSLocalizedString("Today", comment: ""), lastTime) // } else if isDateInCurrentWeek(fileDate) { // fileTime = lastTime // } else { fileTime = lastTime // } //path let array = filePath .deletingLastPathComponent .components(separatedBy: "/") let string = NSMutableString() if array.count > 4 { string.append(".../") string.append(array[array.count - 3]) string.append("/") string.append(array[array.count - 2]) string.append("/") string.append(array[array.count - 1]) } else { string.setString(filePath.deletingLastPathComponent) } let image = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage() var info = KMFileInfo() info.size = CGFloat(sizeFloat) info.fileName = filePath.lastPathComponent info.modificationTime = fileTime info.sizeString = fileSize self.image = image self.info = info self.pdfDocument = document self.isLocked = ((document?.isLocked) != nil) self.showPath = string.description } } } } class KMFileInfo: NSObject { var fileName: String = "" var size: CGFloat = 0 var sizeString = "" var modificationTime = "" } //MARK: 文件时间相关处理 extension KMFile { func fileSizeString(_ fSize: Float) -> String { let fileSize = fSize / 1024 let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K" return String(format: "%0.1f %@", size, unit) } func isSameWeek (withDate date: Date) -> Bool { let currentWeekOfYear = getWeekOfYear(date: Date.init()) let targetWeekOfYear = getWeekOfYear(date: date) if targetWeekOfYear == currentWeekOfYear { return false } else { return true } } func isDateInCurrentWeek(_ date: Date) -> Bool { let calendar = Calendar.current // 获取当前日期的星期几 let weekday = calendar.component(.weekday, from: Date()) // 获取一周的第一天(周日)的日期 let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())! // 获取一周的最后一天(下周的第一天)的日期 let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)! // 判断日期是否在当前周的范围内 return date > firstDayOfWeek && date < lastDayOfWeek } func getWeekOfYear(date: Date) -> Int { let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date) return components.weekOfYear ?? 0 } }