This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
RNFileDownloadSessionManager.m
76 lines (62 loc) · 3.3 KB
/
RNFileDownloadSessionManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// RNFileDownloadSessionManager.m
// RNFileDownload
//
// Created by Luavis Kang on 4/2/16.
// Copyright © 2016 Perry Poon. All rights reserved.
//
#import "RNFileDownloadSessionManager.h"
@implementation RNFileDownloadSessionManager
- (instancetype)initWithTargetPath:(NSString *)targetPath downloadFileName:(NSString *)fileName bridge:(RCTBridge *)bridge callback:(RCTResponseSenderBlock)callback
{
self = [super init];
if (self) {
self.bridge = bridge;
self.callback = callback;
_targetPath = targetPath;
_downloadFileName = fileName;
}
return self;
}
#pragma mark - Session delegates
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
if(_downloadFileName == nil) {
_downloadFileName = downloadTask.response.suggestedFilename;
if(!self.downloadFileName)
_downloadFileName = [self randomStringWithLength:RANDOM_FILENAME_LENGTH];
}
if(bytesWritten > 0 && self.bridge != nil)
[self.bridge.eventDispatcher sendAppEventWithName:[@"RNFileDownloadProgress" stringByAppendingString:downloadTask.originalRequest.URL.absoluteString]
body:@{
@"filename": self.downloadFileName,
@"sourceUrl": downloadTask.originalRequest.URL.absoluteString,
@"targetPath": self.targetPath,
@"bytesWritten": @(bytesWritten),
@"totalBytesWritten": @(totalBytesWritten),
@"totalBytesExpectedToWrite": @(totalBytesExpectedToWrite),
}];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSURL *targetDirectoryURL = [NSURL fileURLWithPath:self.targetPath];
NSURL *targetURL = [targetDirectoryURL URLByAppendingPathComponent:self.downloadFileName];
[[NSFileManager defaultManager] moveItemAtURL:location
toURL:targetURL
error:nil];
self.callback(@[[NSNull null], targetURL.absoluteString]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if([error description] != nil) {
NSLog(@"RNFileDownload error: %@", [error description]);
self.callback(@[[error description]]);
}
}
#pragma mark - Tools
- (NSString *)randomStringWithLength: (int) len {
static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length])]];
}
return randomString;
}
@end