From cb719c9569a4e18421a45db3138742282fd63b99 Mon Sep 17 00:00:00 2001 From: Sebastian Willenborg Date: Sun, 16 Nov 2014 18:07:34 +0100 Subject: [PATCH] New Feature: Limitation of loaded bytes to prevent hangs on large files --- .../GeneratePreviewForURL.m | 33 +++++++++++++++++++ README.md | 10 ++++++ 2 files changed, 43 insertions(+) diff --git a/QuickLookStephenProject/GeneratePreviewForURL.m b/QuickLookStephenProject/GeneratePreviewForURL.m index 0edfc74..b3147bd 100644 --- a/QuickLookStephenProject/GeneratePreviewForURL.m +++ b/QuickLookStephenProject/GeneratePreviewForURL.m @@ -6,6 +6,7 @@ #import "QLSFileAttributes.h" +#define DEFAULT_MAX_FILE_SIZE 1024 * 100 // Generate a preview for the document with the given url OSStatus GeneratePreviewForURL(void *thisInterface, @@ -40,6 +41,38 @@ OSStatus GeneratePreviewForURL(void *thisInterface, (NSString *)kQLPreviewPropertyHeightKey : @800 }; + // Get size of current File + NSFileManager *man = [NSFileManager defaultManager]; + NSURL *file_url = (__bridge NSURL *)(url); + NSDictionary *attrs = [man attributesOfItemAtPath: [file_url path] error: NULL]; + + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + + // the plugin is running as com.apple.quicklook.satellite therefore we need to load our own settings + NSDictionary *defaults = [userDefaults persistentDomainForName:@"com.whomwah.quicklookstephen"]; + + long long maxFileSizeSetting = [[defaults valueForKey:@"maxFileSize"] longLongValue]; + unsigned long long maxFileSize = DEFAULT_MAX_FILE_SIZE; + if(maxFileSizeSetting > 0) { + maxFileSize = maxFileSizeSetting; + } + + // Display less data, if file is too big + if(attrs.fileSize > maxFileSize) { + NSFileHandle *myFile= [NSFileHandle fileHandleForReadingAtPath:[file_url path]]; + if(!myFile) { + return noErr; + } + NSData *displayData = [myFile readDataOfLength:maxFileSize]; + [myFile closeFile]; + + QLPreviewRequestSetDataRepresentation( + request, + (__bridge CFDataRef)displayData, + kUTTypePlainText, + (__bridge CFDictionaryRef)previewProperties); + return noErr; + } QLPreviewRequestSetURLRepresentation( request, url, diff --git a/README.md b/README.md index 6189679..7f99498 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,16 @@ Compliling the project yourself? Just copy the generated `QLStephen.qlgenerator` file into the relevant `QuickLook` folder (as above). +## Settings + +### Maximum file size + +To keep quickview fast the priview is limited in its number of shown bytes. +The default value is 100kB. + + defaults write com.whomwah.quicklookstephen maxFileSize 102400 + + ## Trouble? If you’ve installed the plugin, but don’t see any changes: