Browse Source

AI翻译 - 文件翻译成功下载时,若存在同名文件,删除旧文件后再保存开启新文档

wanjun 1 year ago
parent
commit
8254dcc421

+ 9 - 1
PDF Office/PDF Master/Class/Home/ViewController/KMAITranslationVC.swift

@@ -175,6 +175,15 @@ class KMAITranslationVC: NSViewController {
 
             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
@@ -196,7 +205,6 @@ class KMAITranslationVC: NSViewController {
                 return
             }
 
-            let destinationURL = FileManager.default.temporaryDirectory.appendingPathComponent(downFileName)
 
             do {
                 try FileManager.default.moveItem(at: tempLocalURL, to: destinationURL)