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 cac9254..db9ee7c 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,16 @@ Compiling 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: