KMBatchOperateFile.swift 11 KB

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