123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // KMBatchBaseParameter.swift
- // PDF Reader Pro
- //
- // Created by kdanmobile on 2023/10/25.
- //
- import Cocoa
- @objcMembers class KMBatchBaseParameter: NSObject{
- var error: NSError?
- var operateFile: KMBatchOperateFile?
- var pageChoice: KMBatchOperatePageChoice = .All {
- willSet{
-
- }
- didSet {
- if pageChoice != oldValue {
- self.localPagesArray = nil
- }
- }
- }
- var pageRangeString: String? {
- willSet{
-
- }
- didSet {
- if pageRangeString != oldValue {
- self.localPagesArray = nil
- }
- }
- }
- var progress: Float?
- var status: KMBatchOperateStatus = .Waiting
- var savePath: String?
- var outPutPath: String?
- var pagesArray: [NSNumber]? {
- if self.localPagesArray == nil {
- self.localPagesArray = self.fetchUnlimitPagesArray()
- }
- return self.localPagesArray
- }
- private var localPagesArray: [NSNumber]?
-
- private var _doc: CPDFDocument?
-
- init(file:KMBatchOperateFile) {
- super.init()
- self.operateFile = file
- }
-
- func fetchUnlimitPagesArray() -> [NSNumber]? {
- var document = self._doc
- if document == nil { // 优化重复创建
- document = CPDFDocument(url: URL(fileURLWithPath: self.operateFile?.filePath ?? ""))
- self._doc = document
- }
- if let data = document?.isLocked, data { // 加锁的话,需要解锁
- if let data = self.operateFile?.password, data.isEmpty == false {
- document?.unlock(withPassword: data)
- }
- }
- let pageNumber = document?.pageCount ?? 0
- if pageNumber <= 0 { // 容错判断
- return nil
- }
- if self.pageChoice == .All {
- var selectPages: [NSNumber] = []
- for i in 1...pageNumber {
- selectPages.append(NSNumber(integerLiteral: Int(i)))
- }
- return selectPages
- } else if self.pageChoice == .Odd {
- var selectPages: [NSNumber] = []
- for i in stride(from: 1, through: pageNumber, by: 2) {
- selectPages.append(NSNumber(integerLiteral: Int(i)))
- }
- return selectPages
- } else if self.pageChoice == .Even {
- var selectPages: [NSNumber] = []
- for i in stride(from: 2, through: pageNumber, by: 2) {
- selectPages.append(NSNumber(integerLiteral: Int(i)))
- }
- return selectPages
- } else {
- var pageNumbers: [NSNumber] = []
- var isInvalid = false
- if self.pageRangeString?.count ?? 0 > 0{
- for c in self.pageRangeString! {
- if c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-" {
- isInvalid = true
- break
- } else {
- isInvalid = false
- }
- }
- }
- if !isInvalid {
- if self.pageRangeString == nil {
- return nil
- }
- if let data = self.pageRangeString?.isEmpty, data { // 数据错误
- return nil
- }
-
- let array = self.pageRangeString!.components(separatedBy: ",")
- for s in array {
- if s.isEmpty {
- isInvalid = true
- break
- } else {
- let pages = s.components(separatedBy: "-")
- if pages.count > 2 {
- isInvalid = true
- break
- } else if pages.count == 1 {
- let p = pages[0]
- if p.isEmpty || Int(p)! > pageNumber || Int(p)! == 0 {
- isInvalid = true
- break
- } else {
- var isEqual = false
- for pageNumber in pageNumbers {
- if pageNumber.intValue == Int(p)! {
- isEqual = true
- isInvalid = true
- break
- }
- }
- if !isEqual {
- pageNumbers.append(NSNumber(integerLiteral: Int(p)!))
- }
- }
- } else if pages.count == 2 {
- let p1 = pages[0]
- let p2 = pages[1]
- if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1)! == 0 {
- isInvalid = true
- break
- } else {
- var isEqual = false
- for i in Int(p1)!...Int(p2)! {
- for pageNumber in pageNumbers {
- if pageNumber.intValue == i {
- isEqual = true
- isInvalid = true
- break
- }
- }
- }
- if !isEqual {
- for i in Int(p1)!...Int(p2)! {
- pageNumbers.append(NSNumber(integerLiteral: i))
- }
- }
- }
- }
- }
- }
- }
- if isInvalid {
- return nil
- }
- let tmpArray = NSMutableArray(array: pageNumbers)
- QuickSort(list: tmpArray, startIndex: 0, endIndex: pageNumbers.count - 1)
- return tmpArray as? [NSNumber]
- }
- }
-
- func QuickSort(list: NSMutableArray, startIndex: NSInteger, endIndex: NSInteger) {
- if startIndex >= endIndex {
- return
- }
- let temp = list[startIndex] as! NSNumber
- var tempIndex = startIndex
-
- for i in (startIndex + 1)...endIndex {
- let t = list[i] as! NSNumber
- if temp.intValue > t.intValue {
- tempIndex = tempIndex + 1
- list.exchangeObject(at: tempIndex, withObjectAt: i)
- }
- }
-
- list.exchangeObject(at: tempIndex, withObjectAt: startIndex)
- QuickSort(list: list, startIndex: startIndex, endIndex: tempIndex-1)
- QuickSort(list: list, startIndex: tempIndex+1, endIndex: endIndex)
- }
-
- func getUniqueFilePath(filePath: String) -> String {
- var i = 0
- var isDirectory = ObjCBool(false)
- var uniqueFilePath = filePath
- let fileManager = FileManager.default
- fileManager.fileExists(atPath: uniqueFilePath, isDirectory: &isDirectory)
- if isDirectory.boolValue {
- while fileManager.fileExists(atPath: uniqueFilePath) {
- i += 1
- uniqueFilePath = "\(filePath)(\(i))"
- }
- } else {
- while fileManager.fileExists(atPath: uniqueFilePath) {
- i += 1
- var path = "\(filePath.deletingPathExtension)(\(i))"
- // uniqueFilePath = path.stringByAppendingPathComponent(filePath.extension)
- // path.append(".")
- path.append(filePath.extension)
- uniqueFilePath = path
- }
- }
-
- return uniqueFilePath
- }
-
- func resetState() {
- status = .Waiting
- progress = 0
- error = nil
- savePath = nil
- outPutPath = nil
- }
- func fetchDestinationFilepath() -> String? {
- if self.isEqual(self.operateFile?.convertInfo) {
- // 在子类重写了这个方法,这里永远不会调用,所以不需要写
- } else if self.isEqual(self.operateFile?.addPasswordInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_SetPassword")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.removePasswordInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemovePassword")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.addWatermarkInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_Watermark")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.removeWatermarkInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveWatermark")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.addBackgroundInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddBackground")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.removeBackgroundInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveBackground")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.addHeaderFooterInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddHeaderFooter")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.removeHeaderFooterInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveHeaderFooter")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.addBatesInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_AddBates")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.removeBatesInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_RemoveBates")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- } else if self.isEqual(self.operateFile?.compressInfo) {
- if self.savePath?.count ?? 0 > 0 && self.operateFile?.filePath.count ?? 0 > 0 {
-
- let filePath1 = self.savePath?.stringByAppendingPathComponent(self.operateFile!.filePath.getLastComponentDeleteExtension + "_Compressed")
- self.outPutPath = self.getUniqueFilePath(filePath: filePath1!.stringByAppendingPathExtension("pdf"))
- return self.outPutPath
- }
- }
- return nil
- }
- }
|