Ver código fonte

【2025】【转档】测试 - 转档的导出位置有同名文件时同时保留修复

lizhe 1 mês atrás
pai
commit
6e6f70276c

+ 12 - 0
PDF Office/PDF Master/KMClass/Convert/Base/KMConvertBaseWindowController.swift

@@ -528,6 +528,18 @@ class KMConvertBaseWindowController: KMNBaseWindowController {
                 let name = url.deletingPathExtension().lastPathComponent
                 panel.nameFieldStringValue = name
                 panel.allowedFileTypes = [self.fileExtension]
+                
+                // 检查文件是否已存在
+                var finalURL = panel.directoryURL?.appendingPathComponent(panel.nameFieldStringValue).appendingPathExtension(self.fileExtension)
+                
+                var count = 1
+                while FileManager.default.fileExists(atPath: finalURL?.path ?? "") {
+                    // 如果文件已存在,给文件名添加后缀数字
+                    let newName = "\(name)(\(count))"
+                    panel.nameFieldStringValue = newName
+                    finalURL = panel.directoryURL?.appendingPathComponent(newName).appendingPathExtension(self.fileExtension)
+                    count += 1
+                }
             } completion: { response, url in
                 if (response == .cancel) {
                     return

+ 40 - 116
PDF Office/PDF Master/KMClass/NewBatch/Manager/KMBatchManager.swift

@@ -97,62 +97,6 @@ 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]) {
         guard !filesData.isEmpty else { return }
 
@@ -167,10 +111,7 @@ extension KMBatchManager {
             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: "")
-            }
+            var path = self.fetchFilePath(type: .convertPDF, filePath: filePath, outputFolderPath: outputFolderPath)
 
             let convert = self.addConvertParameter(settingData)
             let pageCount = document.pageCount
@@ -180,7 +121,7 @@ extension KMBatchManager {
 
             convert.outputFolderPath = outputFolderPath
             convert.filePath = filePath
-            convert.outputFileName = fileName
+            convert.outputFileName = path.deletingPathExtension.lastPathComponent
             convert.pages = pages
             convert.isAllowOCR = settingData.needRecognizeText
             convert.ocrLanguage = settingData.languageType
@@ -327,13 +268,9 @@ extension KMBatchManager {
             if document != nil {
                 //计算需要处理的页面
                 
-                var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                if ((fileName.isEmpty)) {
-                    fileName = NSLocalizedString("Untitled", comment: "")
-                }
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .OCR, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 
-                self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: fileName, data: data) { [unowned self] progress in
+                self.convertOCR(outputFolderPath: outputFolderPath, document: document!, fileName: path.deletingPathExtension.lastPathComponent, data: data) { [unowned self] progress in
                     self.itemProgress(item: item, processValue: progress)
                 } complete: { [unowned self] document, text, error in
                     if error == nil {
@@ -376,11 +313,7 @@ extension KMBatchManager {
                 let item = filesData[i]
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                 if document != nil {
-                    var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                    if ((fileName.isEmpty)) {
-                        fileName = NSLocalizedString("Untitled", comment: "")
-                    }
-                    let path = outputFolderPath + "/" + fileName + ".pdf"
+                    var path = self.fetchFilePath(type: .compress, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     
                     KMCompressManager.shared.compress(documentURL: URL(fileURLWithPath: item.filePath), fileURL: URL(fileURLWithPath: path), limit: false, model: data) { currentPage, totalPages in
                         let progress = Float(currentPage) / Float(totalPages)
@@ -415,11 +348,7 @@ extension KMBatchManager {
                 let item = filesData[i]
                 let docuemt = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                 if (docuemt != nil) {
-                    var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                    if ((fileName.isEmpty)) {
-                        fileName = NSLocalizedString("Untitled", comment: "")
-                    }
-                    let path = outputFolderPath + "/" + fileName + ".pdf"
+                    var path = self.fetchFilePath(type: .security, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     
                     var options: [CPDFDocumentWriteOption : Any] = [:]
                     //开启密码
@@ -479,12 +408,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                if ((fileName.isEmpty)) {
-                    fileName = NSLocalizedString("Untitled", comment: "")
-                }
-                
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .watermark, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -533,12 +457,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                if ((fileName.isEmpty)) {
-                    fileName = NSLocalizedString("Untitled", comment: "")
-                }
-                
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .background, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -590,12 +509,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                if ((fileName.isEmpty)) {
-                    fileName = NSLocalizedString("Untitled", comment: "")
-                }
-                
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .headerAndFooter, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -644,12 +558,7 @@ extension KMBatchManager {
         if filesData.count != 0 {
             for i in 0..<filesData.count {
                 let item = filesData[i]
-                var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                if ((fileName.isEmpty)) {
-                    fileName = NSLocalizedString("Untitled", comment: "")
-                }
-                
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .batesNumber, filePath: item.filePath, outputFolderPath: outputFolderPath)
                 let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
 
                 if (document?.allowsPrinting == false || document?.allowsCopying == false) {
@@ -697,12 +606,7 @@ extension KMBatchManager {
             for i in 0..<filesData.count {
                 let item = filesData[i]
 //                DispatchQueue.global().async {
-                    var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                    if ((fileName.isEmpty)) {
-                        fileName = NSLocalizedString("Untitled", comment: "")
-                    }
-                    
-                    let path = outputFolderPath + "/" + fileName + ".pdf"
+                var path = self.fetchFilePath(type: .batchRemove, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     let document = CPDFDocument.init(url: URL(fileURLWithPath: item.filePath))
                     if document != nil {
                         if (document!.allowsPrinting == false || document!.allowsCopying == false) {
@@ -769,8 +673,7 @@ extension KMBatchManager {
             if data.isNewPDF {
                 if data.isMergeAll {
                     let item = filesData[0]
-                    var fileName = item.filePath.deletingPathExtension.lastPathComponent
-                    let path = outputFolderPath + "/" + fileName + ".pdf"
+                    var path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
                     
                     
                     var pdfDocument = CPDFDocument()
@@ -792,7 +695,7 @@ extension KMBatchManager {
                         model.pageRange = pages
                         
                         self.batchProgress()
-                        self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { progress in
+                        self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: path.deletingPathExtension.lastPathComponent, data: model) { progress in
                             self.batchProgress()
                         } complete: { document, text, error in
                             self.batchSuccess()
@@ -813,7 +716,7 @@ extension KMBatchManager {
                 }
             } else {
                 var fileName = data.selectFilePath.deletingPathExtension.lastPathComponent
-                let path = outputFolderPath + "/" + fileName + ".pdf"
+                let path = self.fetchFilePath(type: .imageToPDF, filePath: data.selectFilePath, outputFolderPath: outputFolderPath)
                 
                 var pdfDocument = CPDFDocument(url: NSURL(fileURLWithPath: data.selectFilePath) as URL)
                 let count: Int = Int(pdfDocument?.pageCount ?? 0)
@@ -869,8 +772,7 @@ extension KMBatchManager {
         let item = filesData[index]
 
         if data.isOCR {
-            let fileName = item.filePath.deletingPathExtension.lastPathComponent
-            let path = outputFolderPath + "/" + fileName + ".pdf"
+            let path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
 
             let pdfDocument = CPDFDocument()
             pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
@@ -887,15 +789,14 @@ extension KMBatchManager {
             model.pageRange = pages
 
             self.itemProgress(item: item, processValue: 0)
-            self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: fileName, data: model) { [unowned self] progress in
+            self.convertOCR(outputFolderPath: outputFolderPath, document: pdfDocument!, fileName: path.deletingPathExtension.lastPathComponent, data: model) { [unowned self] progress in
                 self.itemProgress(item: item, processValue: progress)
             } complete: { [unowned self] document, text, error in
                 self.itemSuccess(item: filesData[index])
                 processFile(at: index + 1, outputFolderPath: outputFolderPath, data: data)
             }
         } else {
-            let fileName = item.filePath.deletingPathExtension.lastPathComponent
-            let path = outputFolderPath + "/" + fileName + ".pdf"
+            let path = self.fetchFilePath(type: .imageToPDF, filePath: item.filePath, outputFolderPath: outputFolderPath)
 
             let pdfDocument = CPDFDocument()
             pdfDocument?.km_insert(image: item.image, at: pdfDocument?.pageCount ?? 0)
@@ -983,6 +884,29 @@ extension KMBatchManager {
             try?FileManager.default.removeItem(atPath: path)
         }
     }
+    
+    func fetchFilePath(type: KMBatchCollectionViewType, filePath: String, outputFolderPath: String) -> String {
+        var fileName = filePath.deletingPathExtension.lastPathComponent
+        if fileName.isEmpty {
+            fileName = NSLocalizedString("Untitled", comment: "")
+        }
+
+        var path = outputFolderPath + "/" + fileName + ".pdf"
+
+//        // 检查文件是否已存在,如果存在,则添加数字后缀
+//        var finalPath = path
+//        var count = 1
+//        while FileManager.default.fileExists(atPath: finalPath) {
+//            let newFileName = "\(fileName) \(count)"
+//            finalPath = outputFolderPath + "/" + newFileName + ".pdf"
+//            count += 1
+//        }
+
+        // 使用最终路径进行保存或其他操作
+//        path = finalPath
+        
+        return path
+    }
 }
 
 //MARK: Alert

+ 0 - 1
PDF Office/PDF Master/KMClass/NewBatch/View/KMBatchProcessingView/Tableview/Views/Tableview/Views/KMBatchProcessingNameTableCell.swift

@@ -12,7 +12,6 @@ class KMBatchProcessingNameTableCell: KMBatchProcessingTableCell {
     @IBOutlet var contentView: NSView!
     @IBOutlet weak var iconButton: NSButton!
     @IBOutlet weak var iconImageView: NSImageView!
-    @IBOutlet weak var iconImageViewWidthConstraint: NSLayoutConstraint!
     @IBOutlet weak var nameLabelLeftConstraint: NSLayoutConstraint!
     
     @IBOutlet weak var nameLabel: NSTextField!

+ 6 - 6
PDF Office/PDF Master/KMClass/NewBatch/View/KMBatchProcessingView/Tableview/Views/Tableview/Views/KMBatchProcessingNameTableCell.xib

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22505" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="23504" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
     <dependencies>
         <deployment identifier="macosx"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22505"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23504"/>
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
@@ -11,8 +11,8 @@
                 <outlet property="contentView" destination="c22-O7-iKe" id="H1z-Vy-z59"/>
                 <outlet property="iconButton" destination="5ya-vD-YSW" id="U9j-eG-AMc"/>
                 <outlet property="iconImageView" destination="c1u-iG-YM2" id="QSS-xq-Fva"/>
-                <outlet property="iconImageViewWidthConstraint" destination="d2u-Ci-pUM" id="xDx-cB-DPX"/>
                 <outlet property="nameLabel" destination="9BP-LK-TK6" id="dAh-4P-6ZZ"/>
+                <outlet property="nameLabelLeftConstraint" destination="ga0-4I-Vkl" id="U92-hY-RfR"/>
             </connections>
         </customObject>
         <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
@@ -22,7 +22,7 @@
             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
             <subviews>
                 <textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="9BP-LK-TK6">
-                    <rect key="frame" x="54" y="30" width="71" height="20"/>
+                    <rect key="frame" x="54" y="30" width="70" height="20"/>
                     <constraints>
                         <constraint firstAttribute="height" constant="20" id="H0s-Nl-EiE"/>
                     </constraints>
@@ -33,7 +33,7 @@
                     </textFieldCell>
                 </textField>
                 <button translatesAutoresizingMaskIntoConstraints="NO" id="5ya-vD-YSW">
-                    <rect key="frame" x="127" y="34" width="12" height="12"/>
+                    <rect key="frame" x="126" y="34" width="12" height="12"/>
                     <buttonCell key="cell" type="square" bezelStyle="shadowlessSquare" image="icon_base_lock" imagePosition="overlaps" alignment="center" imageScaling="proportionallyDown" inset="2" id="Bp4-WW-FZi">
                         <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                         <font key="font" metaFont="system"/>
@@ -57,10 +57,10 @@
             </subviews>
             <constraints>
                 <constraint firstItem="c1u-iG-YM2" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="8" id="DPy-9S-nnx"/>
-                <constraint firstItem="9BP-LK-TK6" firstAttribute="leading" secondItem="c1u-iG-YM2" secondAttribute="trailing" constant="8" id="Gb2-73-VDt"/>
                 <constraint firstItem="9BP-LK-TK6" firstAttribute="centerY" secondItem="c22-O7-iKe" secondAttribute="centerY" id="ddV-Cy-IHk"/>
                 <constraint firstItem="5ya-vD-YSW" firstAttribute="centerY" secondItem="c22-O7-iKe" secondAttribute="centerY" id="dp9-du-KDu"/>
                 <constraint firstItem="5ya-vD-YSW" firstAttribute="leading" secondItem="9BP-LK-TK6" secondAttribute="trailing" constant="4" id="gKw-Mg-2Xr"/>
+                <constraint firstItem="9BP-LK-TK6" firstAttribute="leading" secondItem="c22-O7-iKe" secondAttribute="leading" constant="56" id="ga0-4I-Vkl"/>
                 <constraint firstItem="9BP-LK-TK6" firstAttribute="width" relation="lessThanOrEqual" secondItem="c22-O7-iKe" secondAttribute="width" multiplier="0.8" id="gbo-oe-34t"/>
                 <constraint firstItem="c1u-iG-YM2" firstAttribute="centerY" secondItem="c22-O7-iKe" secondAttribute="centerY" id="jug-b2-4DS"/>
             </constraints>