NSURLConnectionVCR / NSURLConnectionVCRQuicklook / VCRGetPreview.m

//  VCRGetPreview.m
//  NSURLConnectionVCR
//  Created by Martijn Thé on 2/27/12.
//  Copyright (c) 2012 martijnthe.nl. All rights reserved.

#import "VCRGetPreview.h"
#import  "NSURLConnectionVCR.h"

@interface VCRCache : NSObject <NSCoding>
@property (nonatomic, readwrite, strong) NSURLResponse* response;
@property (nonatomic, readwrite, strong) NSData* responseBody;

OSStatus VCRGetPreviewData(NSURL* url,
                           NSData *__autoreleasing *responseData,
                           NSURLResponse *__autoreleasing*response,
                           NSString *__autoreleasing *displayName,
                           NSString *__autoreleasing *mimeType,
                           NSString *__autoreleasing *utType,
                           NSString *__autoreleasing *encoding)
    NSData* archiveData = [NSData dataWithContentsOfURL:url];
    if (archiveData) {
        VCRCache* cache = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
        if (responseData) {
            *responseData = cache.responseBody;
        if (response) {
            *response = cache.response;
        if (displayName) {
            NSString* _displayName;
            if ([cache.response isKindOfClass:[NSHTTPURLResponse class]]) {
                _displayName = [NSString stringWithFormat:@"%@ [%i]", [[cache.response URL] absoluteString], [(NSHTTPURLResponse*)cache.response statusCode]];
            } else {
                _displayName = [[cache.response URL] absoluteString];
            *displayName = _displayName;
        if (encoding) {
            NSString* _encoding = [cache.response textEncodingName];
            if (_encoding == nil) _encoding = @"UTF-8";
            *encoding = _encoding;
        NSString* _mimeType = [cache.response MIMEType];
        if (_mimeType == nil) _mimeType = @"text/plain";
        if (mimeType) {
            *mimeType = _mimeType;
        if (utType) {
            CFStringRef supportedTypes[] = {kUTTypeHTML, kUTTypeXML, kUTTypeRTF, kUTTypePlainText, kUTTypeImage, kUTTypePDF, kUTTypeMovie, kUTTypeAudio, NULL};
            for (NSInteger idx = 0; idx < 9; ++idx) {
                *utType = (__bridge NSString*)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)_mimeType, supportedTypes[idx]);
                if (*utType != nil) {
        return noErr;
    } else {
        return -1;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.