Browse Source

AI - 首页拖拽翻译文件功能补充

wanjun 1 year ago
parent
commit
660d63b3b1

+ 100 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+Action.swift

@@ -597,6 +597,106 @@ extension KMHomeViewController {
         }
     }
     
+    func aiTranslation(withFilePath path: String) -> Void {
+        let infoDictionary = Bundle .main.infoDictionary!
+         let majorVersion = infoDictionary["CFBundleShortVersionString"]
+
+         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 {
+                 
+             }
+         }
+    }
+    
+    func fileTranslateHandle(_ fileKey: String) -> Void {
+        let infoDictionary = Bundle .main.infoDictionary!
+        let majorVersion = infoDictionary["CFBundleShortVersionString"]
+        
+        KMRequestServerManager.manager.aiTranslationFileTranslateHandle(fileKey: fileKey, from: self.aiTranslationViewController.fromStr, to: self.aiTranslationViewController.toStr, version: "1.0.1") { success, result in
+            if success {
+                let result: NSDictionary = result!.result
+
+                let fileUrl: String = result["fileUrl"] as! String
+                let downFileUrl: String = result["downFileUrl"] as! String
+                let ossDownUrl: String = result["ossDownUrl"] as! String
+                let fileName: String = result["fileName"] as! String
+                let downFileName: String = result["downFileName"] as! String
+                let from: String = result["from"] as! String
+                let to: String = result["to"] as! String
+                
+                self.downloadFile(filePath: ossDownUrl, downFileName: downFileName)
+            }
+        }
+    }
+    
+    func downloadFile(filePath: String, downFileName: String) -> Void {
+        guard let fileURL = URL(string: filePath) else {
+            let alert = NSAlert()
+            alert.alertStyle = .critical
+            alert.messageText = NSLocalizedString("Invalid file link", comment: "")
+            alert.runModal()
+
+            return
+        }
+        let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent(downFileName)
+        if FileManager.default.fileExists(atPath: destinationURL.path) {
+            do {
+                try FileManager.default.removeItem(at: destinationURL)
+                print("删除旧文件成功")
+            } catch {
+                print("删除旧文件失败:\(error)")
+            }
+        }
+        let sessionConfiguration = URLSessionConfiguration.default
+        let session = URLSession(configuration: sessionConfiguration)
+        let downloadTask = session.downloadTask(with: fileURL) { (tempLocalURL, response, error) in
+            if let error = error {
+                let alert = NSAlert()
+                alert.alertStyle = .critical
+                alert.messageText = String(format: "%@:\(error)", NSLocalizedString("Download failure", comment: ""))
+                alert.runModal()
+                
+                return
+            }
+
+            guard let tempLocalURL = tempLocalURL else {
+                let alert = NSAlert()
+                alert.alertStyle = .critical
+                alert.messageText = NSLocalizedString("The download file temporary path is invalid", comment: "")
+                alert.runModal()
+
+                return
+            }
+
+            do {
+                try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)
+                
+                NSDocumentController.shared.openDocument(withContentsOf: destinationURL, display: true) { document, documentWasAlreadyOpen, error in
+                    if error != nil {
+                        NSApp.presentError(error!)
+                    } else {
+                        
+                    }
+                }
+            } catch {
+                let alert = NSAlert()
+                alert.alertStyle = .critical
+                alert.messageText = String(format: "%@:\(error)", NSLocalizedString("File saving failure", comment: ""))
+                alert.runModal()
+            }
+        }
+        downloadTask.resume()
+    }
+    
     override func otherMouseDown(with event: NSEvent) {
         if historyFileViewController.selectFiles.count > 0 {
             let eventPoint = event.locationInWindow as NSPoint

+ 5 - 1
PDF Office/PDF Master/Class/Home/ViewController/KMHomeViewController+UI.swift

@@ -272,7 +272,11 @@ extension KMHomeViewController: KMPDFToolsViewControllerDelegate {
 
 extension KMHomeViewController: KMHomeDragViewDelegate {
     func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
-        self.openFile(withFilePath: filePath)
+        if self.aiHomeState == .AITranslation {
+            self.aiTranslation(withFilePath: filePath.path)
+        } else {
+            self.openFile(withFilePath: filePath)
+        }
     }
     
     func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {