From 1c52fc0e8ee5160ac58b9280fd4a7be788e74d7b Mon Sep 17 00:00:00 2001 From: redstrate Date: Sun, 9 May 2021 22:06:58 -0400 Subject: [PATCH] Fix thumbnail provider --- Thumbnail/Thumbnail.entitlements | 8 +-- Thumbnail/ThumbnailProvider.swift | 104 ++++++++++++++---------------- 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/Thumbnail/Thumbnail.entitlements b/Thumbnail/Thumbnail.entitlements index f2ef3ae..18aff0c 100644 --- a/Thumbnail/Thumbnail.entitlements +++ b/Thumbnail/Thumbnail.entitlements @@ -2,9 +2,9 @@ - com.apple.security.app-sandbox - - com.apple.security.files.user-selected.read-only - + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + diff --git a/Thumbnail/ThumbnailProvider.swift b/Thumbnail/ThumbnailProvider.swift index ce0c2d6..1a34b1f 100644 --- a/Thumbnail/ThumbnailProvider.swift +++ b/Thumbnail/ThumbnailProvider.swift @@ -4,63 +4,55 @@ import Cocoa class ThumbnailProvider: QLThumbnailProvider { override func provideThumbnail(for request: QLFileThumbnailRequest, _ handler: @escaping (QLThumbnailReply?, Error?) -> Void) { - let fc: NSFileCoordinator = NSFileCoordinator() - let intent: NSFileAccessIntent = NSFileAccessIntent.readingIntent(with: request.fileURL) - fc.coordinate(with: [intent], queue: .main) { (err) in - if err == nil { - do { - guard let archive = Archive(data: try Data(contentsOf: intent.url), accessMode: Archive.AccessMode.read) else { - return - } - - guard let entry = archive[ThumbnailPath] else { - return - } - - guard let thumbnailData = readData(archive: archive, entry: entry) else { - return - } - - let image = NSImage(data: thumbnailData) - - let maximumSize = request.maximumSize - let imageSize = image?.size - - var newImageSize = maximumSize - var contextSize = maximumSize - let aspectRatio = imageSize!.height / imageSize!.width - let proposedHeight = aspectRatio * maximumSize.width - - if proposedHeight <= maximumSize.height { - newImageSize.height = proposedHeight - contextSize.height = max(proposedHeight.rounded(.down), request.minimumSize.height) - } else { - newImageSize.width = maximumSize.height / aspectRatio - contextSize.width = max(newImageSize.width.rounded(.down), request.minimumSize.width) - } - - let reply: QLThumbnailReply = QLThumbnailReply.init(contextSize: contextSize) { () -> Bool in - if image != nil { - image!.draw(in: CGRect(x: contextSize.width/2 - newImageSize.width/2, - y: contextSize.height/2 - newImageSize.height/2, - width: newImageSize.width, - height: newImageSize.height)); - - return true - } else { - return false - } - } - - handler(reply, nil) - } catch { - NSLog("Could not load file \(intent.url.lastPathComponent) to preview it") - handler(nil, nil) - } - } else { - NSLog("Could not find file \(intent.url.lastPathComponent) to preview it") - handler(nil, nil) + do { + guard let archive = Archive(data: try Data(contentsOf: request.fileURL), accessMode: Archive.AccessMode.read) else { + return } + + guard let entry = archive[ThumbnailPath] else { + return + } + + guard let thumbnailData = readData(archive: archive, entry: entry) else { + return + } + + let image = NSImage(data: thumbnailData) + + let maximumSize = request.maximumSize + let imageSize = image?.size + + var newImageSize = maximumSize + var contextSize = maximumSize + let aspectRatio = imageSize!.height / imageSize!.width + let proposedHeight = aspectRatio * maximumSize.width + + if proposedHeight <= maximumSize.height { + newImageSize.height = proposedHeight + contextSize.height = max(proposedHeight.rounded(.down), request.minimumSize.height) + } else { + newImageSize.width = maximumSize.height / aspectRatio + contextSize.width = max(newImageSize.width.rounded(.down), request.minimumSize.width) + } + + let reply: QLThumbnailReply = QLThumbnailReply.init(contextSize: contextSize) { () -> Bool in + if image != nil { + image!.draw(in: CGRect(x: contextSize.width/2 - newImageSize.width/2, + y: contextSize.height/2 - newImageSize.height/2, + width: newImageSize.width, + height: newImageSize.height)); + + return true + } else { + return false + } + } + + handler(reply, nil) + } catch { + NSLog("Could not access file \(request.fileURL.lastPathComponent) to preview it") + handler(nil, nil) } } } +