diff options
Diffstat (limited to 'iPhone/CordovaLib/Classes/CDVURLProtocol.m')
-rwxr-xr-x | iPhone/CordovaLib/Classes/CDVURLProtocol.m | 83 |
1 files changed, 52 insertions, 31 deletions
diff --git a/iPhone/CordovaLib/Classes/CDVURLProtocol.m b/iPhone/CordovaLib/Classes/CDVURLProtocol.m index a645288..1959c77 100755 --- a/iPhone/CordovaLib/Classes/CDVURLProtocol.m +++ b/iPhone/CordovaLib/Classes/CDVURLProtocol.m @@ -17,14 +17,17 @@ under the License. */ +#import <AssetsLibrary/ALAsset.h> +#import <AssetsLibrary/ALAssetRepresentation.h> +#import <AssetsLibrary/ALAssetsLibrary.h> +#import <MobileCoreServices/MobileCoreServices.h> #import "CDVURLProtocol.h" #import "CDVCommandQueue.h" #import "CDVWhitelist.h" #import "CDVViewController.h" +#import "CDVFile.h" @interface CDVHTTPURLResponse : NSHTTPURLResponse -- (id)initWithUnauthorizedURL:(NSURL*)url; -- (id)initWithBlankResponse:(NSURL*)url; @property (nonatomic) NSInteger statusCode; @end @@ -106,7 +109,9 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request) NSURL* theUrl = [theRequest URL]; CDVViewController* viewController = viewControllerForRequest(theRequest); - if (viewController != nil) { + if ([[theUrl absoluteString] hasPrefix:kCDVAssetsLibraryPrefix]) { + return YES; + } else if (viewController != nil) { if ([[theUrl path] isEqualToString:@"/!gap_exec"]) { NSString* queuedCommandsJSON = [theRequest valueForHTTPHeaderField:@"cmds"]; NSString* requestId = [theRequest valueForHTTPHeaderField:@"rc"]; @@ -151,21 +156,35 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request) NSURL* url = [[self request] URL]; if ([[url path] isEqualToString:@"/!gap_exec"]) { - CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithBlankResponse:url]; - [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - [[self client] URLProtocolDidFinishLoading:self]; + [self sendResponseWithResponseCode:200 data:nil mimeType:nil]; + return; + } else if ([[url absoluteString] hasPrefix:kCDVAssetsLibraryPrefix]) { + ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset * asset) { + if (asset) { + // We have the asset! Get the data and send it along. + ALAssetRepresentation* assetRepresentation = [asset defaultRepresentation]; + NSString* MIMEType = (__bridge_transfer NSString*)UTTypeCopyPreferredTagWithClass ((__bridge CFStringRef)[assetRepresentation UTI], kUTTagClassMIMEType); + Byte* buffer = (Byte*)malloc ([assetRepresentation size]); + NSUInteger bufferSize = [assetRepresentation getBytes:buffer fromOffset:0.0 length:[assetRepresentation size] error:nil]; + NSData* data = [NSData dataWithBytesNoCopy:buffer length:bufferSize freeWhenDone:YES]; + [self sendResponseWithResponseCode:200 data:data mimeType:MIMEType]; + } else { + // Retrieving the asset failed for some reason. Send an error. + [self sendResponseWithResponseCode:404 data:nil mimeType:nil]; + } + }; + ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError * error) { + // Retrieving the asset failed for some reason. Send an error. + [self sendResponseWithResponseCode:401 data:nil mimeType:nil]; + }; + + ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init]; + [assetsLibrary assetForURL:url resultBlock:resultBlock failureBlock:failureBlock]; return; } NSString* body = [gWhitelist errorStringForURL:url]; - - CDVHTTPURLResponse* response = [[CDVHTTPURLResponse alloc] initWithUnauthorizedURL:url]; - - [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - - [[self client] URLProtocol:self didLoadData:[body dataUsingEncoding:NSASCIIStringEncoding]]; - - [[self client] URLProtocolDidFinishLoading:self]; + [self sendResponseWithResponseCode:401 data:[body dataUsingEncoding:NSASCIIStringEncoding] mimeType:nil]; } - (void)stopLoading @@ -178,29 +197,31 @@ static CDVViewController *viewControllerForRequest(NSURLRequest* request) return NO; } -@end - -@implementation CDVHTTPURLResponse -@synthesize statusCode; - -- (id)initWithUnauthorizedURL:(NSURL*)url +- (void)sendResponseWithResponseCode:(NSInteger)statusCode data:(NSData*)data mimeType:(NSString*)mimeType { - self = [super initWithURL:url MIMEType:@"text/plain" expectedContentLength:-1 textEncodingName:@"UTF-8"]; - if (self) { - self.statusCode = 401; + if (mimeType == nil) { + mimeType = @"text/plain"; } - return self; -} + NSString* encodingName = [@"text/plain" isEqualToString:mimeType] ? @"UTF-8" : nil; + CDVHTTPURLResponse* response = + [[CDVHTTPURLResponse alloc] initWithURL:[[self request] URL] + MIMEType:mimeType + expectedContentLength:[data length] + textEncodingName:encodingName]; + response.statusCode = statusCode; -- (id)initWithBlankResponse:(NSURL*)url -{ - self = [super initWithURL:url MIMEType:@"text/plain" expectedContentLength:-1 textEncodingName:@"UTF-8"]; - if (self) { - self.statusCode = 200; + [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + if (data != nil) { + [[self client] URLProtocol:self didLoadData:data]; } - return self; + [[self client] URLProtocolDidFinishLoading:self]; } +@end + +@implementation CDVHTTPURLResponse +@synthesize statusCode; + - (NSDictionary*)allHeaderFields { return nil; |