Browse Source

综合 - 修复保存文件、文件夹路径不存在时创建对应路径进行文件存储

wanjun 1 year ago
parent
commit
aac3ddadda

+ 25 - 0
PDF Office/PDF Master/Class/Home/ViewController/KMAIOpenPDFFilesVC.swift

@@ -96,6 +96,15 @@ class KMAIOpenPDFFilesVC: NSViewController {
         let imageName = NSString(string: NSString(string: filePath).lastPathComponent).deletingPathExtension
         
         let path = self.fetchDifferentFilePath(filePath: savePath + "/" + imageName + ".pdf")
+        
+        if (!FileManager.default.fileExists(atPath: path.deletingLastPathComponent as String)) {
+            try?FileManager.default.createDirectory(atPath: path.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        if (!FileManager.default.fileExists(atPath: path as String)) {
+            FileManager.default.createFile(atPath: path as String, contents: nil)
+        }
+
         let document = CPDFDocument.init()
         var success = false
         
@@ -133,6 +142,15 @@ class KMAIOpenPDFFilesVC: NSViewController {
         let filePath = url.path
         let folderPath = "convertToPDF.pdf"
         let savePath = folderPath.kUrlToPDFFolderPath()
+        
+        if (!FileManager.default.fileExists(atPath: savePath!.deletingLastPathComponent as String)) {
+            try?FileManager.default.createDirectory(atPath: savePath!.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        if (!FileManager.default.fileExists(atPath: savePath! as String)) {
+            FileManager.default.createFile(atPath: savePath! as String, contents: nil)
+        }
+
         if savePath == nil {
             return
         }
@@ -371,6 +389,13 @@ class KMAIOpenPDFFilesVC: NSViewController {
     @IBAction func openBlankPage(_ sender: Any) {
         let fileName: NSString = String(format: "%@.pdf", NSLocalizedString("Untitled", comment: "")) as NSString
         let savePath = fetchUniquePath(fileName.kUrlToPDFFolderPath())
+        if (!FileManager.default.fileExists(atPath: savePath.deletingLastPathComponent as String)) {
+            try?FileManager.default.createDirectory(atPath: savePath.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        if (!FileManager.default.fileExists(atPath: savePath as String)) {
+            FileManager.default.createFile(atPath: savePath as String, contents: nil)
+        }
         let pdfDocument = CPDFDocument()
         pdfDocument?.insertPage(CGSize(width: 595, height: 842), at: 0)
         pdfDocument?.write(to: URL(fileURLWithPath: savePath))

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

@@ -504,6 +504,15 @@ extension KMHomeViewController {
         let imageName = NSString(string: NSString(string: filePath).lastPathComponent).deletingPathExtension
         
         let path = self.fetchDifferentFilePath(filePath: savePath + "/" + imageName + ".pdf")
+        
+        if (!FileManager.default.fileExists(atPath: path.deletingLastPathComponent as String)) {
+            try?FileManager.default.createDirectory(atPath: path.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        if (!FileManager.default.fileExists(atPath: path as String)) {
+            FileManager.default.createFile(atPath: path as String, contents: nil)
+        }
+
         let document = CPDFDocument.init()
         var success = false
         
@@ -541,6 +550,15 @@ extension KMHomeViewController {
         let filePath = url.path
         let folderPath = "convertToPDF.pdf"
         let savePath = folderPath.kUrlToPDFFolderPath()
+        
+        if (!FileManager.default.fileExists(atPath: savePath!.deletingLastPathComponent as String)) {
+            try?FileManager.default.createDirectory(atPath: savePath!.deletingLastPathComponent as String, withIntermediateDirectories: true, attributes: nil)
+        }
+        
+        if (!FileManager.default.fileExists(atPath: savePath! as String)) {
+            FileManager.default.createFile(atPath: savePath! as String, contents: nil)
+        }
+
         if savePath == nil {
             return
         }