Skip to content

Commit

Permalink
Merge pull request #37 from comawill/pull-req/limit-size
Browse files Browse the repository at this point in the history
New Feature: Limitation of loaded bytes to prevent hangs on large files
  • Loading branch information
tsdorsey committed Apr 19, 2016
2 parents 59eca27 + cb719c9 commit 1468453
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions QuickLookStephenProject/GeneratePreviewForURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1468453

Please sign in to comment.