Browse Source

【APPCenter】Crash编号:67

liujiajie 10 months ago
parent
commit
4a817f45ed

+ 60 - 39
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2129,8 +2129,12 @@ extension KMMainViewController {
     
     @objc private func shareDocument(sender:KMToolbarViewController, limit: Bool = false) {
         if (limit) {
+            let document = self.listView.document ?? CPDFDocument()
+            if  document?.documentURL == nil {
+                return
+            }
             var doucumentURL : URL = self.listView.document.documentURL
-            if doucumentURL != nil {
+            if doucumentURL.path.count > 0 {
                 let docDir = NSTemporaryDirectory()
                 let documentName : String = doucumentURL.path.lastPathComponent
                 let path = docDir.stringByAppendingPathComponent(documentName)
@@ -2154,8 +2158,13 @@ extension KMMainViewController {
             return
         }
         
+        
+        let document = self.listView.document ?? CPDFDocument()
+        if  document?.documentURL == nil {
+            return
+        }
         var doucumentURL : URL = self.listView.document.documentURL
-        if doucumentURL != nil {
+        if doucumentURL.path.count > 0 {
             let docDir = NSTemporaryDirectory()
             let documentName : String = doucumentURL.path.lastPathComponent
             let path = docDir.stringByAppendingPathComponent(documentName)
@@ -2178,22 +2187,25 @@ extension KMMainViewController {
     @objc private func shareFlatten(sender:KMToolbarViewController, limit: Bool = false) {
         if (limit) {
             let document = self.listView.document ?? CPDFDocument()
-            var path = document!.documentURL.path
-            if path != nil {
+            var path: String?
+            if  document?.documentURL != nil {
+                path = document?.documentURL.path ?? ""
+            }
+            if path?.count ?? 0 > 0 {
                 let docDir = NSTemporaryDirectory()
-                let documentName : String = path.lastPathComponent
+                let documentName : String = path?.lastPathComponent ?? ""
                 path = docDir.stringByAppendingPathComponent(documentName)
             }
-            let pathFolder = path.fileURL.deletingLastPathComponent().path
-            var tfileName = path.deletingPathExtension.lastPathComponent
+            let pathFolder = path?.fileURL.deletingLastPathComponent().path
+            var tfileName = path?.deletingPathExtension.lastPathComponent
             let tStdFileSuffix = "_flatten"
-            tfileName = tfileName + tStdFileSuffix + ".pdf"
-            path = pathFolder + "/" + tfileName
+            tfileName = (tfileName ?? "") + tStdFileSuffix + ".pdf"
+            path = (pathFolder ?? "") + "/" + (tfileName ?? "")
 
-            let data = KMTools.saveWatermarkDocumentForFlatten(document: document!, to: URL(fileURLWithPath: path))
+            let data = KMTools.saveWatermarkDocumentForFlatten(document: document!, to: URL(fileURLWithPath: path ?? ""))
             let success = data != nil
             if success {
-                let url = URL(fileURLWithPath: path)
+                let url = URL(fileURLWithPath: path ?? "")
                 let picker = NSSharingServicePicker.init(items: [url])
                 if sender.shareButton.window != nil {
                     picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
@@ -2205,20 +2217,23 @@ extension KMMainViewController {
         }
         
         let document = self.listView.document ?? CPDFDocument()
-        var path = document!.documentURL.path
-        if path != nil {
+        var path: String?
+        if  document?.documentURL != nil {
+            path = document?.documentURL.path ?? ""
+        }
+        if path?.count ?? 0 > 0 {
             let docDir = NSTemporaryDirectory()
-            let documentName : String = path.lastPathComponent
+            let documentName : String = path?.lastPathComponent ?? ""
             path = docDir.stringByAppendingPathComponent(documentName)
         }
-        let pathFolder = path.fileURL.deletingLastPathComponent().path
-        var tfileName = path.deletingPathExtension.lastPathComponent
+        let pathFolder = path?.fileURL.deletingLastPathComponent().path
+        var tfileName = path?.deletingPathExtension.lastPathComponent
         let tStdFileSuffix = "_flatten"
-        tfileName = tfileName + tStdFileSuffix + ".pdf"
-        path = pathFolder + "/" + tfileName
-        let success : Bool = document!.writeFlatten(to: URL(fileURLWithPath: path))
+        tfileName = (tfileName ?? "") + tStdFileSuffix + ".pdf"
+        path = (pathFolder ?? "") + "/" + (tfileName ?? "")
+        let success : Bool = document?.writeFlatten(to: URL(fileURLWithPath: path ?? "")) ?? false
         if success {
-            let url = URL(fileURLWithPath: path)
+            let url = URL(fileURLWithPath: path ?? "")
             let picker = NSSharingServicePicker.init(items: [url])
             if sender.shareButton.window != nil {
                 picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
@@ -2230,23 +2245,26 @@ extension KMMainViewController {
     @objc private func shareOriginalPDF(sender:KMToolbarViewController, limit: Bool = false) {
         if (limit) {
             let document = self.listView.document ?? CPDFDocument()
-            var path = document!.documentURL.path
-            if path != nil {
+            var path: String?
+            if  document?.documentURL != nil {
+                path = document?.documentURL.path ?? ""
+            }
+            if path?.count ?? 0 > 0{
                 let docDir = NSTemporaryDirectory()
-                let documentName : String = path.lastPathComponent
+                let documentName : String = path?.lastPathComponent ?? ""
                 path = docDir.stringByAppendingPathComponent(documentName)
             }
 //            var writeSuccess = document!.write(to: URL(fileURLWithPath: path))
-            let data = KMTools.saveWatermarkDocument(document: document!, to: URL(fileURLWithPath: path), secureOptions: self.secureOptions, removePWD: self.removeSecureFlag)
+            let data = KMTools.saveWatermarkDocument(document: document!, to: URL(fileURLWithPath: path ?? ""), secureOptions: self.secureOptions, removePWD: self.removeSecureFlag)
             var writeSuccess = data != nil
             if writeSuccess == false {
                 __NSBeep()
                 return;
             }
-            let newDocument = CPDFDocument(url: URL(fileURLWithPath: path))!
+            let newDocument = CPDFDocument(url: URL(fileURLWithPath: path ?? ""))
             
-            for i in 0 ... newDocument.pageCount-1 {
-                let page = newDocument.page(at: i)
+            for i in 0 ... (newDocument?.pageCount ?? 1)-1 {
+                let page = newDocument?.page(at: i)
                 var annotations : [CPDFAnnotation] = []
                 for annotation in page!.annotations {
                     annotations.append(annotation)
@@ -2255,9 +2273,9 @@ extension KMMainViewController {
                     annotation.page.removeAnnotation(annotation)
                 }
             }
-            writeSuccess = newDocument.write(to:URL(fileURLWithPath: path))
+            writeSuccess = newDocument?.write(to:URL(fileURLWithPath: path ?? "")) ?? false
             if writeSuccess {
-                let url = URL(fileURLWithPath: path)
+                let url = URL(fileURLWithPath: path ?? "")
                 let picker = NSSharingServicePicker.init(items: [url])
                 if sender.shareButton.window != nil {
                     picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)
@@ -2269,21 +2287,24 @@ extension KMMainViewController {
         }
         
         let document = self.listView.document ?? CPDFDocument()
-        var path = document!.documentURL.path
-        if path != nil {
+        var path: String?
+        if  document?.documentURL != nil {
+            path = document?.documentURL.path ?? ""
+        }
+        if path?.count ?? 0 > 0 {
             let docDir = NSTemporaryDirectory()
-            let documentName : String = path.lastPathComponent
+            let documentName : String = path?.lastPathComponent ?? ""
             path = docDir.stringByAppendingPathComponent(documentName)
         }
-        var writeSuccess = document!.write(to: URL(fileURLWithPath: path))
+        var writeSuccess = document?.write(to: URL(fileURLWithPath: path ?? ""))
         if writeSuccess == false {
             __NSBeep()
             return;
         }
-        let newDocument = CPDFDocument(url: URL(fileURLWithPath: path))!
+        let newDocument = CPDFDocument(url: URL(fileURLWithPath: path ?? ""))
         
-        for i in 0 ... newDocument.pageCount-1 {
-            let page = newDocument.page(at: i)
+        for i in 0 ... (newDocument?.pageCount ?? 1)-1 {
+            let page = newDocument!.page(at: i)
             var annotations : [CPDFAnnotation] = []
             for annotation in page!.annotations {
                 annotations.append(annotation)
@@ -2292,9 +2313,9 @@ extension KMMainViewController {
                 annotation.page.removeAnnotation(annotation)
             }
         }
-        writeSuccess = newDocument.write(to:URL(fileURLWithPath: path))
-        if writeSuccess {
-            let url = URL(fileURLWithPath: path)
+        writeSuccess = newDocument?.write(to:URL(fileURLWithPath: path ?? ""))
+        if writeSuccess ?? false {
+            let url = URL(fileURLWithPath: path ?? "")
             let picker = NSSharingServicePicker.init(items: [url])
             if sender.shareButton.window != nil {
                 picker.show(relativeTo: sender.shareButton.bounds, of: sender.shareButton, preferredEdge: NSRectEdge.minY)