KMBatchBaseParameter.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // KMBatchBaseParameter.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/25.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBatchBaseParameter: NSObject{
  9. var error: NSError?
  10. var operateFile: KMBatchOperateFile?
  11. var pageChoice: KMBatchOperatePageChoice = .All {
  12. willSet{
  13. }
  14. didSet {
  15. if pageChoice != oldValue {
  16. self.localPagesArray = nil
  17. }
  18. }
  19. }
  20. var pageRangeString: String? {
  21. willSet{
  22. }
  23. didSet {
  24. if pageRangeString != oldValue {
  25. self.localPagesArray = nil
  26. }
  27. }
  28. }
  29. var progress: Float?
  30. var status: KMBatchOperateStatus = .Waiting
  31. var savePath: String?
  32. var outPutPath: String?
  33. var pagesArray: [NSNumber]? {
  34. if self.localPagesArray == nil {
  35. self.localPagesArray = self.fetchUnlimitPagesArray()
  36. }
  37. return self.localPagesArray
  38. }
  39. private var localPagesArray: [NSNumber]?
  40. private var _doc: CPDFDocument?
  41. init(file:KMBatchOperateFile) {
  42. super.init()
  43. self.operateFile = file
  44. }
  45. func fetchUnlimitPagesArray() -> [NSNumber]? {
  46. var document = self._doc
  47. if document == nil { // 优化重复创建
  48. document = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
  49. self._doc = document
  50. }
  51. if let data = document?.isLocked, data { // 加锁的话,需要解锁
  52. if let data = self.operateFile?.password, data.isEmpty == false {
  53. document?.unlock(withPassword: data)
  54. }
  55. }
  56. let pageNumber = document?.pageCount ?? 0
  57. if pageNumber <= 0 { // 容错判断
  58. return nil
  59. }
  60. if self.pageChoice == .All {
  61. var selectPages: [NSNumber] = []
  62. for i in 1...pageNumber {
  63. selectPages.append(NSNumber(integerLiteral: Int(i)))
  64. }
  65. return selectPages
  66. } else if self.pageChoice == .Odd {
  67. var selectPages: [NSNumber] = []
  68. for i in stride(from: 1, through: pageNumber, by: 2) {
  69. selectPages.append(NSNumber(integerLiteral: Int(i)))
  70. }
  71. return selectPages
  72. } else if self.pageChoice == .Even {
  73. var selectPages: [NSNumber] = []
  74. for i in stride(from: 2, through: pageNumber, by: 2) {
  75. selectPages.append(NSNumber(integerLiteral: Int(i)))
  76. }
  77. return selectPages
  78. } else {
  79. var pageNumbers: [NSNumber] = []
  80. var isInvalid = false
  81. if self.pageRangeString?.count ?? 0 > 0{
  82. for c in self.pageRangeString! {
  83. if c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-" {
  84. isInvalid = true
  85. break
  86. } else {
  87. isInvalid = false
  88. }
  89. }
  90. }
  91. if !isInvalid {
  92. if self.pageRangeString == nil {
  93. return nil
  94. }
  95. if let data = self.pageRangeString?.isEmpty, data { // 数据错误
  96. return nil
  97. }
  98. let array = self.pageRangeString!.components(separatedBy: ",")
  99. for s in array {
  100. if s.isEmpty {
  101. isInvalid = true
  102. break
  103. } else {
  104. let pages = s.components(separatedBy: "-")
  105. if pages.count > 2 {
  106. isInvalid = true
  107. break
  108. } else if pages.count == 1 {
  109. let p = pages[0]
  110. if p.isEmpty || Int(p)! > pageNumber || Int(p)! == 0 {
  111. isInvalid = true
  112. break
  113. } else {
  114. var isEqual = false
  115. for pageNumber in pageNumbers {
  116. if pageNumber.intValue == Int(p)! {
  117. isEqual = true
  118. isInvalid = true
  119. break
  120. }
  121. }
  122. if !isEqual {
  123. pageNumbers.append(NSNumber(integerLiteral: Int(p)!))
  124. }
  125. }
  126. } else if pages.count == 2 {
  127. let p1 = pages[0]
  128. let p2 = pages[1]
  129. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1)! == 0 {
  130. isInvalid = true
  131. break
  132. } else {
  133. var isEqual = false
  134. for i in Int(p1)!...Int(p2)! {
  135. for pageNumber in pageNumbers {
  136. if pageNumber.intValue == i {
  137. isEqual = true
  138. isInvalid = true
  139. break
  140. }
  141. }
  142. }
  143. if !isEqual {
  144. for i in Int(p1)!...Int(p2)! {
  145. pageNumbers.append(NSNumber(integerLiteral: i))
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. if isInvalid {
  154. return nil
  155. }
  156. let tmpArray = NSMutableArray(array: pageNumbers)
  157. QuickSort(list: tmpArray, startIndex: 0, endIndex: pageNumbers.count - 1)
  158. return tmpArray as? [NSNumber]
  159. }
  160. }
  161. func QuickSort(list: NSMutableArray, startIndex: NSInteger, endIndex: NSInteger) {
  162. if startIndex >= endIndex {
  163. return
  164. }
  165. let temp = list[startIndex] as! NSNumber
  166. var tempIndex = startIndex
  167. for i in (startIndex + 1)...endIndex {
  168. let t = list[i] as! NSNumber
  169. if temp.intValue > t.intValue {
  170. tempIndex = tempIndex + 1
  171. list.exchangeObject(at: tempIndex, withObjectAt: i)
  172. }
  173. }
  174. list.exchangeObject(at: tempIndex, withObjectAt: startIndex)
  175. QuickSort(list: list, startIndex: startIndex, endIndex: tempIndex-1)
  176. QuickSort(list: list, startIndex: tempIndex+1, endIndex: endIndex)
  177. }
  178. func getUniqueFilePath(filePath: String) -> String {
  179. var i = 0
  180. var isDirectory = ObjCBool(false)
  181. var uniqueFilePath = filePath
  182. let fileManager = FileManager.default
  183. fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
  184. if isDirectory.boolValue {
  185. while fileManager.fileExists(atPath: uniqueFilePath) {
  186. i += 1
  187. uniqueFilePath = "\(filePath)(\(i))"
  188. }
  189. } else {
  190. while fileManager.fileExists(atPath: uniqueFilePath) {
  191. i += 1
  192. var path = "\(filePath.deletingPathExtension)(\(i))"
  193. // uniqueFilePath = path.stringByAppendingPathComponent(filePath.extension)
  194. // path.append(".")
  195. path.append(filePath.extension)
  196. uniqueFilePath = path
  197. }
  198. }
  199. return uniqueFilePath
  200. }
  201. func resetState() {
  202. status = .Waiting
  203. progress = 0
  204. error = nil
  205. savePath = nil
  206. outPutPath = nil
  207. }
  208. func fetchDestinationFilepath() -> String? {
  209. if self.isEqual(self.operateFile?.convertInfo) {
  210. // 在子类重写了这个方法,这里永远不会调用,所以不需要写
  211. } else if self.isEqual(self.operateFile?.addPasswordInfo) {
  212. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  213. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_SetPassword")
  214. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  215. return self.outPutPath
  216. }
  217. } else if self.isEqual(self.operateFile?.removePasswordInfo) {
  218. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  219. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemovePassword")
  220. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  221. return self.outPutPath
  222. }
  223. } else if self.isEqual(self.operateFile?.addWatermarkInfo) {
  224. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  225. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_Watermark")
  226. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  227. return self.outPutPath
  228. }
  229. } else if self.isEqual(self.operateFile?.removeWatermarkInfo) {
  230. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  231. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveWatermark")
  232. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  233. return self.outPutPath
  234. }
  235. } else if self.isEqual(self.operateFile?.addBackgroundInfo) {
  236. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  237. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddBackground")
  238. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  239. return self.outPutPath
  240. }
  241. } else if self.isEqual(self.operateFile?.removeBackgroundInfo) {
  242. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  243. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveBackground")
  244. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  245. return self.outPutPath
  246. }
  247. } else if self.isEqual(self.operateFile?.addHeaderFooterInfo) {
  248. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  249. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddHeaderFooter")
  250. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  251. return self.outPutPath
  252. }
  253. } else if self.isEqual(self.operateFile?.removeHeaderFooterInfo) {
  254. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  255. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveHeaderFooter")
  256. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  257. return self.outPutPath
  258. }
  259. } else if self.isEqual(self.operateFile?.addBatesInfo) {
  260. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  261. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddBates")
  262. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  263. return self.outPutPath
  264. }
  265. } else if self.isEqual(self.operateFile?.removeBatesInfo) {
  266. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  267. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveBates")
  268. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  269. return self.outPutPath
  270. }
  271. } else if self.isEqual(self.operateFile?.compressInfo) {
  272. if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
  273. let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_Compressed")
  274. self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
  275. return self.outPutPath
  276. }
  277. }
  278. return nil
  279. }
  280. }