KMBatchAddBackgroundOperation.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // KMBatchAddBackgroundOperation.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/12/11.
  6. //
  7. import Foundation
  8. class KMBatchAddBackgroundOperation: KMBatchOperation{
  9. var background: KMBackgroundModel?
  10. var pdfDocument: CPDFDocument!
  11. var password: String = ""
  12. init(file: KMBatchOperateFile, backgroundM: KMBackgroundModel) {
  13. super.init(file: file)
  14. self.background = backgroundM
  15. self.password = file.password
  16. }
  17. override func start() {
  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. if !self.isCancelled {
  23. self.delegate?.fileBeginOperate?(self.operateFile!, info: self.operateFile!.addBackgroundInfo)
  24. willChangeValue(forKey: "isExecuting")
  25. self.hasExcuting = true
  26. didChangeValue(forKey: "isExecuting")
  27. let newArr = NSMutableArray()
  28. for i in 0..<(self.operateFile?.addBackgroundInfo.pagesArray?.count ?? 0) {
  29. let number = self.operateFile?.addBackgroundInfo.pagesArray![i]
  30. newArr.add(number!.intValue - 1)
  31. }
  32. if !FileManager.default.fileExists(atPath: self.operateFile!.filePath) {
  33. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("File Not Exist")), info: self.operateFile!.addBackgroundInfo)
  34. self.willChangeValue(forKey: "isFinished")
  35. self.hasFinished = true
  36. self.didChangeValue(forKey: "isFinished")
  37. return
  38. }
  39. if self.pdfDocument == nil {
  40. 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!.addBackgroundInfo)
  41. self.willChangeValue(forKey: "isFinished")
  42. self.hasFinished = true
  43. self.didChangeValue(forKey: "isFinished")
  44. return
  45. }
  46. if !self.pdfDocument!.allowsPrinting || !self.pdfDocument!.allowsCopying {
  47. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("This is a secured document. Editing is not permitted.")), info: self.operateFile!.addBackgroundInfo)
  48. self.willChangeValue(forKey: "isFinished")
  49. self.hasFinished = true
  50. self.didChangeValue(forKey: "isFinished")
  51. return
  52. }
  53. if newArr.count < 1 {
  54. let err = NSError(domain: "LocalError", code: 0, userInfo: [NSLocalizedDescriptionKey: KMLocalizedString("Invalid page range or the page number is out of range. Please try again.")])
  55. self.delegate?.fileOperateFailed?(self.operateFile!, error: err, info: self.operateFile!.addBackgroundInfo)
  56. self.willChangeValue(forKey: "isFinished")
  57. self.hasFinished = true
  58. self.didChangeValue(forKey: "isFinished")
  59. }
  60. self.saveAsPDF(background: self.background!, path: (self.operateFile?.addBackgroundInfo.fetchDestinationFilepath())!)
  61. }else {
  62. willChangeValue(forKey: "isFinished")
  63. willChangeValue(forKey: "isExecuting")
  64. hasExcuting = false
  65. hasFinished = true
  66. didChangeValue(forKey: "isExecuting")
  67. didChangeValue(forKey: "isFinished")
  68. }
  69. }
  70. func saveAsPDF(background: KMBackgroundModel, path: String) {
  71. var filePath = self.pdfDocument?.documentURL?.path
  72. let password = self.password
  73. if filePath?.count ?? 0 < 1 {
  74. let str = String(format: "%@.pdf", KMLocalizedString("Untitled"))
  75. let writeSuccess = self.pdfDocument!.write(to: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  76. if writeSuccess {
  77. let newDocument: CPDFDocument = CPDFDocument(url: URL(fileURLWithPath: (kTempSavePath?.stringByAppendingPathComponent(str))!))
  78. filePath = newDocument.documentURL?.path
  79. } else {
  80. NSSound.beep()
  81. return
  82. }
  83. }
  84. guard let document = CPDFDocument(url: URL(fileURLWithPath: filePath!)) else {
  85. return
  86. }
  87. if password.count > 0 {
  88. document.unlock(withPassword: password)
  89. }
  90. let tBackground: CPDFBackground = document.background()
  91. tBackground.opacity = background.opacity
  92. tBackground.scale = background.scale
  93. tBackground.rotation = CGFloat(-background.rotation)
  94. tBackground.horizontalAlignment = UInt(background.horizontalMode)
  95. tBackground.verticalAlignment = UInt(background.verticalMode)
  96. tBackground.xOffset = background.horizontalSpace
  97. tBackground.yOffset = background.verticalSpace
  98. tBackground.update()
  99. // Save to temporary path
  100. let documentPath = NSTemporaryDirectory()
  101. let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
  102. try? FileManager.default.removeItem(atPath: tempPath)
  103. let result = document.write(to: URL(fileURLWithPath: tempPath))
  104. if result {
  105. if FileManager.default.fileExists(atPath: path) {
  106. try? FileManager.default.removeItem(atPath: path)
  107. }
  108. try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
  109. } else {
  110. try? FileManager.default.removeItem(atPath: tempPath)
  111. }
  112. if result {
  113. self.delegate?.fileOperateSuccessed?(self.operateFile!, info: self.operateFile!.addBackgroundInfo)
  114. }else {
  115. self.delegate?.fileOperateFailed?(self.operateFile!, error: self.errorWithMsg(KMLocalizedString("Failed")), info: self.operateFile!.addBackgroundInfo)
  116. }
  117. self.willChangeValue(forKey: "isFinished")
  118. self.hasFinished = true
  119. self.didChangeValue(forKey: "isFinished")
  120. }
  121. override func cancel() {
  122. // super.cancel()
  123. if isExecuting {
  124. operateFile!.removeWatermarkInfo.status = .Waiting
  125. if FileManager.default.fileExists(atPath: (self.operateFile?.addBackgroundInfo.outPutPath)!) { try? FileManager.default.removeItem(atPath: (self.operateFile?.addBackgroundInfo.outPutPath)!)
  126. }
  127. self.delegate?.fileOperateCanceled?(self.operateFile!, info: self.operateFile!.addBackgroundInfo)
  128. willChangeValue(forKey: "isFinished")
  129. hasFinished = true
  130. didChangeValue(forKey: "isFinished")
  131. } else {
  132. willChangeValue(forKey: "isCancelled")
  133. hasCanceled = true
  134. didChangeValue(forKey: "isCancelled")
  135. }
  136. }
  137. }