@@ -194,6 +194,52 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
194194
195195#pragma mark NSURLSession delegate methods
196196
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+
197243
198244#pragma mark - Received Response
199245// set expected content length on response received
@@ -284,41 +330,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
284330 NSLog (@" oops" );
285331 }
286332
287- if (respFile)
288- {
289- @try {
290- NSFileManager * fm = [NSFileManager defaultManager ];
291- NSString * folder = [destPath stringByDeletingLastPathComponent ];
292-
293- if (![fm fileExistsAtPath: folder]) {
294- [fm createDirectoryAtPath: folder withIntermediateDirectories: YES attributes: NULL error: nil ];
295- }
296-
297- // if not set overwrite in options, defaults to TRUE
298- BOOL overwrite = [options valueForKey: @" overwrite" ] == nil ? YES : [[options valueForKey: @" overwrite" ] boolValue ];
299- BOOL appendToExistingFile = [destPath containsString: @" ?append=true" ];
300-
301- appendToExistingFile = !overwrite;
302-
303- // For solving #141 append response data if the file already exists
304- // base on PR#139 @kejinliang
305- if (appendToExistingFile) {
306- destPath = [destPath stringByReplacingOccurrencesOfString: @" ?append=true" withString: @" " ];
307- }
308-
309- if (![fm fileExistsAtPath: destPath]) {
310- [fm createFileAtPath: destPath contents: [[NSData alloc ] init ] attributes: nil ];
311- }
312-
313- writeStream = [[NSOutputStream alloc ] initToFileAtPath: destPath append: appendToExistingFile];
314- [writeStream scheduleInRunLoop: [NSRunLoop currentRunLoop ] forMode: NSRunLoopCommonModes ];
315- [writeStream open ];
316- }
317- @catch (NSException * ex)
318- {
319- NSLog (@" write file error" );
320- }
321- }
333+ [self configureWriteStream ];
322334
323335 completionHandler (NSURLSessionResponseAllow );
324336}
@@ -345,11 +357,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
345357
346358 // If we need to process the data, we defer writing into the file until the we have all the data, at which point
347359 // we can perform the processing and then write into the file
348- if (respFile && ![self ShouldTransformFile ]) {
349- [writeStream write: (const uint8_t *)[data bytes ] maxLength: [data length ]];
350- } else {
351- [respData appendData: data];
352- }
360+ [self processData: data];
353361
354362 if (expectedBytes == 0 ) {
355363 return ;
@@ -545,6 +553,17 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPe
545553
546554#pragma mark NSURLSessionDownloadTask delegate methods
547555
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+
548567- (void )URLSession : (NSURLSession *)session downloadTask : (NSURLSessionDownloadTask *)downloadTask didWriteData : (int64_t )bytesWritten totalBytesWritten : (int64_t )totalBytesWritten totalBytesExpectedToWrite : (int64_t )totalBytesExpectedToWrite {
549568 if (totalBytesExpectedToWrite == 0 ) {
550569 return ;
@@ -563,5 +582,4 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
563582 }
564583}
565584
566-
567585@end
0 commit comments