KMCompressWIndowControllerNew.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KMCompressWIndowControllerNew.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/8.
  6. //
  7. import Cocoa
  8. typealias KMCompressWIndowControllerNewBatchAction = (_ view: KMCompressWIndowControllerNew, _ filePaths: [URL]) -> Void
  9. typealias KMCompressWIndowControllerNewItemClick = () -> Void
  10. typealias KMCompressWIndowControllerNewResultCallback = (_ result: Bool,_ openDocuemt: Bool, _ fileURL: URL, _ error: String)->()
  11. class KMCompressWIndowControllerNew: KMBaseWindowController {
  12. @IBOutlet weak var compressView: KMCompressView!
  13. var resultCallback: KMCompressWIndowControllerNewResultCallback!
  14. var itemClick: KMCompressWIndowControllerNewItemClick?
  15. var batchAction: KMCompressWIndowControllerNewBatchAction?
  16. var documentURL: URL! {
  17. didSet{
  18. if self.password.count != 0 {
  19. self.compressView.documentURL = self.documentURL
  20. } else {
  21. KMBaseWindowController.checkPassword(url: documentURL, type: .owner) { [unowned self] success, paasswordString in
  22. if success {
  23. self.password = paasswordString
  24. self.compressView.documentURL = self.documentURL
  25. } else {
  26. guard let callBack = self.itemClick else { return }
  27. callBack()
  28. }
  29. }
  30. }
  31. }
  32. }
  33. var password: String = "" {
  34. didSet {
  35. self.compressView.password = password
  36. }
  37. }
  38. private var datas: [String] = []
  39. var limit = true
  40. var oriDocumentUrl: URL?
  41. override func windowDidLoad() {
  42. super.windowDidLoad()
  43. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  44. self.compressView.cancelAction = { [weak self] view in
  45. // self.closeWindow()
  46. guard let callBack = self?.itemClick else { return }
  47. callBack()
  48. }
  49. self.compressView.batchAction = { [unowned self] view in
  50. print("Batch 按钮点击")
  51. if KMMemberInfo.shared.isMemberAllFunction == false {
  52. KMMemberInfo.shared.advancedFunctionUsage()
  53. return
  54. }
  55. guard let callBack = batchAction else { return }
  56. callBack(self, [documentURL])
  57. }
  58. self.compressView.compressAction = { [weak self] view, value in
  59. self?.compressButtonAction(value: value)
  60. }
  61. }
  62. private func compressButtonAction(value: Int, limit: Bool = false) {
  63. DispatchQueue.main.async {
  64. NSPanel.savePanel(self.window!, true) { panel in
  65. var url: URL = self.documentURL
  66. if (self.oriDocumentUrl != nil) {
  67. url = self.oriDocumentUrl!
  68. }
  69. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_Compressed"
  70. panel.allowedFileTypes = ["pdf"]
  71. } completion: { response, url, isOpen in
  72. if (response == .cancel) {
  73. return
  74. }
  75. self.beginLoading()
  76. DispatchQueue.global().async {
  77. let docuemt = CPDFDocument.init(url: self.documentURL)
  78. if (docuemt?.isLocked)! && self.password != nil {
  79. docuemt?.unlock(withPassword: self.password)
  80. }
  81. let option = value
  82. var result = false
  83. if (limit) {
  84. if let _document = docuemt, let _ = KMTools.saveWatermarkDocumentForCompress(document: _document, to: url!, imageQuality: option) {
  85. result = true
  86. }
  87. } else {
  88. if let data = docuemt?.writeOptimize(to: url, withOptions: [.imageQualityOption : option]) {
  89. result = data
  90. }
  91. }
  92. if (result) {
  93. self._clearData()
  94. }
  95. DispatchQueue.main.async {
  96. self.endLoading()
  97. guard let callback = self.resultCallback else {
  98. if (result) {
  99. if isOpen { /// 开启文档
  100. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  101. }
  102. } else {
  103. NSWorkspace.shared.activateFileViewerSelecting([url!])
  104. }
  105. }
  106. return
  107. }
  108. callback(result, isOpen, url!, "")
  109. }
  110. }
  111. }
  112. }
  113. }
  114. private func _clearData() {
  115. if let _ = self.oriDocumentUrl {
  116. if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
  117. try?FileManager.default.removeItem(atPath: data)
  118. }
  119. }
  120. }
  121. func closeWindow() {
  122. self.close()
  123. }
  124. }
  125. extension KMCompressWIndowControllerNew: KMLoadingProtocol {
  126. func beginLoading() {
  127. self.window?.contentView?.beginLoading()
  128. }
  129. func endLoading() {
  130. self.window?.contentView?.endLoading()
  131. }
  132. }