2015-03-23 15:07:33 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-01-29 17:10:49 -08:00
|
|
|
|
|
|
|
#import "RCTImageDownloader.h"
|
2015-02-18 17:48:13 -08:00
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
#import "RCTImageLoader.h"
|
2015-07-14 04:06:17 -07:00
|
|
|
#import "RCTImageUtils.h"
|
2015-05-22 07:17:08 -07:00
|
|
|
#import "RCTLog.h"
|
2015-07-27 13:46:59 -07:00
|
|
|
#import "RCTNetworking.h"
|
2015-01-29 17:10:49 -08:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
@implementation RCTImageDownloader
|
2015-02-18 17:48:13 -08:00
|
|
|
{
|
2015-06-29 05:15:25 -07:00
|
|
|
NSURLCache *_cache;
|
2015-03-30 20:12:32 -07:00
|
|
|
dispatch_queue_t _processingQueue;
|
2015-02-18 17:48:13 -08:00
|
|
|
}
|
2015-01-29 17:10:49 -08:00
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-02-18 17:48:13 -08:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-06-29 05:15:25 -07:00
|
|
|
_cache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 diskCapacity:200 * 1024 * 1024 diskPath:@"React/RCTImageDownloader"];
|
2015-03-30 20:12:32 -07:00
|
|
|
_processingQueue = dispatch_queue_create("com.facebook.React.DownloadProcessingQueue", DISPATCH_QUEUE_SERIAL);
|
2015-02-18 17:48:13 -08:00
|
|
|
}
|
2015-06-29 05:15:25 -07:00
|
|
|
return self;
|
2015-02-18 17:48:13 -08:00
|
|
|
}
|
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
|
|
|
{
|
|
|
|
return [requestURL.scheme.lowercaseString hasPrefix:@"http"];
|
|
|
|
}
|
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
/**
|
|
|
|
* Downloads a block of raw data and returns it. Note that the callback block
|
|
|
|
* will not be executed on the same thread you called the method from, nor on
|
|
|
|
* the main thread. Returns a token that can be used to cancel the download.
|
|
|
|
*/
|
|
|
|
- (RCTImageLoaderCancellationBlock)downloadDataForURL:(NSURL *)url
|
2015-09-02 08:25:10 -07:00
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressBlock
|
|
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionBlock
|
2015-02-18 17:48:13 -08:00
|
|
|
{
|
2015-07-27 13:46:59 -07:00
|
|
|
if (![_bridge respondsToSelector:NSSelectorFromString(@"networking")]) {
|
|
|
|
RCTLogError(@"You need to import the RCTNetworking library in order to download remote images.");
|
|
|
|
return ^{};
|
|
|
|
}
|
2015-02-18 17:48:13 -08:00
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
__weak RCTImageDownloader *weakSelf = self;
|
|
|
|
RCTURLRequestCompletionBlock runBlocks = ^(NSURLResponse *response, NSData *data, NSError *error) {
|
2015-07-09 15:48:22 -01:00
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
|
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
|
|
|
if (httpResponse.statusCode != 200) {
|
|
|
|
data = nil;
|
|
|
|
error = [[NSError alloc] initWithDomain:NSURLErrorDomain
|
|
|
|
code:httpResponse.statusCode
|
|
|
|
userInfo:nil];
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 17:51:14 -08:00
|
|
|
|
2015-03-30 20:12:32 -07:00
|
|
|
dispatch_async(_processingQueue, ^{
|
2015-07-27 13:46:59 -07:00
|
|
|
completionBlock(error, data);
|
2015-03-30 20:12:32 -07:00
|
|
|
});
|
2015-02-18 17:48:13 -08:00
|
|
|
};
|
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:url];
|
|
|
|
{
|
|
|
|
NSCachedURLResponse *cachedResponse = [_cache cachedResponseForRequest:request];
|
|
|
|
if (cachedResponse) {
|
|
|
|
runBlocks(cachedResponse.response, cachedResponse.data, nil);
|
|
|
|
return ^{};
|
2015-02-18 17:51:14 -08:00
|
|
|
}
|
2015-07-27 13:46:59 -07:00
|
|
|
}
|
2015-02-18 17:48:13 -08:00
|
|
|
|
2015-07-27 13:46:59 -07:00
|
|
|
RCTDownloadTask *task = [_bridge.networking downloadTaskWithRequest:request completionBlock:^(NSURLResponse *response, NSData *data, NSError *error) {
|
|
|
|
if (response && !error) {
|
|
|
|
RCTImageDownloader *strongSelf = weakSelf;
|
|
|
|
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
|
|
|
|
[strongSelf->_cache storeCachedResponse:cachedResponse forRequest:request];
|
|
|
|
}
|
|
|
|
runBlocks(response, data, error);
|
2015-02-03 16:15:20 -08:00
|
|
|
}];
|
2015-07-27 13:46:59 -07:00
|
|
|
if (progressBlock) {
|
|
|
|
task.downloadProgressBlock = progressBlock;
|
|
|
|
}
|
|
|
|
return ^{ [task cancel]; };
|
2015-02-03 16:15:20 -08:00
|
|
|
}
|
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(UIViewContentMode)resizeMode
|
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
|
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
2015-06-29 05:15:25 -07:00
|
|
|
{
|
2015-09-02 08:25:10 -07:00
|
|
|
__block RCTImageLoaderCancellationBlock decodeCancel = nil;
|
2015-07-27 13:46:59 -07:00
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
__weak RCTImageDownloader *weakSelf = self;
|
|
|
|
RCTImageLoaderCancellationBlock downloadCancel = [self downloadDataForURL:imageURL progressHandler:progressHandler completionHandler:^(NSError *error, NSData *imageData) {
|
|
|
|
if (error) {
|
|
|
|
completionHandler(error, nil);
|
|
|
|
} else {
|
|
|
|
decodeCancel = [weakSelf.bridge.imageLoader decodeImageData:imageData size:size scale:scale resizeMode:resizeMode completionBlock:completionHandler];
|
2015-06-29 05:15:25 -07:00
|
|
|
}
|
2015-09-02 08:25:10 -07:00
|
|
|
}];
|
2015-06-29 05:15:25 -07:00
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
return ^{
|
|
|
|
downloadCancel();
|
2015-06-29 05:15:25 -07:00
|
|
|
|
2015-09-02 08:25:10 -07:00
|
|
|
if (decodeCancel) {
|
|
|
|
decodeCancel();
|
2015-06-29 05:15:25 -07:00
|
|
|
}
|
2015-09-02 08:25:10 -07:00
|
|
|
};
|
2015-06-29 05:15:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2015-07-27 13:46:59 -07:00
|
|
|
|
|
|
|
@implementation RCTBridge (RCTImageDownloader)
|
|
|
|
|
|
|
|
- (RCTImageDownloader *)imageDownloader
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTImageDownloader class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|