Browse Source

【综合】合并补充加密文档逻辑

tangchao 1 year ago
parent
commit
e8ba6a6718

+ 16 - 0
PDF Office/PDF Master.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -356,5 +356,21 @@
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            uuid = "BA23229A-725E-418E-B150-169E35516026"
+            shouldBeEnabled = "Yes"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "PDF Master/Class/Batch/WindowController/PasswordWindowController.swift"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "48"
+            endingLineNumber = "48"
+            landmarkName = "cancelAction(_:)"
+            landmarkType = "7">
+         </BreakpointContent>
+      </BreakpointProxy>
    </Breakpoints>
 </Bucket>

+ 1 - 3
PDF Office/PDF Master/Class/Batch/WindowController/PasswordWindowController.swift

@@ -24,14 +24,12 @@ protocol PasswordWindowDelegate: AnyObject {
     @IBOutlet var passwordTextField: NSSecureTextField!
     
     @IBOutlet var titleTextField: NSTextField!
-    
     @IBOutlet var textField: NSTextField!
-    
     @IBOutlet var openButton: NSButton!
     @IBOutlet var cancelButton: NSButton!
+    
     override init(window: NSWindow?) {
         super.init(window: window)
-        
     }
     
     required init?(coder: NSCoder) {

+ 29 - 18
PDF Office/PDF Master/Class/Common/Base/KMBaseWindowController.swift

@@ -31,27 +31,38 @@ class KMBaseWindowController: NSWindowController {
 
 extension KMBaseWindowController {
     static func checkPassword(url: URL, password: String = "", completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
-        let document = CPDFDocument.init(url: url)
-        if document!.isLocked {
-            if (document?.unlock(withPassword: password)) != nil {
+        // 判断路径 + document
+        guard let document = CPDFDocument.init(url: url) else {
+            return completion(false, "")
+        }
+        // 判断是否为加密文档
+        if document.isLocked == false {
+            completion(true, "")
+            return
+        }
+        // 加密文件,尝试解锁
+        if password.isEmpty == false {
+            let preStatus = document.permissionsStatus
+            document.unlock(withPassword: password)
+            if document.permissionsStatus.rawValue > preStatus.rawValue { // 解密成功
                 completion(true, password)
-            } else {
-                DispatchQueue.main.async {
-                    let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
-                    passwordWindowController.fileURL = url
-                    NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
-                    passwordWindowController.closeCallBack = { passwordString in
-                        if passwordString.count != 0 {
-                            document?.unlock(withPassword: passwordString)
-                            completion(true, passwordString)
-                        } else {
-                            completion(false, "")
-                        }
-                    }
+                return
+            }
+        }
+        
+        // 弹密码弹窗
+        Task { @MainActor in
+            let passwordWindowController = PasswordWindowController(windowNibName: "PasswordWindowController")
+            passwordWindowController.fileURL = url
+            NSWindow.currentWindow().km_beginSheet(windowC: passwordWindowController)
+            passwordWindowController.closeCallBack = { passwordString in
+                if passwordString.count != 0 {
+                    document.unlock(withPassword: passwordString)
+                    completion(true, passwordString)
+                } else {
+                    completion(false, "")
                 }
             }
-        } else {
-            completion(true, "")
         }
     }
 }

+ 6 - 0
PDF Office/PDF Master/Class/Common/Category/NSWindowController+KMExtension.swift

@@ -12,3 +12,9 @@ extension NSWindowController {
         return self.window
     }
 }
+
+extension NSWindow {
+    override func km_window() -> NSWindow? {
+        return self
+    }
+}

+ 1 - 1
PDF Office/PDF Master/Class/PDFTools/Merge/MergeNew/View/KMMergeView.swift

@@ -404,7 +404,7 @@ extension KMMergeView {
     func openPasswordFile(completion: @escaping ((_ success: Bool, _ resultPassword: String) -> Void)) {
         if lockFiles.count != 0 {
             let file = lockFiles[lockFilesIndex]
-            KMBaseWindowController.checkPassword(url: URL(string: file.filePath)!) { [unowned self] success, resultPassword in
+            KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: file.filePath)) { [unowned self] success, resultPassword in
                 if success {
                     file.password = resultPassword
                     lockFilesIndex = lockFilesIndex + 1