Ver código fonte

【2025】【批量】批量转档递归优化

lizhe 1 mês atrás
pai
commit
e1f5ce15d6

+ 110 - 50
PDF Office/PDF Master/KMClass/NewBatch/Manager/KMBatchManager.swift

@@ -97,60 +97,116 @@ extension KMBatchManager {
         self.convertFile(outputFolderPath: outputFolderPath, data: data, filesData: self.batchFilesData)
     }
     
+//    func convertFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]) {
+//        if filesData.count != 0 {
+//            DispatchQueue.global().async {
+//                for i in 0..<filesData.count {
+//                    let item = filesData[i]
+//                    //创建Document
+//                    let filePath = item.filePath
+//                    let document = self.fetchDocument(filePath: filePath, model: item)
+//                    
+//                    let settingData = data as? KMBatchConvertPDFViewModel  ?? KMBatchConvertPDFViewModel()
+//                    var fileName = filePath.deletingPathExtension.lastPathComponent
+//                    if ((fileName.isEmpty)) {
+//                        fileName = NSLocalizedString("Untitled", comment: "")
+//                    }
+//                    
+//                    let convert = self.addConvertParameter(settingData)
+//                    
+//                    let pageCount = document.pageCount
+//                    //获取page
+//                    var pages:[Int] = []
+//                    for i in 0..<pageCount {
+//                        pages.append(Int(i)+1)
+//                    }
+//                    
+//                    convert.outputFolderPath = outputFolderPath
+//                    convert.filePath = filePath
+//                    convert.outputFileName = fileName
+//                    convert.pages = pages
+//                    
+//                    convert.isAllowOCR = settingData.needRecognizeText
+//                    convert.ocrLanguage = settingData.languageType
+//                    
+//                    item.state = .clock
+//                    KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { [unowned self] progressValue in
+//                        print("转档进度 - \(progressValue)")
+//                        let progress = Float(progressValue) / Float(pageCount)
+//                        self.itemProgress(item: item, processValue: progress)
+//                    }, completion: { [unowned self] finished, error in
+//                        if finished {
+//                            if FileManager.default.fileExists(atPath: outputFolderPath) {
+//                                NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
+//                            }
+//                            self.itemSuccess(item: item)
+//                        } else {
+//                            self.itemFailure(item: item, error: error! as NSError)
+//                        }
+//                        
+//                        if i == filesData.count - 1 {
+//                            self.batchSuccess()
+//                        }
+//                    })
+//                }
+//            }
+//        }
+//    }
+    
     func convertFile(outputFolderPath: String, data: KMBatchSettingItemViewModel, filesData: [KMBatchProcessingTableViewModel]) {
-        if filesData.count != 0 {
-            DispatchQueue.global().async {
-                for i in 0..<filesData.count {
-                    let item = filesData[i]
-                    //创建Document
-                    let filePath = item.filePath
-                    let document = self.fetchDocument(filePath: filePath, model: item)
-                    
-                    let settingData = data as? KMBatchConvertPDFViewModel  ?? KMBatchConvertPDFViewModel()
-                    var fileName = filePath.deletingPathExtension.lastPathComponent
-                    if ((fileName.isEmpty)) {
-                        fileName = NSLocalizedString("Untitled", comment: "")
-                    }
-                    
-                    let convert = self.addConvertParameter(settingData)
-                    
-                    let pageCount = document.pageCount
-                    //获取page
-                    var pages:[Int] = []
-                    for i in 0..<pageCount {
-                        pages.append(Int(i)+1)
+        guard !filesData.isEmpty else { return }
+
+        func processFile(at index: Int) {
+            guard index < filesData.count else {
+                self.batchSuccess()
+                return
+            }
+
+            let item = filesData[index]
+            let filePath = item.filePath
+            let document = self.fetchDocument(filePath: filePath, model: item)
+
+            let settingData = data as? KMBatchConvertPDFViewModel ?? KMBatchConvertPDFViewModel()
+            var fileName = filePath.deletingPathExtension.lastPathComponent
+            if fileName.isEmpty {
+                fileName = NSLocalizedString("Untitled", comment: "")
+            }
+
+            let convert = self.addConvertParameter(settingData)
+            let pageCount = document.pageCount
+
+            // 获取页面
+            let pages: [Int] = Array(1...Int(pageCount))
+
+            convert.outputFolderPath = outputFolderPath
+            convert.filePath = filePath
+            convert.outputFileName = fileName
+            convert.pages = pages
+            convert.isAllowOCR = settingData.needRecognizeText
+            convert.ocrLanguage = settingData.languageType
+
+            item.state = .clock
+
+            KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { [unowned self] progressValue in
+                print("转档进度 - \(progressValue)")
+                let progress = Float(progressValue) / Float(pageCount)
+                self.itemProgress(item: item, processValue: progress)
+            }, completion: { [unowned self] finished, error in
+                if finished {
+                    if FileManager.default.fileExists(atPath: outputFolderPath) {
+                        NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
                     }
-                    
-                    convert.outputFolderPath = outputFolderPath
-                    convert.filePath = filePath
-                    convert.outputFileName = fileName
-                    convert.pages = pages
-                    
-                    convert.isAllowOCR = settingData.needRecognizeText
-                    convert.ocrLanguage = settingData.languageType
-                    
-                    item.state = .clock
-                    KMPDFConvertManager.defaultManager.convert(convert: convert, progress: { [unowned self] progressValue in
-                        print("转档进度 - \(progressValue)")
-                        let progress = Float(progressValue) / Float(pageCount)
-                        self.itemProgress(item: item, processValue: progress)
-                    }, completion: { [unowned self] finished, error in
-                        if finished {
-                            if FileManager.default.fileExists(atPath: outputFolderPath) {
-                                NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: outputFolderPath)])
-                            }
-                            self.itemSuccess(item: item)
-                        } else {
-                            self.itemFailure(item: item, error: error! as NSError)
-                        }
-                        
-                        if i == filesData.count - 1 {
-                            self.batchSuccess()
-                        }
-                    })
+                    self.itemSuccess(item: item)
+                } else {
+                    self.itemFailure(item: item, error: error! as NSError)
                 }
-            }
+
+                processFile(at: index + 1)
+            })
         }
+
+        // 开始处理第一个文件
+        processFile(at: 0)
     }
     
     func addConvertParameter(_ data: KMBatchConvertPDFViewModel) -> KMPDFConvert {
@@ -937,6 +993,10 @@ extension KMBatchManager {
     }
     
     func batchProgress() {
+        for item in filesData {
+            self.itemProgress(item: item, processValue: 0)
+        }
+        
         self.state = .processing
         NotificationCenter.default.post(name: NSNotification.Name(kBacthProcessNotification), object: nil)
     }