forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodePushDownloadHandler.m
101 lines (81 loc) · 3.48 KB
/
CodePushDownloadHandler.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#import "CodePush.h"
@implementation CodePushDownloadHandler {
// Header chars used to determine if the file is a zip.
char _header[4];
}
- (id)init:(NSString *)downloadFilePath
progressCallback:(void (^)(long long, long long))progressCallback
doneCallback:(void (^)(BOOL))doneCallback
failCallback:(void (^)(NSError *err))failCallback {
self.outputFileStream = [NSOutputStream outputStreamToFileAtPath:downloadFilePath
append:NO];
self.receivedContentLength = 0;
self.progressCallback = progressCallback;
self.doneCallback = doneCallback;
self.failCallback = failCallback;
return self;
}
-(void)download:(NSString*)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
}
#pragma mark NSURLConnection Delegate Methods
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.expectedContentLength = response.expectedContentLength;
[self.outputFileStream open];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (self.receivedContentLength < 4) {
for (int i = 0; i < [data length]; i++) {
int headerOffset = (int)self.receivedContentLength + i;
if (headerOffset >= 4) {
break;
}
const char *bytes = [data bytes];
_header[headerOffset] = bytes[i];
}
}
self.receivedContentLength = self.receivedContentLength + [data length];
NSInteger bytesLeft = [data length];
do {
NSInteger bytesWritten = [self.outputFileStream write:[data bytes]
maxLength:bytesLeft];
if (bytesWritten == -1) {
break;
}
bytesLeft -= bytesWritten;
} while (bytesLeft > 0);
self.progressCallback(self.expectedContentLength, self.receivedContentLength);
// bytesLeft should not be negative.
assert(bytesLeft >= 0);
if (bytesLeft) {
[self.outputFileStream close];
[connection cancel];
self.failCallback([self.outputFileStream streamError]);
}
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[self.outputFileStream close];
self.failCallback(error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
// We should have received all of the bytes if this is called.
assert(self.receivedContentLength == self.expectedContentLength);
[self.outputFileStream close];
BOOL isZip = _header[0] == 'P' && _header[1] == 'K' && _header[2] == 3 && _header[3] == 4;
self.doneCallback(isZip);
}
@end