mirror of
https://github.com/status-im/react-native-fs.git
synced 2026-08-27 18:11:06 +00:00
First pass at native code to support background downloads
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@ typedef void (^ProgressCallback)(NSNumber*, NSNumber*);
|
||||
|
||||
@end
|
||||
|
||||
@interface Downloader : NSObject <NSURLConnectionDelegate>
|
||||
@interface Downloader : NSObject <NSURLSessionDelegate, NSURLSessionDownloadDelegate>
|
||||
|
||||
- (void)downloadFile:(DownloadParams*)params;
|
||||
- (void)stopDownload;
|
||||
|
||||
+24
-26
@@ -8,7 +8,8 @@
|
||||
|
||||
@property (copy) DownloadParams* params;
|
||||
|
||||
@property (retain) NSURLConnection* connection;
|
||||
@property (retain) NSURLSession* session;
|
||||
@property (retain) NSURLSessionTask* task;
|
||||
@property (retain) NSNumber* statusCode;
|
||||
@property (retain) NSNumber* contentLength;
|
||||
@property (retain) NSNumber* bytesWritten;
|
||||
@@ -27,12 +28,7 @@
|
||||
|
||||
NSURL* url = [NSURL URLWithString:_params.fromUrl];
|
||||
|
||||
NSMutableURLRequest* downloadRequest = [NSMutableURLRequest requestWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
timeoutInterval:30];
|
||||
|
||||
[[NSFileManager defaultManager] createFileAtPath:_params.toFile contents:nil attributes:nil];
|
||||
|
||||
_fileHandle = [NSFileHandle fileHandleForWritingAtPath:_params.toFile];
|
||||
|
||||
if (!_fileHandle) {
|
||||
@@ -41,31 +37,26 @@
|
||||
return _params.errorCallback(error);
|
||||
}
|
||||
|
||||
_connection = [[NSURLConnection alloc] initWithRequest:downloadRequest delegate:self startImmediately:NO];
|
||||
NSURLSessionConfiguration *config;
|
||||
config = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||||
// TODO: use the following config for session objects:
|
||||
//config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:fromUrl];
|
||||
|
||||
[_connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
|
||||
[_connection start];
|
||||
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
|
||||
_task = [_session downloadTaskWithURL:url];
|
||||
[_task resume];
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
|
||||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didReceiveResponse:(NSURLResponse *)response
|
||||
{
|
||||
[_fileHandle closeFile];
|
||||
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)downloadTask.response;
|
||||
_statusCode = [NSNumber numberWithLong:httpResponse.statusCode];
|
||||
_contentLength = [NSNumber numberWithLong:httpResponse.expectedContentLength];
|
||||
|
||||
return _params.errorCallback(error);
|
||||
return _params.beginCallback(_statusCode, _contentLength, httpResponse.allHeaderFields);
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
|
||||
{
|
||||
NSHTTPURLResponse* httpUrlResponse = (NSHTTPURLResponse*)response;
|
||||
|
||||
_statusCode = [NSNumber numberWithLong:httpUrlResponse.statusCode];
|
||||
_contentLength = [NSNumber numberWithLongLong: httpUrlResponse.expectedContentLength];
|
||||
|
||||
return _params.beginCallback(_statusCode, _contentLength, httpUrlResponse.allHeaderFields);
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
|
||||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(NSData *)data
|
||||
{
|
||||
if ([_statusCode isEqualToNumber:[NSNumber numberWithInt:200]]) {
|
||||
[_fileHandle writeData:data];
|
||||
@@ -76,16 +67,23 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
|
||||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
|
||||
{
|
||||
[_fileHandle closeFile];
|
||||
|
||||
return _params.completeCallback(_statusCode, _bytesWritten);
|
||||
}
|
||||
|
||||
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionTask *)downloadTask didCompleteWithError:(NSError *)error
|
||||
{
|
||||
[_fileHandle closeFile];
|
||||
return _params.errorCallback(error);
|
||||
}
|
||||
|
||||
|
||||
- (void)stopDownload
|
||||
{
|
||||
[_connection cancel];
|
||||
[_task cancel];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user