KMFile.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // KMFile.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2024/1/11.
  6. //
  7. import Cocoa
  8. class KMFile: NSObject {
  9. var filePath: String = "" {
  10. didSet {
  11. self.url = NSURL(fileURLWithPath: filePath) as URL
  12. reloadData()
  13. }
  14. }
  15. var url: URL?
  16. var showPath: String = ""
  17. var pdfDocument: CPDFDocument?
  18. var systemPDFDocument: PDFDocument?
  19. var password: String = ""
  20. var isLocked: Bool = false
  21. var info: KMFileInfo = KMFileInfo()
  22. var attributes: [FileAttributeKey : Any]?
  23. var image: NSImage = NSImage()
  24. func reloadData() {
  25. if filePath.count != 0 {
  26. let attribe = try?FileManager.default.attributesOfItem(atPath: filePath)
  27. if attribe != nil {
  28. let document = CPDFDocument.init(url: URL(fileURLWithPath: filePath))
  29. document?.unlock(withPassword: self.password)
  30. let dateFormatter: DateFormatter = DateFormatter.init()
  31. let fileDate: Date = attribe![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
  32. var fileTime: String = ""
  33. if fileDate.isToday() {
  34. dateFormatter.dateFormat = "HH:mm"
  35. } else if self.isDateInCurrentWeek(fileDate) {
  36. dateFormatter.dateFormat = "EEE, HH:mm"
  37. } else {
  38. dateFormatter.dateFormat = "MMM d, yyyy"
  39. }
  40. let fileType = ""
  41. let sizeFloat: Float = attribe![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
  42. let fileSize = fileSizeString(sizeFloat).isEmpty ? "" : fileSizeString(sizeFloat)
  43. let lastTime = dateFormatter.string(from: fileDate)
  44. if fileDate.isToday() {
  45. fileTime = String(format: "%@, %@", NSLocalizedString("Today", comment: ""), lastTime)
  46. } else if isDateInCurrentWeek(fileDate) {
  47. fileTime = lastTime
  48. } else {
  49. fileTime = lastTime
  50. }
  51. //path
  52. let array = filePath
  53. .deletingLastPathComponent
  54. .components(separatedBy: "/")
  55. let string = NSMutableString()
  56. if array.count > 4 {
  57. string.append(".../")
  58. string.append(array[array.count - 3])
  59. string.append("/")
  60. string.append(array[array.count - 2])
  61. string.append("/")
  62. string.append(array[array.count - 1])
  63. } else {
  64. string.setString(filePath.deletingLastPathComponent)
  65. }
  66. var image: NSImage
  67. if #available(macOS 13.0, *) {
  68. image = NSImage.previewForFile(path: URL(filePath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage()
  69. } else {
  70. let page = document?.page(at: 0) ?? CPDFPage()
  71. image = page.thumbnail(of: page.size) ?? NSImage()
  72. }
  73. var info = KMFileInfo()
  74. info.size = CGFloat(sizeFloat)
  75. info.fileName = filePath.lastPathComponent
  76. info.modificationTime = fileTime
  77. self.image = image
  78. self.info = info
  79. self.pdfDocument = document
  80. self.isLocked = document!.isLocked
  81. self.showPath = string.description
  82. }
  83. }
  84. }
  85. }
  86. class KMFileInfo: NSObject {
  87. var fileName: String = ""
  88. var size: CGFloat = 0
  89. var sizeString = ""
  90. var modificationTime = ""
  91. }
  92. //MARK: 文件时间相关处理
  93. extension KMFile {
  94. func fileSizeString(_ fSize: Float) -> String {
  95. let fileSize = fSize / 1024
  96. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  97. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  98. return String(format: "%0.1f %@", size, unit)
  99. }
  100. func isSameWeek (withDate date: Date) -> Bool {
  101. let currentWeekOfYear = getWeekOfYear(date: Date.init())
  102. let targetWeekOfYear = getWeekOfYear(date: date)
  103. if targetWeekOfYear == currentWeekOfYear {
  104. return false
  105. } else {
  106. return true
  107. }
  108. }
  109. func isDateInCurrentWeek(_ date: Date) -> Bool {
  110. let calendar = Calendar.current
  111. // 获取当前日期的星期几
  112. let weekday = calendar.component(.weekday, from: Date())
  113. // 获取一周的第一天(周日)的日期
  114. let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())!
  115. // 获取一周的最后一天(下周的第一天)的日期
  116. let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)!
  117. // 判断日期是否在当前周的范围内
  118. return date > firstDayOfWeek && date < lastDayOfWeek
  119. }
  120. func getWeekOfYear(date: Date) -> Int {
  121. let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
  122. return components.weekOfYear ?? 0
  123. }
  124. }