KMBatchRemoveBackgroundOperation.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // KMBatchRemoveBackgroundOperation.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/1.
  6. //
  7. import Foundation
  8. class KMBatchRemoveBackgroundOperation: KMBatchOperation{
  9. var pdfDocument: CPDFDocument?
  10. override init(file: KMBatchOperateFile) {
  11. super.init(file: file)
  12. }
  13. override func start() {
  14. self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
  15. if let data = self.pdfDocument?.isLocked, data {
  16. self.pdfDocument?.unlock(withPassword: self.operateFile?.password)
  17. }
  18. if !self.isCancelled {
  19. self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
  20. willChangeValue(forKey: "isExecuting")
  21. self.hasExcuting = true
  22. didChangeValue(forKey: "isExecuting")
  23. if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
  24. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist", nil)), info: self.operateFile!.removeBackgroundInfo)
  25. self.willChangeValue(forKey: "isFinished")
  26. self.hasFinished = true
  27. self.didChangeValue(forKey: "isFinished")
  28. return
  29. }
  30. if self.pdfDocument == nil {
  31. 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!.removeBackgroundInfo)
  32. self.willChangeValue(forKey: "isFinished")
  33. self.hasFinished = true
  34. self.didChangeValue(forKey: "isFinished")
  35. return
  36. }
  37. if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying {
  38. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.", nil)), info: self.operateFile!.removeBackgroundInfo)
  39. self.willChangeValue(forKey: "isFinished")
  40. self.hasFinished = true
  41. self.didChangeValue(forKey: "isFinished")
  42. return
  43. }
  44. KMBatchRemoveBackgroundOperation.saveAsPDFRemoveAllBackground(self.pdfDocument!, password: self.operateFile?.password, toPath: self.operateFile!.removeBackgroundInfo.fetchDestinationFilepath()!) { result in
  45. if result == 1 {
  46. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
  47. } else {
  48. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed", nil)) as NSError, info: self.operateFile!.removeBackgroundInfo)
  49. }
  50. self.willChangeValue(forKey: "isFinished")
  51. self.hasFinished = true
  52. self.didChangeValue(forKey: "isFinished")
  53. }
  54. }else {
  55. willChangeValue(forKey: "isFinished")
  56. willChangeValue(forKey: "isExecuting")
  57. hasExcuting = false
  58. hasFinished = true
  59. didChangeValue(forKey: "isExecuting")
  60. didChangeValue(forKey: "isFinished")
  61. }
  62. }
  63. override func cancel() {
  64. // super.cancel()
  65. if isExecuting {
  66. operateFile!.removeBackgroundInfo.status = .Waiting
  67. if FileManager.default.fileExists(atPath: operateFile!.removeBackgroundInfo.outPutPath!) {
  68. try? FileManager.default.removeItem(atPath: operateFile!.removeBackgroundInfo.outPutPath!)
  69. }
  70. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.removeBackgroundInfo)
  71. willChangeValue(forKey: "isFinished")
  72. hasFinished = true
  73. didChangeValue(forKey: "isFinished")
  74. } else {
  75. willChangeValue(forKey: "isCancelled")
  76. hasCanceled = true
  77. didChangeValue(forKey: "isCancelled")
  78. }
  79. }
  80. func defaultError() -> Error {
  81. let errorDict: [String: Any] = [NSLocalizedDescriptionKey: NSLocalizedString("Failed", comment: "")]
  82. let error = NSError(domain: "PDFReaderBatchErrorDomain", code: -1, userInfo: errorDict)
  83. return error
  84. }
  85. class func saveAsPDFRemoveAllBackground(_ pdfDocument: CPDFDocument, password: String?, toPath path: String, completionHandler handler: @escaping (Int) -> Void) {
  86. let document = CPDFDocument(url: pdfDocument.documentURL)
  87. if let password = password {
  88. document?.unlock(withPassword: password)
  89. }
  90. let tBackground = document?.background()
  91. tBackground?.clear()
  92. let documentPath = NSTemporaryDirectory()
  93. let tempPath = documentPath.stringByAppendingPathComponent(path.lastPathComponent)
  94. if FileManager.default.fileExists(atPath: tempPath) {
  95. try?FileManager.default.removeItem(atPath: tempPath)
  96. }
  97. let result = document?.write(to: URL(fileURLWithPath: tempPath))
  98. if result ?? false {
  99. if FileManager.default.fileExists(atPath: path) {
  100. try?FileManager.default.removeItem(atPath: path)
  101. }
  102. try?FileManager.default.moveItem(atPath: tempPath, toPath: path)
  103. } else {
  104. try?FileManager.default.removeItem(atPath: tempPath)
  105. }
  106. DispatchQueue.main.async {
  107. if let result = result {
  108. handler(result ? 1 : 0)
  109. }
  110. }
  111. }
  112. }