KMFileThumbManager.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // KMFileThumbManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/22.
  6. //
  7. import Cocoa
  8. import CryptoKit
  9. class KMFileThumbManager: NSObject {
  10. let thumbFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("thumb")
  11. @objc public static let manager = KMFileThumbManager()
  12. private var iconSize: CGSize = CGSizeMake(126*3, 168*3)
  13. override init() {
  14. super.init()
  15. let tabClosingNoti = NSNotification.Name("CTTabClosingNotification")
  16. NotificationCenter.default.addObserver(self, selector: #selector(tabClosingNotification), name: tabClosingNoti, object: nil)
  17. refreshData()
  18. }
  19. func refreshData() {
  20. if let path = thumbFolderPath {
  21. try?FileManager.default.removeItem(atPath: path)
  22. if (!FileManager.default.fileExists(atPath: path)) {
  23. try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
  24. }
  25. }
  26. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  27. var fileNames: [String] = []
  28. for url in urls {
  29. let filePath = url.path
  30. let fileName = filePath.getLastComponentDeleteExtension
  31. let resultPath = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png"))
  32. if (!FileManager.default.fileExists(atPath: resultPath!)) {
  33. updateFile(url, iconSize)
  34. }
  35. fileNames.append((fileName + ".png"))
  36. }
  37. if let contents = try?FileManager.default.contentsOfDirectory(atPath: thumbFolderPath!) {
  38. for name in contents {
  39. if name != ".DS_Store" {
  40. if let index = fileNames.firstIndex(of: name) {
  41. } else {
  42. if let path = thumbFolderPath?.stringByAppendingPathComponent(name) {
  43. try?FileManager.default.removeItem(atPath: path)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. private func generateThumbnail(for pdfURL: URL, _ size: NSSize) -> NSImage? {
  51. guard let pdfDocument = PDFDocument(url: pdfURL),
  52. let pdfPage = pdfDocument.page(at: 0) else {
  53. return nil
  54. }
  55. if pdfDocument.isLocked || pdfDocument.isEncrypted {
  56. return NSImage(named: "file_lock")
  57. }
  58. let pdfPageBounds = pdfPage.bounds(for: .mediaBox)
  59. let scale = min(size.width / pdfPageBounds.width, size.height / pdfPageBounds.height)
  60. let thumbnailSize = CGSize(width: pdfPageBounds.width * scale, height: pdfPageBounds.height * scale)
  61. let thumbnail = pdfPage.thumbnail(of: thumbnailSize, for: .mediaBox)
  62. return thumbnail
  63. }
  64. public func updateFile(_ fileURL: URL, _ iconSize: CGSize) {
  65. let filePath = fileURL.path
  66. let fileName = filePath.getLastComponentDeleteExtension
  67. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  68. if let image = generateThumbnail(for: fileURL, iconSize) {
  69. guard let data = image.tiffRepresentation else {
  70. return
  71. }
  72. let imageRep = NSBitmapImageRep(data: data)
  73. imageRep?.size = image.size
  74. if let imageData = imageRep?.representation(using: .png, properties: [:]) {
  75. try?imageData.write(to: URL(fileURLWithPath: resultPath))
  76. }
  77. }
  78. }
  79. }
  80. public func getFileThumb(_ fileURL: URL) -> NSImage? {
  81. let filePath = fileURL.path
  82. let fileName = filePath.getLastComponentDeleteExtension
  83. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  84. return NSImage(contentsOf: URL(fileURLWithPath: resultPath))
  85. }
  86. return nil
  87. }
  88. @objc func tabClosingNotification(_ sender: Notification) {
  89. self.refreshData()
  90. }
  91. }