KMFileAttribute.swift 7.6 KB

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