KMFileAttribute.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(fileURLWithPath: filePath))
  34. isLocked = document!.isLocked
  35. self.pdfDocument = document
  36. fileImage = NSImage.previewForFile(path: URL(fileURLWithPath: filePath), ofSize: CGSizeMake(136, 214), asIcon: true) ?? NSImage()
  37. }
  38. }
  39. func fetchSelectPages() -> [NSNumber] {
  40. guard let pdfDocument = pdfDocument else { return [] }
  41. selectPages.removeAll()
  42. if pagesType == .all {
  43. for i in 0..<pdfDocument.pageCount {
  44. selectPages.append(i + 1 as NSNumber)
  45. }
  46. pagesString = "1-\(pdfDocument.pageCount)"
  47. } else if pagesType == .odd {
  48. pagesString = ""
  49. for i in 0..<pdfDocument.pageCount where i % 2 == 0 {
  50. selectPages.append(i + 1 as NSNumber)
  51. if pagesString == "" {
  52. pagesString = "\(i + 1)"
  53. } else {
  54. pagesString = "\(pagesString),\(i + 1)"
  55. }
  56. }
  57. } else if pagesType == .even {
  58. pagesString = ""
  59. for i in 0..<pdfDocument.pageCount where i % 2 != 0 {
  60. selectPages.append(i + 1 as NSNumber)
  61. if pagesString == "" {
  62. pagesString = "\(i + 1)"
  63. } else {
  64. pagesString = "\(pagesString),\(i + 1)"
  65. }
  66. }
  67. } else {
  68. isInvalidString(pagesString)
  69. if !bAllPage {
  70. self.QuickSort(&selectPages, startIndex: 0, endIndex: selectPages.count-1)
  71. }
  72. }
  73. return selectPages
  74. }
  75. func isInvalidString(_ text: String) -> Bool {
  76. var document: PDFDocument?
  77. if (self.myPDFDocument != nil) {
  78. document = self.myPDFDocument
  79. } else {
  80. document = PDFDocument(url: URL(fileURLWithPath: self.filePath ))
  81. }
  82. if let data = document?.isLocked, data {
  83. document?.unlock(withPassword: self.password ?? "")
  84. }
  85. let pageNumber = document?.pageCount ?? 1
  86. if (self.bAllPage) {
  87. self.selectPages = []
  88. for i in 1 ... pageNumber {
  89. self.selectPages.append(NSNumber(value: i))
  90. }
  91. return false
  92. }
  93. var pageNumbers: [NSNumber] = []
  94. var isInvalid = false
  95. var c: unichar = 0
  96. for c in text {
  97. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  98. isInvalid = true
  99. break
  100. }else{
  101. isInvalid = false
  102. }
  103. }
  104. if (!isInvalid) {
  105. let array = text.components(separatedBy: ",")
  106. for s in array {
  107. if s.isEmpty {
  108. isInvalid = true
  109. break
  110. }else{
  111. let pages = s.components(separatedBy: "-")
  112. if (pages.count>2) {
  113. isInvalid = true
  114. break
  115. }else if(pages.count==1){
  116. let p = pages.first!
  117. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  118. isInvalid = true
  119. break
  120. }else{
  121. var isEqual = false
  122. for pageNumber in pageNumbers {
  123. if pageNumber.stringValue == p {
  124. isEqual = true
  125. isInvalid = true
  126. break
  127. }
  128. }
  129. if (!isEqual) {
  130. pageNumbers.append(NSNumber(value: Int(p)!))
  131. }
  132. }
  133. }else if(pages.count==2){
  134. let p1 = pages[0]
  135. let p2 = pages[1]
  136. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  137. isInvalid = true
  138. break
  139. }else{
  140. var isEqual = false
  141. for i in Int(p1)! ... Int(p2)! {
  142. for pageNumber in pageNumbers {
  143. if pageNumber.intValue == i {
  144. isEqual = true
  145. isInvalid = true
  146. break
  147. }
  148. }
  149. }
  150. if (!isEqual) {
  151. for i in Int(p1)! ... Int(p2)! {
  152. pageNumbers.append(NSNumber(value: i))
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if (text.isEmpty) {
  161. isInvalid = true
  162. }
  163. if (isInvalid) {
  164. self.selectPages = []
  165. }else{
  166. self.selectPages = pageNumbers
  167. }
  168. return isInvalid
  169. }
  170. func QuickSort(_ list: inout [NSNumber],startIndex: Int, endIndex: Int) {
  171. if(startIndex >= endIndex) {
  172. return
  173. }
  174. let temp = list[startIndex]
  175. var tempIndex = startIndex
  176. for i in startIndex+1 ... endIndex {
  177. let t = list[i]
  178. if (temp.intValue > t.intValue) {
  179. tempIndex = tempIndex + 1
  180. let tmp = list[tempIndex]
  181. list[tempIndex] = list[i]
  182. list[i] = tmp
  183. }
  184. }
  185. let tmp = list[tempIndex]
  186. list[tempIndex] = list[startIndex]
  187. list[startIndex] = tmp
  188. self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  189. self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  190. }
  191. /*
  192. /* give our representation to the image browser */
  193. - (id)imageRepresentation
  194. {
  195. return self.filePath;
  196. }
  197. /* use the absolute filepath as identifier */
  198. - (NSString *)imageUID
  199. {
  200. return self.filePath;
  201. }
  202. - (NSString*)imageTitle
  203. {
  204. return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
  205. }
  206. */
  207. }