Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Limitation of loaded bytes to prevent hangs on large files #37

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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:
Expand Down