KMCompressOperation.swift 4.4 KB

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