KMFile.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // KMFile.swift
  3. // PDF Reader Pro
  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 = "yyyy-MM-dd HH:mm:ss"
  39. // }
  40. let fileType = ""
  41. let sizeFloat: CGFloat = (attribe![FileAttributeKey(rawValue: "NSFileSize")]) as? CGFloat ?? 0.0
  42. let fileSize = fileSizeString(Float(sizeFloat)).isEmpty ? "" : fileSizeString(Float(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. let image = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: NSMakeSize(136, 214), asIcon: true) ?? NSImage()
  67. var info = KMFileInfo()
  68. info.size = CGFloat(sizeFloat)
  69. info.fileName = filePath.lastPathComponent
  70. info.modificationTime = fileTime
  71. info.sizeString = fileSize
  72. self.image = image
  73. self.info = info
  74. // self.pdfDocument = document
  75. // self.isLocked = ((document?.isLocked) != nil)
  76. self.showPath = string.description
  77. }
  78. }
  79. }
  80. }
  81. class KMFileInfo: NSObject {
  82. var fileName: String = ""
  83. var size: CGFloat = 0
  84. var sizeString = ""
  85. var modificationTime = ""
  86. }
  87. //MARK: 文件时间相关处理
  88. extension KMFile {
  89. func fileSizeString(_ fSize: Float) -> String {
  90. let fileSize = fSize / 1024
  91. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  92. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  93. return String(format: "%0.1f %@", size, unit)
  94. }
  95. func isSameWeek (withDate date: Date) -> Bool {
  96. let currentWeekOfYear = getWeekOfYear(date: Date.init())
  97. let targetWeekOfYear = getWeekOfYear(date: date)
  98. if targetWeekOfYear == currentWeekOfYear {
  99. return false
  100. } else {
  101. return true
  102. }
  103. }
  104. func isDateInCurrentWeek(_ date: Date) -> Bool {
  105. let calendar = Calendar.current
  106. // 获取当前日期的星期几
  107. let weekday = calendar.component(.weekday, from: Date())
  108. // 获取一周的第一天(周日)的日期
  109. let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())!
  110. // 获取一周的最后一天(下周的第一天)的日期
  111. let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)!
  112. // 判断日期是否在当前周的范围内
  113. return date > firstDayOfWeek && date < lastDayOfWeek
  114. }
  115. func getWeekOfYear(date: Date) -> Int {
  116. let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
  117. return components.weekOfYear ?? 0
  118. }
  119. }