KMFileThumbManager.swift 4.4 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. if (!FileManager.default.fileExists(atPath: thumbFolderPath!)) {
  16. try?FileManager.default.createDirectory(atPath: thumbFolderPath!, withIntermediateDirectories: true, attributes: nil)
  17. }
  18. refreshData()
  19. }
  20. private func refreshData() {
  21. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  22. var fileNames: [String] = []
  23. for url in urls {
  24. let filePath = url.path
  25. let fileName = filePath.getLastComponentDeleteExtension
  26. let resultPath = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png"))
  27. if (!FileManager.default.fileExists(atPath: resultPath!)) {
  28. updateFile(url, iconSize)
  29. }
  30. fileNames.append((fileName + ".png"))
  31. }
  32. if let contents = try?FileManager.default.contentsOfDirectory(atPath: thumbFolderPath!) {
  33. for name in contents {
  34. if name != ".DS_Store" {
  35. if let index = fileNames.firstIndex(of: name) {
  36. } else {
  37. if let path = thumbFolderPath?.stringByAppendingPathComponent(name) {
  38. try?FileManager.default.removeItem(atPath: path)
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. private func generateThumbnail(for pdfURL: URL, _ size: NSSize) -> NSImage? {
  46. guard let pdfDocument = CGPDFDocument(pdfURL as CFURL),
  47. let pdfPage = pdfDocument.page(at: 1) else {
  48. return nil
  49. }
  50. let pageRect = pdfPage.getBoxRect(.mediaBox)
  51. // 创建图形上下文
  52. let bitsPerComponent = 8
  53. let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
  54. let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: bitsPerComponent, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo)
  55. // 绘制PDF页面
  56. // context?.saveGState()
  57. // context?.translateBy(x: 0, y: size.height)
  58. // context?.scaleBy(x: 1.0, y: -1.0) // 反转坐标系
  59. context?.scaleBy(x: size.width / pageRect.width, y: size.height / pageRect.height) // 缩放
  60. context?.drawPDFPage(pdfPage)
  61. context?.restoreGState()
  62. // 创建NSImage
  63. if let cgImage = context?.makeImage() {
  64. return NSImage(cgImage: cgImage, size: NSSize(width: size.width, height: size.height))
  65. }
  66. return nil
  67. }
  68. public func updateFile(_ fileURL: URL, _ iconSize: CGSize) {
  69. let filePath = fileURL.path
  70. let fileName = filePath.getLastComponentDeleteExtension
  71. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  72. if let image = generateThumbnail(for: fileURL, iconSize) {
  73. guard let data = image.tiffRepresentation else {
  74. return
  75. }
  76. let imageRep = NSBitmapImageRep(data: data)
  77. imageRep?.size = image.size
  78. if let imageData = imageRep?.representation(using: .png, properties: [:]) {
  79. try?imageData.write(to: URL(fileURLWithPath: resultPath))
  80. }
  81. }
  82. }
  83. }
  84. public func getFileThumb(_ fileURL: URL) -> NSImage? {
  85. let filePath = fileURL.path
  86. let fileName = filePath.getLastComponentDeleteExtension
  87. if let resultPath: String = thumbFolderPath?.stringByAppendingPathComponent((fileName + ".png")) {
  88. return NSImage(contentsOf: URL(fileURLWithPath: resultPath))
  89. }
  90. return nil
  91. }
  92. }