KMCompressOperation.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // File.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/30.
  6. //
  7. import Foundation
  8. typealias compressCallbackBlock = (_ isSuccess: Bool) -> Void
  9. class KMCompressOperation: KMBatchOperation{
  10. var dpiValue: NSNumber = 0
  11. var pdfDocument: CPDFDocument?
  12. init(file: KMBatchOperateFile, compressValue dpiType: NSNumber) {
  13. super.init(file: file)
  14. self.dpiValue = dpiType
  15. }
  16. override func start() {
  17. if !self.isCancelled {
  18. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
  19. if let data = self.pdfDocument?.isLocked, data {
  20. self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
  21. }
  22. self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.compressInfo)
  23. willChangeValue(forKey: "isExecuting")
  24. self.hasExcuting = true
  25. didChangeValue(forKey: "isExecuting")
  26. if !FileManager.default.fileExists(atPath: self.operateFile?.filePath ?? "") {
  27. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist")), info: self.operateFile!.removePasswordInfo)
  28. self.willChangeValue(forKey: "isFinished")
  29. self.hasFinished = true
  30. self.didChangeValue(forKey: "isFinished")
  31. return
  32. }
  33. if self.pdfDocument == nil {
  34. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")), info: self.operateFile!.removeWatermarkInfo)
  35. self.willChangeValue(forKey: "isFinished")
  36. self.hasFinished = true
  37. self.didChangeValue(forKey: "isFinished")
  38. return
  39. }
  40. self.compressToPath(targetPath: (self.operateFile?.compressInfo.fetchDestinationFilepath()), documentPath: self.operateFile?.filePath ?? "", password: self.operateFile?.password ?? "", compressType: self.dpiValue) { isSuccess in
  41. if isSuccess {
  42. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.compressInfo)
  43. }else{
  44. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed")), info: self.operateFile!.compressInfo)
  45. }
  46. }
  47. DispatchQueue.main.async {
  48. if self.viewController?.view.window?.isVisible == true {
  49. self.willChangeValue(forKey: "isFinished")
  50. self.hasFinished = true
  51. self.didChangeValue(forKey: "isFinished")
  52. }
  53. }
  54. }else {
  55. DispatchQueue.main.async {
  56. if self.viewController?.view.window?.isVisible == true {
  57. self.willChangeValue(forKey: "isFinished")
  58. self.willChangeValue(forKey: "isExecuting")
  59. self.hasExcuting = false
  60. self.hasFinished = true
  61. self.didChangeValue(forKey: "isExecuting")
  62. self.didChangeValue(forKey: "isFinished")
  63. }
  64. }
  65. }
  66. }
  67. override func cancel() {
  68. // super.cancel()
  69. if isExecuting {
  70. self.operateFile?.compressInfo.status = .Waiting
  71. if FileManager.default.fileExists(atPath: (operateFile?.compressInfo.outPutPath)!) { try? FileManager.default.removeItem(atPath: (operateFile!.compressInfo.outPutPath)!)
  72. }
  73. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.compressInfo)
  74. DispatchQueue.main.async {
  75. if self.viewController?.view.window?.isVisible == true {
  76. self.willChangeValue(forKey: "isFinished")
  77. self.hasFinished = true
  78. self.didChangeValue(forKey: "isFinished")
  79. }
  80. }
  81. } else {
  82. DispatchQueue.main.async {
  83. if self.viewController?.view.window?.isVisible == true {
  84. self.willChangeValue(forKey: "isCancelled")
  85. self.hasCanceled = true
  86. self.didChangeValue(forKey: "isCancelled")
  87. }
  88. }
  89. }
  90. }
  91. func compressToPath(targetPath: String?, documentPath: String, password: String, compressType: NSNumber?, completeHandler: compressCallbackBlock) {
  92. if self.pdfDocument!.isLocked {
  93. let unlockSuccess = self.pdfDocument!.unlock(withPassword: password)
  94. if !unlockSuccess {
  95. completeHandler(false)
  96. return
  97. }
  98. }
  99. if compressType == nil {
  100. completeHandler(false)
  101. return
  102. }
  103. let isSuccessfully = self.pdfDocument!.writeOptimize(to: URL(fileURLWithPath: targetPath ?? ""), withOptions: [CPDFDocumentOptimizeOption.imageQualityOption: compressType as Any])
  104. completeHandler(isSuccessfully)
  105. }
  106. }