|
@@ -34,22 +34,28 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
var accessSemaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 1)
|
|
|
var didSuccess:Int = 1
|
|
|
|
|
|
+ var complention = { (status:Int) in
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
static var instance = FileConverter()
|
|
|
class func shared() -> FileConverter {
|
|
|
return instance
|
|
|
}
|
|
|
|
|
|
- func converter(_ inSrcPath: String, inDesPath: String) -> Int {
|
|
|
- return converter(inSrcPath, inDesPath: inDesPath, params: nil)
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String, complention:@escaping (_ status:Int) -> ()) {
|
|
|
+ return converter(inSrcPath, inDesPath: inDesPath, params: nil, complention: complention)
|
|
|
}
|
|
|
|
|
|
- func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Int {
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?, complention:@escaping (_ status:Int) -> ()) {
|
|
|
+ self.complention = complention;
|
|
|
+
|
|
|
if !FileManager.default.fileExists(atPath: inSrcPath) {
|
|
|
- return -1
|
|
|
+ self.complention(-1)
|
|
|
+
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
- accessSemaphore?.wait()
|
|
|
-
|
|
|
self.srcPath = inSrcPath
|
|
|
self.desPath = inDesPath
|
|
|
|
|
@@ -68,18 +74,18 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
self.srcPath = symbolicLink!
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if FileManager.default.fileExists(atPath: self.desPath) {
|
|
|
try! FileManager.default.removeItem(atPath: self.desPath)
|
|
|
}
|
|
|
|
|
|
self.pathExtension = NSString(string: self.desPath).pathExtension
|
|
|
-// let outputPath = NSString(string: self.desPath).deletingPathExtension
|
|
|
-// let output = self.desPath
|
|
|
+ // let outputPath = NSString(string: self.desPath).deletingPathExtension
|
|
|
+ // let output = self.desPath
|
|
|
let password = params?.value(forKey: "password")
|
|
|
let tParams = params ?? [:]
|
|
|
let useOldLibValue = params?.value(forKey: "useOldLib")
|
|
|
-
|
|
|
+
|
|
|
var useOldLib = false;
|
|
|
if (nil != useOldLibValue) {
|
|
|
if ((useOldLibValue as? String) != nil) {
|
|
@@ -88,7 +94,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
useOldLib = (useOldLibValue as? NSNumber)!.boolValue || (useOldLibValue as? NSNumber)!.intValue == 1
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
NSLog("%@ - %@", useOldLib ? "老库" : "新库", params ?? "")
|
|
|
|
|
|
self.convertQueue.async {
|
|
@@ -106,22 +112,22 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
self.pages.append(i)
|
|
|
}
|
|
|
|
|
|
- if !useOldLib && NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
|
|
|
+ if !useOldLib && NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
|
|
|
autoreleasepool {
|
|
|
- let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
- self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
|
|
|
- if (nil == self.pdfConverter && nil != password) {
|
|
|
- self.pdfConverter = CPDFConverterImg.init(url: url, password: password as? String)
|
|
|
- }
|
|
|
- self.pdfConverter?.delegate = self
|
|
|
- self.options = CPDFConvertImgOptions()
|
|
|
- if (NSArray(array: ["jpg", "JPG"]).contains(self.pathExtension)) {
|
|
|
- (self.options as! CPDFConvertImgOptions).type = .JPEG
|
|
|
- }else {
|
|
|
- (self.options as! CPDFConvertImgOptions).type = .PNG
|
|
|
- }
|
|
|
- self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
|
|
|
+ let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
|
|
|
+ self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
|
|
|
+ if (nil == self.pdfConverter && nil != password) {
|
|
|
+ self.pdfConverter = CPDFConverterImg.init(url: url, password: password as? String)
|
|
|
}
|
|
|
+ self.pdfConverter?.delegate = self
|
|
|
+ self.options = CPDFConvertImgOptions()
|
|
|
+ if (NSArray(array: ["jpg", "JPG"]).contains(self.pathExtension)) {
|
|
|
+ (self.options as! CPDFConvertImgOptions).type = .JPEG
|
|
|
+ }else {
|
|
|
+ (self.options as! CPDFConvertImgOptions).type = .PNG
|
|
|
+ }
|
|
|
+ self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
|
|
|
+ }
|
|
|
}else if !useOldLib && NSArray(array: ["ppt", "PPT", "PPTX", "pptx"]).contains(self.pathExtension) {
|
|
|
autoreleasepool {
|
|
|
self.pdfConverter = CPDFConverterPPT.init(url: url, password: nil)
|
|
@@ -230,6 +236,22 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String) -> Int {
|
|
|
+ return converter(inSrcPath, inDesPath: inDesPath, params: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Int {
|
|
|
+ if !FileManager.default.fileExists(atPath: inSrcPath) {
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+
|
|
|
+ accessSemaphore?.wait()
|
|
|
+
|
|
|
+ converter(inSrcPath, inDesPath: inDesPath, params: params) { status in
|
|
|
+ self.semaphore?.signal()
|
|
|
+ };
|
|
|
|
|
|
semaphore?.wait()
|
|
|
accessSemaphore?.signal()
|
|
@@ -268,7 +290,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
self.pdfConverter?.delegate = nil
|
|
|
self.pdfConverter = nil
|
|
|
|
|
|
- semaphore?.signal()
|
|
|
+ self.complention(didSuccess)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -284,11 +306,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
|
|
|
|
|
|
sleep(2)
|
|
|
|
|
|
- // self.fpConverter?.stopConvertsionIfNeed()
|
|
|
- // self.fpConverter?.setDelegate(nil)
|
|
|
- // self.fpConverter = nil
|
|
|
- //
|
|
|
- semaphore?.signal()
|
|
|
+ self.complention(didSuccess)
|
|
|
}
|
|
|
}
|
|
|
|