Browse Source

综合 - 修复多选开启文档崩溃

wanjun 1 year ago
parent
commit
5ffaac4e5b

+ 8 - 0
PDF Office/PDF Master/Class/Document/KMDocumentController.swift

@@ -86,6 +86,10 @@ class KMDocumentController: NSDocumentController {
         pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
         pdfDocument?.write(to: URL(fileURLWithPath: savePath))
         NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: savePath), display: true) { document, documentWasAlreadyOpen, error in
+            if error != nil {
+                NSApp.presentError(error!)
+                return
+            }
             if document is KMMainDocument {
                 let newDocument = document
                 (newDocument as! KMMainDocument).isNewCreated = false
@@ -127,6 +131,10 @@ class KMDocumentController: NSDocumentController {
         windowController.beginSheetModal(for: NSApp.mainWindow!) { filePath in
             if filePath != nil && FileManager.default.fileExists(atPath: filePath) {
                 NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filePath), display: true) { document, documentWasAlreadyOpen, error in
+                    if error != nil {
+                        NSApp.presentError(error!)
+                        return
+                    }
                     if document is KMMainDocument {
                         (document as! KMMainDocument).isNewCreated = true
                     }

+ 8 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMCloudDocumentsViewController.swift

@@ -383,6 +383,10 @@ class KMCloudDocumentsViewController: NSViewController {
                     return
                 }
                 NSDocumentController.shared.openDocument(withContentsOf: fileUrl, display: true) { document, result, error in
+                    if error != nil {
+                        NSApp.presentError(error!)
+                        return
+                    }
                     (document as! KMMainDocument).cloud = true
                     (document as! KMMainDocument).cloudUploadHanddler = { callback in
                         KMCloudDocumentTools.uploadDocument(self.currentAccount!, file) { finished, fileUrl in
@@ -420,6 +424,10 @@ class KMCloudDocumentsViewController: NSViewController {
                     self.fileListEndLoading()
                     if (finished) {
                         NSDocumentController.shared.openDocument(withContentsOf: fileUrl, display: true) { document, result, error in
+                            if error != nil {
+                                NSApp.presentError(error!)
+                                return
+                            }
                             (document as! KMMainDocument).cloud = true
                             (document as! KMMainDocument).cloudUploadHanddler = { callback in
                                 KMCloudDocumentTools.uploadDocument(self.currentAccount!, file) { finished, fileUrl in

+ 36 - 15
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -395,14 +395,21 @@ extension KMHomeViewController {
                                     selectDocument?.browser.window.orderFront(nil)
                                 }
                             } else {
-                                NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
-                                    if error != nil {
-                                        NSApp.presentError(error!)
-                                        return
-                                    }
+                                if !url.path.isPDFValid() {
+                                    let alert = NSAlert()
+                                    alert.alertStyle = .critical
+                                    alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
+                                    alert.runModal()
+                                } else {
+                                    NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
+                                        if error != nil {
+                                            NSApp.presentError(error!)
+                                            return
+                                        }
 
-//                                    (document as! KMMainDocument).mainViewController.listView.document.unlock(withPassword: password)
-                                    (document as! KMMainDocument).mainViewController?.password = password
+    //                                    (document as! KMMainDocument).mainViewController.listView.document.unlock(withPassword: password)
+                                        (document as! KMMainDocument).mainViewController?.password = password
+                                    }
                                 }
                             }
                         }
@@ -422,10 +429,17 @@ extension KMHomeViewController {
                                 selectDocument?.browser.window.orderFront(nil)
                             }
                         } else {
-                            NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
-                                if error != nil {
-                                    NSApp.presentError(error!)
-                                    return
+                            if !url.path.isPDFValid() {
+                                let alert = NSAlert()
+                                alert.alertStyle = .critical
+                                alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
+                                alert.runModal()
+                            } else {
+                                NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
+                                    if error != nil {
+                                        NSApp.presentError(error!)
+                                        return
+                                    }
                                 }
                             }
                         }
@@ -446,10 +460,17 @@ extension KMHomeViewController {
                             }
                         }
                     } else {
-                        NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
-                            if error != nil {
-                                NSApp.presentError(error!)
-                                return
+                        if !url.path.isPDFValid() {
+                            let alert = NSAlert()
+                            alert.alertStyle = .critical
+                            alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
+                            alert.runModal()
+                        } else {
+                            NSDocumentController.shared.openDocument(withContentsOf: url, display: true) { document, documentWasAlreadyOpen, error in
+                                if error != nil {
+                                    NSApp.presentError(error!)
+                                    return
+                                }
                             }
                         }
                     }