KMFileAttribute.swift 7.9 KB

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