KMBatchOperateFile.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // KMBatchOperateFile.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/25.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBatchOperateFile: NSObject{
  9. /**基本信息*/
  10. var password: String = ""
  11. var filePath: String = ""
  12. var outputFilePath: String = ""
  13. lazy var sizeString: String = {
  14. let attributes = try? FileManager.default.attributesOfItem(atPath: self.filePath)
  15. let fileSize: Int = attributes?[FileAttributeKey.size] as! Int
  16. let _sizeString = self.fileSizeString(fSize: Float(fileSize))
  17. return _sizeString
  18. }()
  19. var fileType: KMBatchOperateFileType?
  20. /*当前正在进行的任务状态*/
  21. var error: NSError? {
  22. return self.currentOperateInfo?.error
  23. }
  24. var status: KMBatchOperateStatus?{
  25. return self.currentOperateInfo?.status
  26. }
  27. var dpi: Int? {
  28. if self.currentOperateType == .Convert{
  29. return self.currentConvertParameter?.dpi
  30. }
  31. return 0
  32. }
  33. var progress: Float?{
  34. return self.currentOperateInfo?.progress
  35. }
  36. var currentOperateType: KMBatchOperationType?
  37. var convertType: KMConvertWithPDFType = .WordAdvance
  38. /*当前的操作详细信息**/
  39. lazy var createPDFInfo: KMBatchBaseParameter = {
  40. let info = KMBatchBaseParameter(file: self)
  41. return info
  42. }()
  43. var convertInfo: KMBatchConvertParameter?{
  44. return self.currentConvertParameter
  45. }
  46. lazy var mergeInfo: KMBatchBaseParameter = {
  47. let info = KMBatchBaseParameter(file: self)
  48. return info
  49. }()
  50. lazy var compressInfo: KMBatchBaseParameter = {
  51. let info = KMBatchBaseParameter(file: self)
  52. return info
  53. }()
  54. lazy var ocrInfo: KMBatchBaseParameter = {
  55. let info = KMBatchBaseParameter(file: self)
  56. return info
  57. }()
  58. lazy var addPasswordInfo: KMBatchBaseParameter = {
  59. let info = KMBatchBaseParameter(file: self)
  60. return info
  61. }()
  62. lazy var removePasswordInfo: KMBatchBaseParameter = {
  63. let info = KMBatchBaseParameter(file: self)
  64. return info
  65. }()
  66. lazy var addWatermarkInfo: KMBatchBaseParameter = {
  67. let info = KMBatchBaseParameter(file: self)
  68. return info
  69. }()
  70. lazy var removeWatermarkInfo: KMBatchBaseParameter = {
  71. let info = KMBatchBaseParameter(file: self)
  72. return info
  73. }()
  74. lazy var addBackgroundInfo: KMBatchBaseParameter = {
  75. let info = KMBatchBaseParameter(file: self)
  76. return info
  77. }()
  78. lazy var removeBackgroundInfo: KMBatchBaseParameter = {
  79. let info = KMBatchBaseParameter(file: self)
  80. return info
  81. }()
  82. lazy var addHeaderFooterInfo: KMBatchBaseParameter = {
  83. let info = KMBatchBaseParameter(file: self)
  84. return info
  85. }()
  86. lazy var removeHeaderFooterInfo: KMBatchBaseParameter = {
  87. let info = KMBatchBaseParameter(file: self)
  88. return info
  89. }()
  90. lazy var addBatesInfo: KMBatchBaseParameter = {
  91. let info = KMBatchBaseParameter(file: self)
  92. return info
  93. }()
  94. lazy var removeBatesInfo: KMBatchBaseParameter = {
  95. let info = KMBatchBaseParameter(file: self)
  96. return info
  97. }()
  98. var currentOperateInfo: KMBatchBaseParameter?{
  99. switch self.currentOperateType {
  100. case .CreatePDF:
  101. return self.createPDFInfo
  102. case .Convert:
  103. return self.convertInfo
  104. case .Merge:
  105. return self.mergeInfo
  106. case .Compress:
  107. return self.compressInfo
  108. case .OCR:
  109. return self.ocrInfo
  110. case .AddPassword:
  111. return self.addPasswordInfo
  112. case .RemovePassword:
  113. return self.removePasswordInfo
  114. case .AddWatermark:
  115. return self.addWatermarkInfo
  116. case .RemoveWatermark:
  117. return self.removeWatermarkInfo
  118. case .AddBackground:
  119. return self.addBackgroundInfo
  120. case .RemoveBackground:
  121. return self.removeBackgroundInfo
  122. case .AddHeaderFooter:
  123. return self.addHeaderFooterInfo
  124. case .RemoveHeaderFooter:
  125. return self.removeHeaderFooterInfo
  126. case .AddBates:
  127. return self.addBatesInfo
  128. case .RemoveBates:
  129. return self.removeBatesInfo
  130. case .none:
  131. return nil
  132. case .some(.NotDetermined):
  133. return nil
  134. }
  135. }
  136. /*转换详细信息**/
  137. lazy var standardWordParameter: KMBatchConvertParameter = {
  138. let paramter = KMBatchConvertParameter(file: self)
  139. paramter.convertType = .WordStandard
  140. return paramter
  141. }()
  142. lazy var advanceWordParameter: KMBatchConvertParameter = {
  143. let paramter = KMBatchConvertParameter(file: self)
  144. paramter.convertType = .WordAdvance
  145. return paramter
  146. }()
  147. lazy var excelParameter: KMBatchConvertParameter = {
  148. let paramter = KMBatchConvertParameter(file: self)
  149. paramter.convertType = .Excel
  150. return paramter
  151. }()
  152. lazy var PPTParameter: KMBatchConvertParameter = {
  153. let paramter = KMBatchConvertParameter(file: self)
  154. paramter.convertType = .PowerPoint
  155. return paramter
  156. }()
  157. lazy var RTFParameter: KMBatchConvertParameter = {
  158. let paramter = KMBatchConvertParameter(file: self)
  159. paramter.convertType = .RTF
  160. return paramter
  161. }()
  162. lazy var CSVParameter: KMBatchConvertParameter = {
  163. let paramter = KMBatchConvertParameter(file: self)
  164. paramter.convertType = .CSV
  165. return paramter
  166. }()
  167. lazy var HTMLParameter: KMBatchConvertParameter = {
  168. let paramter = KMBatchConvertParameter(file: self)
  169. paramter.convertType = .HTML
  170. return paramter
  171. }()
  172. lazy var TXTParameter: KMBatchConvertParameter = {
  173. let paramter = KMBatchConvertParameter(file: self)
  174. paramter.convertType = .Text
  175. return paramter
  176. }()
  177. lazy var JPEGParameter: KMBatchConvertParameter = {
  178. let paramter = KMBatchConvertParameter(file: self)
  179. paramter.convertType = .JPEG
  180. return paramter
  181. }()
  182. lazy var JPGParameter: KMBatchConvertParameter = {
  183. let paramter = KMBatchConvertParameter(file: self)
  184. paramter.convertType = .JPG
  185. return paramter
  186. }()
  187. lazy var PNGParameter: KMBatchConvertParameter = {
  188. let paramter = KMBatchConvertParameter(file: self)
  189. paramter.convertType = .PNG
  190. return paramter
  191. }()
  192. lazy var GIFParameter: KMBatchConvertParameter = {
  193. let paramter = KMBatchConvertParameter(file: self)
  194. paramter.convertType = .GIF
  195. return paramter
  196. }()
  197. lazy var TIFFParameter: KMBatchConvertParameter = {
  198. let paramter = KMBatchConvertParameter(file: self)
  199. paramter.convertType = .TIFF
  200. return paramter
  201. }()
  202. lazy var TGAParameter: KMBatchConvertParameter = {
  203. let paramter = KMBatchConvertParameter(file: self)
  204. paramter.convertType = .TGA
  205. return paramter
  206. }()
  207. lazy var BMPParameter: KMBatchConvertParameter = {
  208. let paramter = KMBatchConvertParameter(file: self)
  209. paramter.convertType = .BMP
  210. return paramter
  211. }()
  212. lazy var JPEG2000Parameter: KMBatchConvertParameter = {
  213. let paramter = KMBatchConvertParameter(file: self)
  214. paramter.convertType = .JPEG2000
  215. return paramter
  216. }()
  217. init(filePath: String) {
  218. super.init()
  219. self.filePath = filePath
  220. let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
  221. let TypeCFString = UTTypeString as CFString?
  222. if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
  223. self.fileType = .PDF
  224. } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
  225. self.fileType = .Image
  226. } else {
  227. self.fileType = .Unknow
  228. }
  229. }
  230. init(filePath: String, type: KMBatchOperationType) {
  231. super.init()
  232. self.filePath = filePath
  233. self.currentOperateType = type
  234. let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, filePath.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
  235. let TypeCFString = UTTypeString as CFString?
  236. if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
  237. self.fileType = .PDF
  238. } else if UTTypeConformsTo(TypeCFString!, kUTTypeImage) {
  239. self.fileType = .Image
  240. } else {
  241. self.fileType = .Unknow
  242. }
  243. }
  244. override func isEqual(_ other: Any?) -> Bool {
  245. if let other = other as? Self {
  246. if self.filePath == other.filePath {
  247. return true
  248. }
  249. return false
  250. }
  251. return false
  252. }
  253. func pagesArrayIntersect(with anArray: [Any]) -> [Any] {
  254. let document = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
  255. document?.unlock(withPassword: self.password)
  256. var pageNumbersArr = [Any]()
  257. let count: Int = Int(document?.pageCount ?? 0)
  258. if count > 0 {
  259. for j in 0..<count {
  260. pageNumbersArr.append(j+1)
  261. }
  262. }
  263. let set1 = NSMutableSet(array: anArray)
  264. let set2 = NSMutableSet(array: pageNumbersArr)
  265. set1.intersect(set2 as! Set<AnyHashable>)
  266. let setArray = set1.allObjects as [Any]
  267. let sortedArray = setArray.sorted { (obj1, obj2) -> Bool in
  268. let number1 = obj1 as? NSNumber ?? NSNumber()
  269. let number2 = obj2 as? NSNumber ?? NSNumber()
  270. return number1.compare(number2) == .orderedAscending
  271. }
  272. return sortedArray
  273. }
  274. var currentConvertParameter: KMBatchConvertParameter? {
  275. if self.convertType == .WordStandard {
  276. return self.standardWordParameter
  277. } else if self.convertType == .WordAdvance {
  278. return self.advanceWordParameter
  279. } else if self.convertType == .Excel {
  280. return self.excelParameter
  281. } else if self.convertType == .PowerPoint {
  282. return self.PPTParameter
  283. } else if self.convertType == .RTF {
  284. return self.RTFParameter
  285. } else if self.convertType == .CSV {
  286. return self.CSVParameter
  287. } else if self.convertType == .HTML {
  288. return self.HTMLParameter
  289. } else if self.convertType == .Text {
  290. return self.TXTParameter
  291. } else if self.convertType == .JPEG {
  292. return self.JPEGParameter
  293. } else if self.convertType == .JPG {
  294. return self.JPGParameter
  295. } else if self.convertType == .PNG {
  296. return self.PNGParameter
  297. } else if self.convertType == .GIF {
  298. return self.GIFParameter
  299. } else if self.convertType == .TIFF {
  300. return self.TIFFParameter
  301. } else if self.convertType == .TGA {
  302. return self.TGAParameter
  303. } else if self.convertType == .BMP {
  304. return self.BMPParameter
  305. } else if self.convertType == .JPEG2000 {
  306. return self.JPEG2000Parameter
  307. }
  308. return nil
  309. }
  310. func fileSizeString(fSize: Float) -> String {
  311. let fileSize = fSize / 1024
  312. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024.0 : fileSize/1048576.0) : fileSize
  313. let unit: String = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  314. return String(format: "%0.1f ", size) + unit
  315. }
  316. }