KMFileThumbManager.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 default_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, default_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) {
  65. self.updateFile(fileURL, default_IconSize)
  66. }
  67. public func updateFile(_ fileURL: URL, _ iconSize: CGSize) {
  68. let filePath = fileURL.path
  69. let fileName = filePath.getLastComponentDeleteExtension
  70. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  71. if let image = generateThumbnail(for: fileURL, iconSize) {
  72. guard let data = image.tiffRepresentation else {
  73. return
  74. }
  75. let imageRep = NSBitmapImageRep(data: data)
  76. imageRep?.size = image.size
  77. if let imageData = imageRep?.representation(using: .png, properties: [:]) {
  78. try?imageData.write(to: URL(fileURLWithPath: resultPath))
  79. }
  80. }
  81. }
  82. }
  83. public func getFileThumb(_ fileURL: URL) -> NSImage? {
  84. let filePath = fileURL.path
  85. let fileName = filePath.getLastComponentDeleteExtension
  86. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  87. return NSImage(contentsOf: URL(fileURLWithPath: resultPath))
  88. }
  89. return nil
  90. }
  91. @objc func tabClosingNotification(_ sender: Notification) {
  92. self.refreshData()
  93. }
  94. }