@@ -167,9 +167,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
167
167
respFile = NO ;
168
168
}
169
169
170
- NSURLSessionDataTask *task = [session dataTaskWithRequest: req];
171
- [task resume ];
172
- self.task = task;
170
+ if (backgroundTask) {
171
+ NSURLSessionDownloadTask *task = [session downloadTaskWithRequest: req];
172
+ [task resume ];
173
+ self.task = task;
174
+ } else {
175
+ NSURLSessionDataTask *task = [session dataTaskWithRequest: req];
176
+ [task resume ];
177
+ self.task = task;
178
+ }
173
179
174
180
// network status indicator
175
181
if ([[options objectForKey: CONFIG_INDICATOR] boolValue ]) {
@@ -188,6 +194,52 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
188
194
189
195
#pragma mark NSURLSession delegate methods
190
196
197
+ - (void )configureWriteStream {
198
+ if (respFile)
199
+ {
200
+ @try {
201
+ NSFileManager * fm = [NSFileManager defaultManager ];
202
+ NSString * folder = [destPath stringByDeletingLastPathComponent ];
203
+
204
+ if (![fm fileExistsAtPath: folder]) {
205
+ [fm createDirectoryAtPath: folder withIntermediateDirectories: YES attributes: NULL error: nil ];
206
+ }
207
+
208
+ // if not set overwrite in options, defaults to TRUE
209
+ BOOL overwrite = [options valueForKey: @" overwrite" ] == nil ? YES : [[options valueForKey: @" overwrite" ] boolValue ];
210
+ BOOL appendToExistingFile = [destPath containsString: @" ?append=true" ];
211
+
212
+ appendToExistingFile = !overwrite;
213
+
214
+ // For solving #141 append response data if the file already exists
215
+ // base on PR#139 @kejinliang
216
+ if (appendToExistingFile) {
217
+ destPath = [destPath stringByReplacingOccurrencesOfString: @" ?append=true" withString: @" " ];
218
+ }
219
+
220
+ if (![fm fileExistsAtPath: destPath]) {
221
+ [fm createFileAtPath: destPath contents: [[NSData alloc ] init ] attributes: nil ];
222
+ }
223
+
224
+ writeStream = [[NSOutputStream alloc ] initToFileAtPath: destPath append: appendToExistingFile];
225
+ [writeStream scheduleInRunLoop: [NSRunLoop currentRunLoop ] forMode: NSRunLoopCommonModes ];
226
+ [writeStream open ];
227
+ }
228
+ @catch (NSException * ex)
229
+ {
230
+ NSLog (@" write file error" );
231
+ }
232
+ }
233
+ }
234
+
235
+ - (void )processData : (NSData *)data {
236
+ if (respFile && ![self ShouldTransformFile ]) {
237
+ [writeStream write: (const uint8_t *)[data bytes ] maxLength: [data length ]];
238
+ } else {
239
+ [respData appendData: data];
240
+ }
241
+ }
242
+
191
243
192
244
#pragma mark - Received Response
193
245
// set expected content length on response received
@@ -278,41 +330,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
278
330
NSLog (@" oops" );
279
331
}
280
332
281
- if (respFile)
282
- {
283
- @try {
284
- NSFileManager * fm = [NSFileManager defaultManager ];
285
- NSString * folder = [destPath stringByDeletingLastPathComponent ];
286
-
287
- if (![fm fileExistsAtPath: folder]) {
288
- [fm createDirectoryAtPath: folder withIntermediateDirectories: YES attributes: NULL error: nil ];
289
- }
290
-
291
- // if not set overwrite in options, defaults to TRUE
292
- BOOL overwrite = [options valueForKey: @" overwrite" ] == nil ? YES : [[options valueForKey: @" overwrite" ] boolValue ];
293
- BOOL appendToExistingFile = [destPath containsString: @" ?append=true" ];
294
-
295
- appendToExistingFile = !overwrite;
296
-
297
- // For solving #141 append response data if the file already exists
298
- // base on PR#139 @kejinliang
299
- if (appendToExistingFile) {
300
- destPath = [destPath stringByReplacingOccurrencesOfString: @" ?append=true" withString: @" " ];
301
- }
302
-
303
- if (![fm fileExistsAtPath: destPath]) {
304
- [fm createFileAtPath: destPath contents: [[NSData alloc ] init ] attributes: nil ];
305
- }
306
-
307
- writeStream = [[NSOutputStream alloc ] initToFileAtPath: destPath append: appendToExistingFile];
308
- [writeStream scheduleInRunLoop: [NSRunLoop currentRunLoop ] forMode: NSRunLoopCommonModes ];
309
- [writeStream open ];
310
- }
311
- @catch (NSException * ex)
312
- {
313
- NSLog (@" write file error" );
314
- }
315
- }
333
+ [self configureWriteStream ];
316
334
317
335
completionHandler (NSURLSessionResponseAllow );
318
336
}
@@ -339,11 +357,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
339
357
340
358
// If we need to process the data, we defer writing into the file until the we have all the data, at which point
341
359
// we can perform the processing and then write into the file
342
- if (respFile && ![self ShouldTransformFile ]) {
343
- [writeStream write: (const uint8_t *)[data bytes ] maxLength: [data length ]];
344
- } else {
345
- [respData appendData: data];
346
- }
360
+ [self processData: data];
347
361
348
362
if (expectedBytes == 0 ) {
349
363
return ;
@@ -535,5 +549,36 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPe
535
549
}
536
550
}
537
551
552
+ // NSURLSessionDownloadTask delegates
553
+
554
+ #pragma mark NSURLSessionDownloadTask delegate methods
555
+
556
+ - (void )URLSession : (NSURLSession *)session downloadTask : (NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL : (NSURL *)location {
557
+
558
+ NSFileManager *fm = [NSFileManager defaultManager ];
559
+ NSData *data = [fm contentsAtPath: location.path];
560
+
561
+ [self configureWriteStream ];
562
+
563
+ [self processData: data];
564
+
565
+ }
566
+
567
+ - (void )URLSession : (NSURLSession *)session downloadTask : (NSURLSessionDownloadTask *)downloadTask didWriteData : (int64_t )bytesWritten totalBytesWritten : (int64_t )totalBytesWritten totalBytesExpectedToWrite : (int64_t )totalBytesExpectedToWrite {
568
+ if (totalBytesExpectedToWrite == 0 ) {
569
+ return ;
570
+ }
571
+
572
+ NSNumber * now =[NSNumber numberWithFloat: ((float )totalBytesWritten/(float )totalBytesExpectedToWrite)];
573
+ if ([self .progressConfig shouldReport: now]) {
574
+ [self .baseModule emitEventDict: EVENT_PROGRESS
575
+ body: @{
576
+ @" taskId" : taskId,
577
+ @" written" : [NSString stringWithFormat: @" %lld " , (long long ) totalBytesWritten],
578
+ @" total" : [NSString stringWithFormat: @" %lld " , (long long ) totalBytesExpectedToWrite]
579
+ }
580
+ ];
581
+ }
582
+ }
538
583
539
584
@end
0 commit comments