KMCompressWIndowControllerNew.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: NSWindowController {
  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) { [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 = { [unowned self] view in
  45. // self.closeWindow()
  46. guard let callBack = itemClick else { return }
  47. callBack()
  48. }
  49. self.compressView.batchAction = { [unowned self] view in
  50. print("Batch 按钮点击")
  51. guard let callBack = batchAction else { return }
  52. callBack(self, [documentURL])
  53. }
  54. self.compressView.compressAction = { [unowned self] view, value in
  55. self.compressButtonAction(value: value)
  56. }
  57. }
  58. private func compressButtonAction(value: Int, limit: Bool = false) {
  59. DispatchQueue.main.async {
  60. NSPanel.savePanel(self.window!, true) { panel in
  61. var url: URL = self.documentURL
  62. if (self.oriDocumentUrl != nil) {
  63. url = self.oriDocumentUrl!
  64. }
  65. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_Compressed"
  66. panel.allowedFileTypes = ["pdf"]
  67. } completion: { response, url, isOpen in
  68. if (response == .cancel) {
  69. return
  70. }
  71. self.beginLoading()
  72. DispatchQueue.global().async {
  73. let docuemt = CPDFDocument.init(url: self.documentURL)
  74. if (docuemt?.isLocked)! && self.password != nil {
  75. docuemt?.unlock(withPassword: self.password)
  76. }
  77. let option = value
  78. var result = false
  79. if (limit) {
  80. if let _document = docuemt, let _ = KMTools.saveWatermarkDocumentForCompress(document: _document, to: url!, imageQuality: option) {
  81. result = true
  82. }
  83. } else {
  84. if let data = docuemt?.writeOptimize(to: url, withOptions: [.imageQualityOption : option]) {
  85. result = data
  86. }
  87. }
  88. if (result) {
  89. self._clearData()
  90. }
  91. DispatchQueue.main.async {
  92. self.endLoading()
  93. guard let callback = self.resultCallback else {
  94. if (result) {
  95. if isOpen { /// 开启文档
  96. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  97. }
  98. } else {
  99. NSWorkspace.shared.activateFileViewerSelecting([url!])
  100. }
  101. }
  102. return
  103. }
  104. callback(result, isOpen, url!, "")
  105. }
  106. }
  107. }
  108. }
  109. }
  110. private func _clearData() {
  111. if let _ = self.oriDocumentUrl {
  112. if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
  113. try?FileManager.default.removeItem(atPath: data)
  114. }
  115. }
  116. }
  117. func closeWindow() {
  118. self.close()
  119. }
  120. }
  121. extension KMCompressWIndowControllerNew: KMLoadingProtocol {
  122. func beginLoading() {
  123. self.window?.contentView?.beginLoading()
  124. }
  125. func endLoading() {
  126. self.window?.contentView?.endLoading()
  127. }
  128. }