KMFileAttribute.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // KMFileAttribute.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/10/12.
  6. //
  7. import Cocoa
  8. @objcMembers class KMFileAttribute: NSObject {
  9. var filePath: String = "" {
  10. didSet {
  11. self.reloadData()
  12. }
  13. }
  14. var oriFilePath: String?
  15. var myPDFDocument: PDFDocument?
  16. var pdfDocument: CPDFDocument?
  17. var bAllPage = true
  18. private var _selectPages: [NSNumber] = []
  19. var pagesString: String = ""
  20. var isLocked = false
  21. var password: String?
  22. var pagesType: KMPageRange = .all
  23. //新增参数
  24. var fileSize: CGFloat = 0
  25. var fileImage: NSImage = NSImage()
  26. /*
  27. @property (nonatomic, assign) BOOL pageRangeError;
  28. */
  29. func reloadData() {
  30. if filePath.count != 0 {
  31. let attribe = try?FileManager.default.attributesOfItem(atPath: filePath)
  32. fileSize = attribe?[FileAttributeKey.size] as! CGFloat
  33. let document = CPDFDocument.init(url: URL(string: filePath))
  34. isLocked = document!.isLocked
  35. fileImage = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage()
  36. }
  37. }
  38. func fetchSelectPages() -> [NSNumber] {
  39. let _ = self.isInvalidString(self.pagesString)
  40. if(!self.bAllPage) {
  41. self.QuickSort(&self._selectPages, startIndex: 0, endIndex: self._selectPages.count-1)
  42. }
  43. return self._selectPages
  44. }
  45. func isInvalidString(_ text: String) -> Bool {
  46. var document: PDFDocument?
  47. if (self.myPDFDocument != nil) {
  48. document = self.myPDFDocument
  49. } else {
  50. document = PDFDocument(url: URL(fileURLWithPath: self.filePath ?? ""))
  51. }
  52. if let data = document?.isLocked, data {
  53. document?.unlock(withPassword: self.password ?? "")
  54. }
  55. let pageNumber = document?.pageCount ?? 1
  56. if (self.bAllPage) {
  57. self._selectPages = []
  58. for i in 1 ... pageNumber {
  59. self._selectPages.append(NSNumber(value: i))
  60. }
  61. return false
  62. }
  63. var pageNumbers: [NSNumber] = []
  64. var isInvalid = false
  65. var c: unichar = 0
  66. for c in text {
  67. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  68. isInvalid = true
  69. break
  70. }else{
  71. isInvalid = false
  72. }
  73. }
  74. if (!isInvalid) {
  75. let array = text.components(separatedBy: ",")
  76. for s in array {
  77. if s.isEmpty {
  78. isInvalid = true
  79. break
  80. }else{
  81. let pages = s.components(separatedBy: "-")
  82. if (pages.count>2) {
  83. isInvalid = true
  84. break
  85. }else if(pages.count==1){
  86. let p = pages.first!
  87. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  88. isInvalid = true
  89. break
  90. }else{
  91. var isEqual = false
  92. for pageNumber in pageNumbers {
  93. if pageNumber.stringValue == p {
  94. isEqual = true
  95. isInvalid = true
  96. break
  97. }
  98. }
  99. if (!isEqual) {
  100. pageNumbers.append(NSNumber(value: Int(p)!))
  101. }
  102. }
  103. }else if(pages.count==2){
  104. let p1 = pages[0]
  105. let p2 = pages[1]
  106. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  107. isInvalid = true
  108. break
  109. }else{
  110. var isEqual = false
  111. for i in Int(p1)! ... Int(p2)! {
  112. for pageNumber in pageNumbers {
  113. if pageNumber.intValue == i {
  114. isEqual = true
  115. isInvalid = true
  116. break
  117. }
  118. }
  119. }
  120. if (!isEqual) {
  121. for i in Int(p1)! ... Int(p2)! {
  122. pageNumbers.append(NSNumber(value: i))
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. if (text.isEmpty) {
  131. isInvalid = true
  132. }
  133. if (isInvalid) {
  134. self._selectPages = []
  135. }else{
  136. self._selectPages = pageNumbers
  137. }
  138. return isInvalid
  139. }
  140. func QuickSort(_ list: inout [NSNumber],startIndex: Int, endIndex: Int) {
  141. if(startIndex >= endIndex) {
  142. return
  143. }
  144. let temp = list[startIndex]
  145. var tempIndex = startIndex
  146. for i in startIndex+1 ... endIndex {
  147. let t = list[i]
  148. if (temp.intValue > t.intValue) {
  149. tempIndex = tempIndex + 1
  150. let tmp = list[tempIndex]
  151. list[tempIndex] = list[i]
  152. list[i] = tmp
  153. }
  154. }
  155. let tmp = list[tempIndex]
  156. list[tempIndex] = list[startIndex]
  157. list[startIndex] = tmp
  158. self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  159. self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  160. }
  161. /*
  162. /* give our representation to the image browser */
  163. - (id)imageRepresentation
  164. {
  165. return self.filePath;
  166. }
  167. /* use the absolute filepath as identifier */
  168. - (NSString *)imageUID
  169. {
  170. return self.filePath;
  171. }
  172. - (NSString*)imageTitle
  173. {
  174. return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
  175. }
  176. */
  177. }