Browse Source

AI - 文件翻译支持Word格式文件

wanjun 1 year ago
parent
commit
05a8c1e285

+ 34 - 32
PDF Office/PDF Master/Class/Home/ViewController/KMAITranslationVC.swift

@@ -116,42 +116,44 @@ class KMAITranslationVC: NSViewController {
                 openPanel.beginSheetModal(for: self.view.window!) { result in
                     if result == .OK {
                         for url in openPanel.urls {
-                            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()
+                            if (url.pathExtension == "pdf") || url.pathExtension == "PDF" {
+                                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()
+                                    return
+                                }
+                            }
+                            if self.isFileGreaterThan10MB(atPath: url.path) {
+                                self.errorView.isHidden = false
+                                self.errorLabel.stringValue = NSLocalizedString("The uploaded file cannot exceed 10MB", comment: "")
                             } else {
-                                if self.isFileGreaterThan10MB(atPath: url.path) {
-                                    self.errorView.isHidden = false
-                                    self.errorLabel.stringValue = NSLocalizedString("The uploaded file cannot exceed 10MB", comment: "")
-                                } else {
-                                    DispatchQueue.main.async {
-                                        self.showProgressWindow()
-                                    }
-                                    
-                                    let infoDictionary = Bundle .main.infoDictionary!
-                                    let majorVersion = infoDictionary["CFBundleShortVersionString"]
+                                DispatchQueue.main.async {
+                                    self.showProgressWindow()
+                                }
+                                
+                                let infoDictionary = Bundle .main.infoDictionary!
+                                let majorVersion = infoDictionary["CFBundleShortVersionString"]
 
-                                    KMRequestServerManager.manager.aiTranslationFileUpload(file: url.path, version: "1.0.1") { [unowned self] success, result in
-    //                                    self.hiddenProgressWindow()
-                                        if success {
-                                            let result: NSDictionary = result!.result
+                                KMRequestServerManager.manager.aiTranslationFileUpload(file: url.path, version: "1.0.1") { [unowned self] success, result in
+//                                    self.hiddenProgressWindow()
+                                    if success {
+                                        let result: NSDictionary = result!.result
 
-                                            let fileKey = result["fileKey"]
-                                            let fileName = result["fileName"]
-                                            let pageCount = result["pageCount"]
-                                            if fileKey != nil {
-                                                self.fileTranslateHandle(fileKey as! String)
-                                            }
-                                        } else {
-                                            let result: String = result!.message
+                                        let fileKey = result["fileKey"]
+                                        let fileName = result["fileName"]
+                                        let pageCount = result["pageCount"]
+                                        if fileKey != nil {
+                                            self.fileTranslateHandle(fileKey as! String)
+                                        }
+                                    } else {
+                                        let result: String = result!.message
 
-                                            DispatchQueue.main.async {
-                                                self.hiddenProgressWindow()
-                                                self.errorView.isHidden = false
-                                                self.errorLabel.stringValue = result
-                                            }
+                                        DispatchQueue.main.async {
+                                            self.hiddenProgressWindow()
+                                            self.errorView.isHidden = false
+                                            self.errorLabel.stringValue = result
                                         }
                                     }
                                 }

+ 31 - 28
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -607,40 +607,43 @@ extension KMHomeViewController {
             self.aiTranslationViewController.errorView.isHidden = false
             self.aiTranslationViewController.errorLabel.stringValue = NSLocalizedString("The uploaded file cannot exceed 10MB", comment: "")
         } else {
-            if !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 {
-                let infoDictionary = Bundle .main.infoDictionary!
-                 let majorVersion = infoDictionary["CFBundleShortVersionString"]
-                DispatchQueue.main.async {
-                    self.showProgressWindow()
+            let url = URL(fileURLWithPath: path)
+            if (url.pathExtension == "pdf") || url.pathExtension == "PDF" {
+                if !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()
+                    return
                 }
-                
-                 KMRequestServerManager.manager.aiTranslationFileUpload(file: path, version: "1.0.1") { [unowned self] success, result in
-                     if success {
-                         let result: NSDictionary = result!.result
+            }
+            let infoDictionary = Bundle .main.infoDictionary!
+             let majorVersion = infoDictionary["CFBundleShortVersionString"]
+            DispatchQueue.main.async {
+                self.showProgressWindow()
+            }
+            
+             KMRequestServerManager.manager.aiTranslationFileUpload(file: path, version: "1.0.1") { [unowned self] success, result in
+                 if success {
+                     let result: NSDictionary = result!.result
 
-                         let fileKey = result["fileKey"]
-                         let fileName = result["fileName"]
-                         let pageCount = result["pageCount"]
-                         if fileKey != nil {
-                             self.fileTranslateHandle(fileKey as! String)
-                         }
-                     } else {
-                         let result: String = result!.message
+                     let fileKey = result["fileKey"]
+                     let fileName = result["fileName"]
+                     let pageCount = result["pageCount"]
+                     if fileKey != nil {
+                         self.fileTranslateHandle(fileKey as! String)
+                     }
+                 } else {
+                     let result: String = result!.message
 
-                         DispatchQueue.main.async {
-                             self.hiddenProgressWindow()
-                             self.aiTranslationViewController.errorView.isHidden = false
-                             self.aiTranslationViewController.errorLabel.stringValue = result
+                     DispatchQueue.main.async {
+                         self.hiddenProgressWindow()
+                         self.aiTranslationViewController.errorView.isHidden = false
+                         self.aiTranslationViewController.errorLabel.stringValue = result
 
-                         }
                      }
                  }
-            }
+             }
         }
     }