-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFileDownloader.m
152 lines (127 loc) · 5.59 KB
/
FileDownloader.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// FileDownloader.m
//
// Created by Jiva DeVoe on 3/15/10.
// Copyright 2010 Random Ideas,LLC. All rights reserved.
//
#import "FileDownloader.h"
#import "NSMutableURLRequest+Posts.h"
@interface FileDownloader ()
@property (nonatomic) int statusCode;
@property (nonatomic) float expectedContentLenght;
@end
@implementation FileDownloader
@synthesize statusCode;
@synthesize delegate;
@synthesize localName;
@synthesize connection;
@synthesize data;
@synthesize url;
@synthesize completionBlock;
@synthesize failureBlock;
+(id)downloadRequest:(NSMutableURLRequest *)inReq toLocalFileNamed:(NSString *)inLocalName completionBlock:(void (^)())inCompletionBlock failureBlock:(void (^)(NSError *))inFailureBlock;
{
return [[self alloc] initWithRequest:inReq toLocalFileNamed:inLocalName forDelegate:nil completionBlock:inCompletionBlock failureBlock:inFailureBlock];
}
+(id)downloadUrl:(NSURL *)inUrl toLocalFileNamed:(NSString *)inLocalName completionBlock:(void (^)())inCompletionBlock failureBlock:(void (^)(NSError *))inFailureBlock;
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:inUrl];
[req setTimeoutInterval:180];
return [[self alloc] initWithRequest:req toLocalFileNamed:inLocalName forDelegate:nil completionBlock:inCompletionBlock failureBlock:inFailureBlock];
}
+(id)downloadUrl:(NSURL *)inUrl toLocalFileNamed:(NSString *)inLocalName forDelegate:(id<FileDownloaderDelegate>)inDelegate;
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:inUrl];
[req setTimeoutInterval:180];
return [[self alloc] initWithRequest:req toLocalFileNamed:inLocalName forDelegate:inDelegate];
}
-(id)initWithRequest:(NSMutableURLRequest *)inReq toLocalFileNamed:(NSString *)inLocalName forDelegate:(id<FileDownloaderDelegate>)inDelegate completionBlock:(void (^)())inCompletionBlock failureBlock:(void (^)(NSError *))inFailureBlock;
{
if((self = [super init]))
{
[self setUrl:[inReq URL]];
[self setDelegate:inDelegate];
[self setLocalName:inLocalName];
[self setCompletionBlock:inCompletionBlock];
[self setFailureBlock:inFailureBlock];
[self setData:[NSMutableData data]];
connection = [[NSURLConnection alloc] initWithRequest:inReq delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[connection start];
statusCode = 0;
}
return self;
}
-(id)initWithRequest:(NSMutableURLRequest *)inReq toLocalFileNamed:(NSString *)inLocalName forDelegate:(id<FileDownloaderDelegate>)inDelegate;
{
return [self initWithRequest:inReq toLocalFileNamed:inLocalName forDelegate:inDelegate completionBlock:nil failureBlock:nil];
}
-(id)initWithDownloadUrl:(NSURL *)inUrl toLocalFileNamed:(NSString *)inLocalName withUsername:(NSString *)inUserName password:(NSString *)inPassword forDelegate:(id<FileDownloaderDelegate>)inDelegate;
{
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:inUrl];
[req addAuthenticationForUsername:inUserName password:inPassword];
[req setTimeoutInterval:180];
return [self initWithRequest:req toLocalFileNamed:inLocalName forDelegate:inDelegate];
}
-(void)cancel;
{
[connection cancel];
}
-(void)dealloc;
{
[self setData:nil];
[self setConnection:nil];
[self setLocalName:nil];
[self setDelegate:nil];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response;
self.statusCode = [resp statusCode];
self.expectedContentLenght = response.expectedContentLength;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(self.statusCode >= 200 && self.statusCode < 300)
{
NSString *dirName = [[self localName] stringByDeletingLastPathComponent];
if(![[NSFileManager defaultManager] fileExistsAtPath:dirName])
[[NSFileManager defaultManager] createDirectoryAtPath:dirName withIntermediateDirectories:YES attributes:nil error:nil];
[data writeToURL:[NSURL fileURLWithPath:localName] atomically:YES];
if(completionBlock)
completionBlock();
[delegate downloader:self downloadedFileToLocation:localName];
}
else
{
NSError *error = nil;
error = [NSError errorWithDomain:@"Error" code:statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Server side error: %d", self.statusCode] forKey:NSLocalizedDescriptionKey]];
if(failureBlock)
failureBlock(error);
if([delegate respondsToSelector:@selector(downloader:failedWithError:)])
[delegate downloader:self failedWithError:error];
}
[self setConnection:nil];
[self setData:nil];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self setConnection:nil];
[self setData:nil];
if(failureBlock)
failureBlock(error);
if([delegate respondsToSelector:@selector(downloader:failedWithError:)])
[delegate downloader:self failedWithError:error];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData
{
[data appendData:inData];
if([delegate respondsToSelector:@selector(downloader:downloadProgressWasUpdatedTo:)] &&
self.expectedContentLenght != NSURLResponseUnknownLength)
{
float downloadedLenght = data.length;
float downloadProgress = downloadedLenght / self.expectedContentLenght;
[self.delegate downloader:self downloadProgressWasUpdatedTo:downloadProgress];
}
}
@end