PDFCompareTest.swift 3.7 KB

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