Browse Source

【页面编辑】对同一个文件拆分,保存在同一位置会直接覆盖掉旧的(已修复)

tangchao 1 year ago
parent
commit
31b5722cb8

+ 23 - 0
PDF Office/PDF Master/Class/Common/Tools/KMTools.swift

@@ -180,6 +180,29 @@ import Cocoa
     @objc class func isOfficeType(_ exn: String) -> Bool {
         return KMTools.officeExtensions.contains(exn.lowercased())
     }
+    
+    @objc class func getUniqueFilePath(filePath: String) -> String {
+        var isDirectory: ObjCBool = false
+        var uniqueFilePath = filePath
+        let fileManager = FileManager.default
+        fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
+        
+        var i = 0
+        if (isDirectory.boolValue) {
+            while fileManager.fileExists(atPath: uniqueFilePath) {
+                i += 1
+                uniqueFilePath = "\(filePath)(\(i))"
+            }
+        } else {
+            let fileURL = URL(fileURLWithPath: filePath)
+            let path = fileURL.deletingPathExtension().path
+            while fileManager.fileExists(atPath: uniqueFilePath) {
+                i += 1
+                uniqueFilePath = "\(path)(\(i).\(fileURL.pathExtension)"
+            }
+        }
+        return uniqueFilePath
+    }
 }
 
 // MARK: -

+ 5 - 4
PDF Office/PDF Master/Class/PDFTools/PageEdit/Controller/KMPDFEditViewController.swift

@@ -670,10 +670,11 @@ class KMPDFEditViewController: KMPDFThumbViewBaseController {
                     let _document: CPDFDocument = (self?.listView?.document)!
                     
                     let filePath = "\(panel.url!.path)/\(_document.documentURL.deletingPathExtension().lastPathComponent)"
-                    if (!FileManager.default.fileExists(atPath: filePath)) {
-                        try?FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: true)
+                    let uniqueFilePath = KMTools.getUniqueFilePath(filePath: filePath)
+//                    Swift.debugPrint(uniqueFilePath)
+                    if (!FileManager.default.fileExists(atPath: uniqueFilePath)) {
+                        try?FileManager.default.createDirectory(atPath: uniqueFilePath, withIntermediateDirectories: true)
                     }
-//                    let fileName = outputModel.outputFileNameDeletePathExtension
                     
                     var filepaths: [String] = []
                     for i in 0 ..< file_indexs!.count {
@@ -681,7 +682,7 @@ class KMPDFEditViewController: KMPDFThumbViewBaseController {
                         if (indexs.isEmpty) {
                             continue
                         }
-                        let filepath = String(format: "%@/%@.pdf", filePath, model.getNewOutputFileName(index: i+1))
+                        let filepath = String(format: "%@/%@.pdf", uniqueFilePath, model.getNewOutputFileName(index: i+1))
                         filepaths.append(filepath)
                         
                         let newDocument = CPDFDocument()