KMCompressWIndowControllerNew.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // KMCompressWIndowControllerNew.swift
  3. // PDF Master
  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. KMBaseWindowController.checkPassword(url: documentURL) { [unowned self] success, paasswordString in
  19. if success {
  20. self.password = paasswordString
  21. self.compressView.documentURL = self.documentURL
  22. } else {
  23. guard let callBack = self.itemClick else { return }
  24. callBack()
  25. }
  26. }
  27. }
  28. }
  29. var password: String!
  30. private var datas: [String] = []
  31. var limit = true
  32. var oriDocumentUrl: URL?
  33. override func windowDidLoad() {
  34. super.windowDidLoad()
  35. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  36. self.compressView.cancelAction = { [unowned self] view in
  37. // self.closeWindow()
  38. guard let callBack = itemClick else { return }
  39. callBack()
  40. }
  41. self.compressView.batchAction = { [unowned self] view in
  42. print("Batch 按钮点击")
  43. guard let callBack = batchAction else { return }
  44. callBack(self, [documentURL])
  45. }
  46. self.compressView.compressAction = { [unowned self] view, value in
  47. self.compressButtonAction(value: value)
  48. }
  49. }
  50. private func compressButtonAction(value: Int, limit: Bool = false) {
  51. DispatchQueue.main.async {
  52. NSPanel.savePanel(self.window!, true) { panel in
  53. var url: URL = self.documentURL
  54. if (self.oriDocumentUrl != nil) {
  55. url = self.oriDocumentUrl!
  56. }
  57. panel.nameFieldStringValue = ""+url.deletingPathExtension().lastPathComponent+"_Compressed"
  58. panel.allowedFileTypes = ["pdf"]
  59. } completion: { response, url, isOpen in
  60. if (response == .cancel) {
  61. return
  62. }
  63. self.beginLoading()
  64. DispatchQueue.global().async {
  65. let docuemt = CPDFDocument.init(url: self.documentURL)
  66. if (docuemt?.isLocked)! && self.password != nil {
  67. docuemt?.unlock(withPassword: self.password)
  68. }
  69. let option = value
  70. var result = false
  71. if (limit) {
  72. if let _document = docuemt, let _ = KMTools.saveWatermarkDocumentForCompress(document: _document, to: url!, imageQuality: option) {
  73. result = true
  74. }
  75. } else {
  76. if let data = docuemt?.writeOptimize(to: url, withOptions: [.imageQualityOption : option]) {
  77. result = data
  78. }
  79. }
  80. if (result) {
  81. self._clearData()
  82. }
  83. DispatchQueue.main.async {
  84. self.endLoading()
  85. guard let callback = self.resultCallback else {
  86. if (result) {
  87. if isOpen { /// 开启文档
  88. NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
  89. }
  90. } else {
  91. NSWorkspace.shared.activateFileViewerSelecting([url!])
  92. }
  93. }
  94. return
  95. }
  96. callback(result, isOpen, url!, "")
  97. }
  98. }
  99. }
  100. }
  101. }
  102. private func _clearData() {
  103. if let _ = self.oriDocumentUrl {
  104. if let data = self.documentURL?.path, FileManager.default.fileExists(atPath: data) {
  105. try?FileManager.default.removeItem(atPath: data)
  106. }
  107. }
  108. }
  109. func closeWindow() {
  110. self.close()
  111. }
  112. }
  113. extension KMCompressWIndowControllerNew: KMLoadingProtocol {
  114. func beginLoading() {
  115. self.window?.contentView?.beginLoading()
  116. }
  117. func endLoading() {
  118. self.window?.contentView?.endLoading()
  119. }
  120. }