KMCompressWIndowControllerNew.swift 5.9 KB

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