iOS downloadFile implementation now async

This commit is contained in:
Chris Dell 2015-11-19 00:20:09 +00:00
parent ab918fa2c8
commit 5fbeb83a66
3 changed files with 104 additions and 10 deletions

10
Downloader.h Normal file
View File

@ -0,0 +1,10 @@
#import <Foundation/Foundation.h>
typedef void (^ErrorCallback)(NSError*);
typedef void (^DownloaderCallback)(NSNumber*, NSNumber*, NSNumber*);
@interface Downloader : NSObject <NSURLConnectionDelegate>
- (void)downloadFile:(NSString*)urlStr toFile:(NSString*)downloadPath callback:(DownloaderCallback)callback errorCallback:(ErrorCallback)errorCallback progressCallback:(DownloaderCallback)progressCallback;
@end

83
Downloader.m Normal file
View File

@ -0,0 +1,83 @@
#import "Downloader.h"
@interface Downloader()
@property (copy) DownloaderCallback callback;
@property (copy) ErrorCallback errorCallback;
@property (copy) DownloaderCallback progressCallback;
@property (retain) NSNumber* statusCode;
@property (retain) NSNumber* contentLength;
@property (retain) NSNumber* bytesWritten;
@property (retain) NSFileHandle* fileHandle;
@end
@implementation Downloader
- (void)downloadFile:(NSString*)urlStr toFile:(NSString*)downloadPath callback:(DownloaderCallback)callback errorCallback:(ErrorCallback)errorCallback progressCallback:(DownloaderCallback)progressCallback
{
_callback = callback;
_errorCallback = errorCallback;
_progressCallback = progressCallback;
_bytesWritten = 0;
NSURL* url = [NSURL URLWithString:urlStr];
NSMutableURLRequest* downloadRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30];
[[NSFileManager defaultManager] createFileAtPath:downloadPath contents:nil attributes:nil];
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:downloadPath];
if (!_fileHandle) {
NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to create target file at path: %@", downloadPath]}];
return _errorCallback(error);
}
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:downloadRequest delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[connection start];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[_fileHandle closeFile];
return _errorCallback(error);
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
NSHTTPURLResponse* httpUrlResponse = (NSHTTPURLResponse*)response;
_statusCode = [NSNumber numberWithLong:httpUrlResponse.statusCode];
_contentLength = [NSNumber numberWithLong: httpUrlResponse.expectedContentLength];
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
if ([_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]) {
[_fileHandle writeData:data];
_bytesWritten = [NSNumber numberWithUnsignedInteger:[_bytesWritten unsignedIntegerValue] + data.length];
return _progressCallback(_statusCode, _contentLength, _bytesWritten);
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
[_fileHandle closeFile];
return _callback(_statusCode, _contentLength, _bytesWritten);
}
@end

View File

@ -9,6 +9,7 @@
#import "RNFSManager.h"
#import "RCTBridge.h"
#import "NSArray+Map.h"
#import "Downloader.h"
@implementation RNFSManager
@ -142,22 +143,22 @@ RCT_EXPORT_METHOD(downloadFile:(NSString *)urlStr
filepath:(NSString *)filepath
callback:(RCTResponseSenderBlock)callback)
{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:urlStr];
NSData *urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
DownloaderCallback downloaderSuccessCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSNumber* bytesWritten) {
return callback(@[[NSNull null], [NSNumber numberWithBool:YES], filepath]);
};
if (error) {
ErrorCallback downloaderErrorCallback = ^(NSError* error) {
return callback([self makeErrorPayload:error]);
}
};
BOOL success = [urlData writeToFile:filepath atomically:YES];
DownloaderCallback downloaderProgressCallback = ^(NSNumber* statusCode, NSNumber* contentLength, NSNumber* bytesWritten) {
if (!success) {
return callback(@[[NSString stringWithFormat:@"Could not write downloaded data to file at path %@", filepath]]);
}
};
callback(@[[NSNull null], [NSNumber numberWithBool:success], filepath]);
Downloader* downloader = [Downloader alloc];
[downloader downloadFile:urlStr toFile:filepath callback:downloaderSuccessCallback errorCallback:downloaderErrorCallback progressCallback:downloaderProgressCallback];
}
RCT_EXPORT_METHOD(pathForBundle:(NSString *)bundleNamed