2015-11-19 00:20:09 +00:00
|
|
|
#import "Downloader.h"
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
@implementation DownloadParams
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-11-19 00:20:09 +00:00
|
|
|
@interface Downloader()
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
@property (copy) DownloadParams* params;
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2016-05-20 23:07:05 +00:00
|
|
|
@property (retain) NSURLSession* session;
|
|
|
|
@property (retain) NSURLSessionTask* task;
|
2015-11-19 00:20:09 +00:00
|
|
|
@property (retain) NSNumber* statusCode;
|
2016-05-31 09:46:03 +00:00
|
|
|
@property (retain) NSNumber* lastProgressValue;
|
2015-11-19 00:20:09 +00:00
|
|
|
@property (retain) NSNumber* contentLength;
|
|
|
|
@property (retain) NSNumber* bytesWritten;
|
|
|
|
|
|
|
|
@property (retain) NSFileHandle* fileHandle;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Downloader
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
- (void)downloadFile:(DownloadParams*)params
|
2015-11-19 00:20:09 +00:00
|
|
|
{
|
2015-11-23 16:29:25 +00:00
|
|
|
_params = params;
|
2016-05-30 22:13:50 +00:00
|
|
|
|
2015-11-19 00:20:09 +00:00
|
|
|
_bytesWritten = 0;
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
NSURL* url = [NSURL URLWithString:_params.fromUrl];
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil];
|
|
|
|
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
|
2015-11-19 00:20:09 +00:00
|
|
|
|
|
|
|
if (!_fileHandle) {
|
2016-05-26 19:46:06 +00:00
|
|
|
NSError* error = [NSError errorWithDomain:@"Downloader" code:NSURLErrorFileDoesNotExist
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat: @"Failed to create target file at path: %@", _params.toFile]}];
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
return _params.errorCallback(error);
|
2016-05-26 19:46:06 +00:00
|
|
|
} else {
|
|
|
|
[_fileHandle closeFile];
|
2015-11-19 00:20:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:07:05 +00:00
|
|
|
NSURLSessionConfiguration *config;
|
2016-05-26 22:51:04 +00:00
|
|
|
if (_params.background) {
|
2016-06-03 05:29:08 +00:00
|
|
|
NSString *uuid = [[NSUUID UUID] UUIDString];
|
|
|
|
config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:uuid];
|
2016-05-26 22:51:04 +00:00
|
|
|
} else {
|
|
|
|
config = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
|
}
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2016-06-03 22:59:08 +00:00
|
|
|
config.HTTPAdditionalHeaders = _params.headers;
|
|
|
|
|
2016-05-20 23:07:05 +00:00
|
|
|
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
|
|
|
|
_task = [_session downloadTaskWithURL:url];
|
|
|
|
[_task resume];
|
2015-11-19 00:20:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-26 19:46:06 +00:00
|
|
|
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
|
2015-11-19 00:20:09 +00:00
|
|
|
{
|
2016-05-20 23:07:05 +00:00
|
|
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)downloadTask.response;
|
2016-05-26 19:46:06 +00:00
|
|
|
if (!_statusCode) {
|
|
|
|
_statusCode = [NSNumber numberWithLong:httpResponse.statusCode];
|
|
|
|
_contentLength = [NSNumber numberWithLong:httpResponse.expectedContentLength];
|
|
|
|
return _params.beginCallback(_statusCode, _contentLength, httpResponse.allHeaderFields);
|
|
|
|
}
|
2015-11-19 00:20:09 +00:00
|
|
|
|
|
|
|
if ([_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]) {
|
|
|
|
[_fileHandle writeData:data];
|
|
|
|
|
2016-05-31 09:48:56 +00:00
|
|
|
_bytesWritten = [NSNumber numberWithLong:[_bytesWritten longValue] + data.length];
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2016-05-31 09:46:03 +00:00
|
|
|
if (_params.progressDivider <= 1) {
|
2016-06-08 16:52:08 +00:00
|
|
|
return _params.progressCallback(_contentLength, _bytesWritten);
|
2016-05-31 09:46:03 +00:00
|
|
|
} else {
|
2016-06-08 16:52:08 +00:00
|
|
|
double doubleBytesWritten = (double)[_bytesWritten longValue];
|
|
|
|
double doubleContentLength = (double)[_contentLength longValue];
|
|
|
|
double doublePercents = doubleBytesWritten / doubleContentLength * 100;
|
|
|
|
NSNumber* progress = [NSNumber numberWithUnsignedInt: floor(doublePercents)];
|
|
|
|
if ([progress unsignedIntValue] % [_params.progressDivider integerValue] == 0) {
|
|
|
|
if (([progress unsignedIntValue] != [_lastProgressValue unsignedIntValue]) || ([_bytesWritten unsignedIntegerValue] == [_contentLength longValue])) {
|
|
|
|
NSLog(@"---Progress callback EMIT--- %zu", [progress unsignedIntValue]);
|
|
|
|
_lastProgressValue = [NSNumber numberWithUnsignedInt:[progress unsignedIntValue]];
|
|
|
|
return _params.progressCallback(_contentLength, _bytesWritten);
|
2016-05-31 09:46:03 +00:00
|
|
|
}
|
2016-06-08 16:52:08 +00:00
|
|
|
}
|
2016-05-31 09:46:03 +00:00
|
|
|
}
|
2015-11-19 00:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:07:05 +00:00
|
|
|
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
|
2015-11-19 00:20:09 +00:00
|
|
|
{
|
2016-05-26 19:46:06 +00:00
|
|
|
NSURL *destURL = [NSURL fileURLWithPath:_params.toFile];
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
|
|
NSError *error = nil;
|
|
|
|
[fm removeItemAtURL:destURL error:nil]; // Remove file at destination path, if it exists
|
|
|
|
[fm moveItemAtURL:location toURL:destURL error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"RNFS download: unable to move tempfile to destination. %@, %@", error, error.userInfo);
|
|
|
|
}
|
2015-11-19 00:20:09 +00:00
|
|
|
|
2016-05-30 22:13:50 +00:00
|
|
|
return _params.completeCallback(_statusCode, _bytesWritten);
|
2015-11-23 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:07:05 +00:00
|
|
|
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionTask *)downloadTask didCompleteWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
return _params.errorCallback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-23 16:29:25 +00:00
|
|
|
- (void)stopDownload
|
|
|
|
{
|
2016-05-20 23:07:05 +00:00
|
|
|
[_task cancel];
|
2015-11-19 00:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|