PDFCompareTest.swift 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // PDFCompareTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2023/2/16.
  6. //
  7. import Foundation
  8. import AppKit
  9. class PDFCompareTest : AutoTest {
  10. static var pdfSharedInstance = PDFCompareTest()
  11. override class func shared() -> AutoTest? {
  12. return pdfSharedInstance
  13. }
  14. override func clearCacheFiles() {
  15. }
  16. override func process(_ originPath:String, resultPath:String, complention:@escaping (_ status:Int) -> ()) {
  17. DispatchQueue.global().async {
  18. autoreleasepool {
  19. Thread.sleep(forTimeInterval: 0.3);
  20. complention(1)
  21. }
  22. }
  23. }
  24. override func compareFiles(_ fileName: String) -> NSArray? {
  25. let files = NSMutableArray()
  26. let checkDirectory = self.checkFileDirectory()
  27. // let originDirectory = self.originFileDirectory()
  28. let resultDirectory = self.resultFileDirectory()
  29. let fName = NSString(string: fileName).deletingPathExtension
  30. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  31. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+".jpg")
  32. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
  33. var isDirectory = ObjCBool(false)
  34. if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
  35. // compare screenshoot between result file with check file
  36. let items = NSMutableArray()
  37. if (isDirectory.boolValue) {
  38. let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
  39. for item in NSArray(array: searchItems) {
  40. let ext = NSString(string: item as! String).pathExtension.lowercased()
  41. if NSArray(array: ["jpg"]).contains(ext) {
  42. let fileName = NSString(string: fName+".jpg"+"/\(item as! String)").deletingPathExtension
  43. items.add(fileName)
  44. }
  45. }
  46. }else {
  47. items.add(fName)
  48. }
  49. let nitems = items.sortedArray { str1, str2 in
  50. let s1 = NSString(string: str1 as! String).deletingPathExtension
  51. let s2 = NSString(string: str2 as! String).deletingPathExtension
  52. if (NSString(string: s1).length != NSString(string: s2).length) {
  53. if (NSString(string: s1).length > NSString(string: s2).length) {
  54. return .orderedDescending
  55. }else {
  56. return .orderedAscending
  57. }
  58. }
  59. return NSString(string: s1).compare(s2)
  60. }
  61. for item in NSArray(array: nitems) {
  62. let subFileName = item as! String
  63. let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
  64. let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
  65. let fileInfo = NSMutableDictionary.fileInfoWith(fileName, // 实际文档名
  66. refFilePath: (isDirectory.boolValue ? (subFileName+".jpg") : nil),
  67. resultPath: rComparePath,
  68. comparePath: cComparePath,
  69. objc: self)
  70. files.add(fileInfo)
  71. }
  72. }
  73. return files
  74. }
  75. }