-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDBRequest.m
More file actions
228 lines (189 loc) · 7.77 KB
/
DBRequest.m
File metadata and controls
228 lines (189 loc) · 7.77 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// DBRestRequest.m
// DropboxSDK
//
// Created by Brian Smith on 4/9/10.
// Copyright 2010 Dropbox, Inc. All rights reserved.
//
#import "DBRequest.h"
#import "DBError.h"
#import "JSON.h"
static id networkRequestDelegate = nil;
@implementation DBRequest
+ (void)setNetworkRequestDelegate:(id<DBNetworkRequestDelegate>)delegate {
networkRequestDelegate = delegate;
}
- (id)initWithURLRequest:(NSURLRequest*)aRequest andInformTarget:(id)aTarget selector:(SEL)aSelector {
if ((self = [super init])) {
request = [aRequest retain];
target = aTarget;
selector = aSelector;
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[networkRequestDelegate networkRequestStarted];
}
return self;
}
- (void) dealloc {
[urlConnection cancel];
[request release];
[urlConnection release];
[fileHandle release];
[userInfo release];
[response release];
[resultFilename release];
[tempFilename release];
[resultData release];
[error release];
[super dealloc];
}
@synthesize failureSelector;
@synthesize downloadProgressSelector;
@synthesize uploadProgressSelector;
@synthesize userInfo;
@synthesize request;
@synthesize response;
@synthesize downloadProgress;
@synthesize uploadProgress;
@synthesize resultData;
@synthesize resultFilename;
@synthesize error;
- (NSString*)resultString {
return [[[NSString alloc]
initWithData:resultData encoding:NSUTF8StringEncoding]
autorelease];
}
- (NSObject*)resultJSON {
return [[self resultString] JSONValue];
}
- (NSInteger)statusCode {
return [response statusCode];
}
- (void)cancel {
[urlConnection cancel];
target = nil;
if (tempFilename) {
[fileHandle closeFile];
NSError* rmError;
if (![[NSFileManager defaultManager] removeItemAtPath:tempFilename error:&rmError]) {
NSLog(@"DBRequest#cancel Error removing temp file: %@", rmError);
}
}
[networkRequestDelegate networkRequestStopped];
}
#pragma mark NSURLConnection delegate methods
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)aResponse {
response = [(NSHTTPURLResponse*)aResponse retain];
if (resultFilename && [self statusCode] == 200) {
// Create the file here so it's created in case it's zero length
// File is downloaded into a temporary file and then moved over when completed successfully
NSString* filename =
[NSString stringWithFormat:@"%.0f", 1000*[NSDate timeIntervalSinceReferenceDate]];
tempFilename = [[NSTemporaryDirectory() stringByAppendingPathComponent:filename] retain];
NSFileManager* fileManager = [[NSFileManager new] autorelease];
BOOL success = [fileManager createFileAtPath:tempFilename contents:nil attributes:nil];
if (!success) {
NSLog(@"DBRequest#connection:didReceiveData: Error creating file at path: %@",
tempFilename);
}
fileHandle = [[NSFileHandle fileHandleForWritingAtPath:tempFilename] retain];
}
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
if (resultFilename && [self statusCode] == 200) {
@try {
[fileHandle writeData:data];
} @catch (NSException* e) {
// In case we run out of disk space
[urlConnection cancel];
[fileHandle closeFile];
[[NSFileManager defaultManager] removeItemAtPath:tempFilename error:nil];
error = [[NSError alloc] initWithDomain:DBErrorDomain
code:DBErrorInsufficientDiskSpace userInfo:userInfo];
SEL sel = failureSelector ? failureSelector : selector;
[target performSelector:sel withObject:self];
[networkRequestDelegate networkRequestStopped];
return;
}
} else {
if (resultData == nil) {
resultData = [NSMutableData new];
}
[resultData appendData:data];
}
bytesDownloaded += [data length];
NSInteger contentLength = [[[response allHeaderFields] objectForKey:@"Content-Length"] intValue];
downloadProgress = (CGFloat)bytesDownloaded / (CGFloat)contentLength;
if (downloadProgressSelector) {
[target performSelector:downloadProgressSelector withObject:self];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[fileHandle closeFile];
[fileHandle release];
fileHandle = nil;
if (self.statusCode != 200) {
NSMutableDictionary* errorUserInfo = [NSMutableDictionary dictionaryWithDictionary:userInfo];
// To get error userInfo, first try and make sense of the response as JSON, if that
// fails then send back the string as an error message
NSString* resultString = [self resultString];
if ([resultString length] > 0) {
@try {
SBJsonParser *jsonParser = [SBJsonParser new];
NSObject* resultJSON = [jsonParser objectWithString:resultString];
[jsonParser release];
if ([resultJSON isKindOfClass:[NSDictionary class]]) {
[errorUserInfo addEntriesFromDictionary:(NSDictionary*)resultJSON];
}
} @catch (NSException* e) {
[errorUserInfo setObject:resultString forKey:@"errorMessage"];
}
}
error = [[NSError alloc] initWithDomain:@"dropbox.com" code:self.statusCode userInfo:errorUserInfo];
} else if (tempFilename) {
// Move temp file over to desired file
NSFileManager* fileManager = [[NSFileManager new] autorelease];
[fileManager removeItemAtPath:resultFilename error:nil];
NSError* moveError;
BOOL success = [fileManager moveItemAtPath:tempFilename toPath:resultFilename error:&moveError];
if (!success) {
NSLog(@"DBRequest#connectionDidFinishLoading: error moving temp file to desired location: %@",
[moveError localizedDescription]);
error = [[NSError alloc] initWithDomain:moveError.domain code:moveError.code userInfo:self.userInfo];
}
[tempFilename release];
tempFilename = nil;
}
SEL sel = (error && failureSelector) ? failureSelector : selector;
[target performSelector:sel withObject:self];
[networkRequestDelegate networkRequestStopped];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)anError {
[fileHandle closeFile];
error = [[NSError alloc] initWithDomain:anError.domain code:anError.code userInfo:self.userInfo];
bytesDownloaded = 0;
downloadProgress = 0;
uploadProgress = 0;
if (tempFilename) {
NSFileManager* fileManager = [[NSFileManager new] autorelease];
NSError* removeError;
BOOL success = [fileManager removeItemAtPath:tempFilename error:&removeError];
if (!success) {
NSLog(@"DBRequest#connection:didFailWithError: error removing temporary file: %@",
[removeError localizedDescription]);
}
[tempFilename release];
tempFilename = nil;
}
SEL sel = failureSelector ? failureSelector : selector;
[target performSelector:sel withObject:self];
[networkRequestDelegate networkRequestStopped];
}
- (void)connection:(NSURLConnection*)connection didSendBodyData:(NSInteger)bytesWritten
totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
uploadProgress = (CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite;
if (uploadProgressSelector) {
[target performSelector:uploadProgressSelector withObject:self];
}
}
@end