-
Notifications
You must be signed in to change notification settings - Fork 29
/
LADownload.m
113 lines (84 loc) · 3.76 KB
/
LADownload.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
102
103
104
105
106
107
108
109
110
111
112
113
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Adam Ziolkowski <[email protected]> and Leon Handreke <[email protected]>
* wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy us a beer in return.
* ----------------------------------------------------------------------------
*/
#import "LADownload.h"
@implementation LADownload
@synthesize request, delegate, destination, receivedLength, totalLength, fileHandle;
- (LADownload *) initWithRequest: (NSURLRequest *) newRequest destination: (NSString *) newDestination delegate: (id) newDelegate {
if ( self = [super init]) {
[self setRequest: newRequest];
[self setDelegate: newDelegate];
[self setDestination: [newDestination stringByAppendingPathExtension:@"download"]];
// Make sure the directory is there
[[NSFileManager defaultManager] createDirectoryAtPath: [self destination] withIntermediateDirectories: YES attributes: nil error: nil];
}
return self;
}
- (double) progress {
return receivedLength / totalLength;
}
- (void) start {
connection = [NSURLConnection connectionWithRequest:[self request] delegate: self];
[connection start];
//NSLog(@"Starting DL %@", connection);
}
- (void) cancel {
[connection cancel];
[fileHandle closeFile];
[[NSFileManager defaultManager] removeItemAtPath: [self destination] error: nil];
}
#pragma mark NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSFileManager *manager = [NSFileManager defaultManager];
//DebugLog([self destination]);
if( [manager fileExistsAtPath: [self destination]] )
{
[manager removeItemAtPath:[self destination] error:nil];
}
[manager createFileAtPath: [self destination] contents:nil attributes:nil];
fileHandle = [NSFileHandle fileHandleForWritingAtPath:[self destination]];
downloadedData = [[NSMutableData alloc] init];
totalLength = [response expectedContentLength];
receivedLength = 0;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//NSLog(@"REceived Data");
//DebugLog(@"Received Data of length %d from connection", [data length]);
[downloadedData appendData:data];
receivedLength += data.length;
//NSTimeInterval elapsedTime = -[startTime timeIntervalSinceNow];
if( [delegate respondsToSelector:@selector(download:didReceiveDataOfLength:)] )
{
[delegate download: self didReceiveDataOfLength: data.length];
}
//can't keep too much data in memory. Write it to disk to avoid getting low memory error.
if( downloadedData.length > 1048576 && fileHandle != nil )
{
[fileHandle writeData: downloadedData];
downloadedData = [[NSMutableData alloc] init];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[fileHandle writeData: downloadedData];
downloadedData = nil;
NSString *newDestination = [[self destination] stringByDeletingPathExtension];
// Delete the old file in case it's still there
[[NSFileManager defaultManager] removeItemAtPath: newDestination error: nil];
//Move the file to it's 'complete' location
[[NSFileManager defaultManager] moveItemAtPath: [self destination] toPath: newDestination error: nil];
if( [delegate respondsToSelector:@selector(downloadDidFinish:)] )
{
[delegate downloadDidFinish: self];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@", [error userInfo]);
}
@end