KMCompressOperation.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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", nil)), 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.", nil)), 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", nil)), info: self.operateFile!.compressInfo)
  45. }
  46. }
  47. if self.viewController?.view.window?.isVisible == true {
  48. self.willChangeValue(forKey: "isFinished")
  49. self.hasFinished = true
  50. self.didChangeValue(forKey: "isFinished")
  51. }
  52. }else {
  53. if self.viewController?.view.window?.isVisible == true {
  54. willChangeValue(forKey: "isFinished")
  55. willChangeValue(forKey: "isExecuting")
  56. hasExcuting = false
  57. hasFinished = true
  58. didChangeValue(forKey: "isExecuting")
  59. didChangeValue(forKey: "isFinished")
  60. }
  61. }
  62. }
  63. override func cancel() {
  64. // super.cancel()
  65. if isExecuting {
  66. self.operateFile?.compressInfo.status = .Waiting
  67. if FileManager.default.fileExists(atPath: (operateFile?.compressInfo.outPutPath)!) { try? FileManager.default.removeItem(atPath: (operateFile!.compressInfo.outPutPath)!)
  68. }
  69. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.compressInfo)
  70. if self.viewController?.view.window?.isVisible == true {
  71. willChangeValue(forKey: "isFinished")
  72. hasFinished = true
  73. didChangeValue(forKey: "isFinished")
  74. }
  75. } else {
  76. if self.viewController?.view.window?.isVisible == true {
  77. willChangeValue(forKey: "isCancelled")
  78. hasCanceled = true
  79. didChangeValue(forKey: "isCancelled")
  80. }
  81. }
  82. }
  83. func compressToPath(targetPath: String?, documentPath: String, password: String, compressType: NSNumber?, completeHandler: compressCallbackBlock) {
  84. if self.pdfDocument!.isLocked {
  85. let unlockSuccess = self.pdfDocument!.unlock(withPassword: password)
  86. if !unlockSuccess {
  87. completeHandler(false)
  88. return
  89. }
  90. }
  91. if compressType == nil {
  92. completeHandler(false)
  93. return
  94. }
  95. let isSuccessfully = self.pdfDocument!.writeOptimize(to: URL(fileURLWithPath: targetPath ?? ""), withOptions: [CPDFDocumentOptimizeOption.imageQualityOption: compressType as Any])
  96. completeHandler(isSuccessfully)
  97. }
  98. }