From 6885e4efaefcd736d78fc25e7af385f1067f783a Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:17:52 -0400 Subject: [PATCH 1/8] Set default indent width to 4 spaces in Xcode project This enables consistent indentation regardless of the default indentation settings on a developer's machine. --- ios/RNFetchBlob.xcodeproj/project.pbxproj | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/RNFetchBlob.xcodeproj/project.pbxproj b/ios/RNFetchBlob.xcodeproj/project.pbxproj index a19a6a1af..76800f55e 100644 --- a/ios/RNFetchBlob.xcodeproj/project.pbxproj +++ b/ios/RNFetchBlob.xcodeproj/project.pbxproj @@ -92,6 +92,7 @@ A15C300F1CD25C330074CB35 /* Products */, 8BD9ABDFAF76406291A798F2 /* Libraries */, ); + indentWidth = 4; sourceTree = ""; }; A15C300F1CD25C330074CB35 /* Products */ = { From b1f215fda8f9a53fab3a29af12c8647eb27923c4 Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:19:57 -0400 Subject: [PATCH 2/8] Use string comparison instead of pointer comparison Comparing two `NSString` objects using pointer equality is undefined. --- ios/RNFetchBlobNetwork.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 31348478c..3eef60562 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -244,7 +244,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options } __block NSURLSessionDataTask * task; - if (path && req.HTTPMethod == @"POST") { + if (path && [req.HTTPMethod isEqualToString:@"POST"]) { task = [session uploadTaskWithRequest:req fromFile:path]; } else { task = [session dataTaskWithRequest:req]; From a5c89407a0b90d81cc4caba61a15e71ba5fede9c Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:27:21 -0400 Subject: [PATCH 3/8] Add config option to upload body as iOS upload task Rather than specifying an existing file to upload (using the `path` config option), this change adds the option to write the request body to a temporary file, and upload it using `NSURLSessionUploadTask`. --- ios/RNFetchBlobNetwork.m | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 3eef60562..742ed50f0 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -83,6 +83,8 @@ @interface RNFetchBlobNetwork () ResponseFormat responseFormat; BOOL * followRedirect; BOOL backgroundTask; + BOOL uploadTask; + NSURL * uploadTempFile; } @end @@ -171,6 +173,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options self.options = options; backgroundTask = [options valueForKey:@"IOSBackgroundTask"] == nil ? NO : [[options valueForKey:@"IOSBackgroundTask"] boolValue]; + uploadTask = [options valueForKey:@"IOSUploadTask"] == nil ? NO : [[options valueForKey:@"IOSUploadTask"] boolValue]; followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue]; isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue]; redirects = [[NSMutableArray alloc] init]; @@ -246,6 +249,21 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options __block NSURLSessionDataTask * task; if (path && [req.HTTPMethod isEqualToString:@"POST"]) { task = [session uploadTaskWithRequest:req fromFile:path]; + } else if (uploadTask && [req.HTTPBody length] > 0) { + NSString *tempPath = [RNFetchBlobFS getTempPath]; + NSURL *tempRootDir = [NSURL fileURLWithPath:tempPath isDirectory:YES]; + NSURL *tempDir = [tempRootDir URLByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; + NSError *error; + if (![[NSFileManager defaultManager] createDirectoryAtURL:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) { + callback(@[error.localizedDescription]); + return; + } + uploadTempFile = [tempDir URLByAppendingPathComponent:taskId]; + if (![req.HTTPBody writeToURL:uploadTempFile options:NSDataWritingAtomic error:&error]) { + callback(@[error.localizedDescription]); + return; + } + task = [session uploadTaskWithRequest:req fromFile:uploadTempFile]; } else { task = [session dataTaskWithRequest:req]; } From b6dd56f70ace02a25ef5ba4da2e93d3318f6f4f6 Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:31:46 -0400 Subject: [PATCH 4/8] Remove upload temprorary file after request completes --- ios/RNFetchBlobNetwork.m | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 742ed50f0..cd30bbf05 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -156,6 +156,17 @@ - (NSString *)md5:(NSString *)input { return ret; } +- (void) removeUploadTempFile { + if (!uploadTempFile) { + return; + } + + NSError *error; + if (![[NSFileManager defaultManager] removeItemAtURL:uploadTempFile error:&error]) { + NSLog(@"Failed to remove upload temporary file: %@", error.localizedDescription); + } +} + // send HTTP request - (void) sendRequest:(__weak NSDictionary * _Nullable )options contentLength:(long) contentLength @@ -563,6 +574,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom callback(@[ errMsg, rnfbRespType, respStr]); + [self removeUploadTempFile]; @synchronized(taskTable, uploadProgressTable, progressTable) { From 0e306edd72815442319ab97df7218df082abf9b4 Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:35:39 -0400 Subject: [PATCH 5/8] Fix incorrect passing of NSString instead of NSURL --- ios/RNFetchBlobNetwork.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index cd30bbf05..6f72d9104 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -259,7 +259,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options __block NSURLSessionDataTask * task; if (path && [req.HTTPMethod isEqualToString:@"POST"]) { - task = [session uploadTaskWithRequest:req fromFile:path]; + task = [session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:path]]; } else if (uploadTask && [req.HTTPBody length] > 0) { NSString *tempPath = [RNFetchBlobFS getTempPath]; NSURL *tempRootDir = [NSURL fileURLWithPath:tempPath isDirectory:YES]; From c63cf09acec3c41584cedfd47fa75dc0b78654b1 Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:43:12 -0400 Subject: [PATCH 6/8] Extract method for uploading request body on iOS --- ios/RNFetchBlobNetwork.m | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 6f72d9104..fc3bb2202 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -261,20 +261,12 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options if (path && [req.HTTPMethod isEqualToString:@"POST"]) { task = [session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:path]]; } else if (uploadTask && [req.HTTPBody length] > 0) { - NSString *tempPath = [RNFetchBlobFS getTempPath]; - NSURL *tempRootDir = [NSURL fileURLWithPath:tempPath isDirectory:YES]; - NSURL *tempDir = [tempRootDir URLByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; NSError *error; - if (![[NSFileManager defaultManager] createDirectoryAtURL:tempDir withIntermediateDirectories:YES attributes:nil error:&error]) { + task = [self uploadTaskWithBodyOfRequest:req session:session error:&error]; + if (!task) { callback(@[error.localizedDescription]); return; } - uploadTempFile = [tempDir URLByAppendingPathComponent:taskId]; - if (![req.HTTPBody writeToURL:uploadTempFile options:NSDataWritingAtomic error:&error]) { - callback(@[error.localizedDescription]); - return; - } - task = [session uploadTaskWithRequest:req fromFile:uploadTempFile]; } else { task = [session dataTaskWithRequest:req]; } @@ -289,6 +281,21 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options } +- (NSURLSessionUploadTask *) uploadTaskWithBodyOfRequest:(NSURLRequest *)req session:(NSURLSession *)session error:(NSError **)error +{ + NSString *tempPath = [RNFetchBlobFS getTempPath]; + NSURL *tempRootDir = [NSURL fileURLWithPath:tempPath isDirectory:YES]; + NSURL *tempDir = [tempRootDir URLByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; + if (![[NSFileManager defaultManager] createDirectoryAtURL:tempDir withIntermediateDirectories:YES attributes:nil error:error]) { + return nil; + } + uploadTempFile = [tempDir URLByAppendingPathComponent:taskId]; + if (![req.HTTPBody writeToURL:uploadTempFile options:NSDataWritingAtomic error:error]) { + return nil; + } + return [session uploadTaskWithRequest:req fromFile:uploadTempFile]; +} + // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled + (void) emitExpiredTasks { From 3308216c1615233239d0ff3663ff31812bd44d2e Mon Sep 17 00:00:00 2001 From: Patrick Montalto Date: Tue, 24 Oct 2017 14:49:55 -0400 Subject: [PATCH 7/8] Only create temporary file for iOS background uploads --- ios/RNFetchBlobNetwork.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index fc3bb2202..076259f46 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -262,7 +262,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options task = [session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:path]]; } else if (uploadTask && [req.HTTPBody length] > 0) { NSError *error; - task = [self uploadTaskWithBodyOfRequest:req session:session error:&error]; + task = [self uploadTaskWithBodyOfRequest:req session:session inBackground:backgroundTask error:&error]; if (!task) { callback(@[error.localizedDescription]); return; @@ -281,8 +281,12 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options } -- (NSURLSessionUploadTask *) uploadTaskWithBodyOfRequest:(NSURLRequest *)req session:(NSURLSession *)session error:(NSError **)error +- (NSURLSessionUploadTask *) uploadTaskWithBodyOfRequest:(NSURLRequest *)req session:(NSURLSession *)session inBackground:(BOOL)background error:(NSError **)error { + if (!background) { + return [session uploadTaskWithRequest:req fromData:req.HTTPBody]; + } + NSString *tempPath = [RNFetchBlobFS getTempPath]; NSURL *tempRootDir = [NSURL fileURLWithPath:tempPath isDirectory:YES]; NSURL *tempDir = [tempRootDir URLByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]]; From d89d844847a40956951e9553195c962c3bb3c384 Mon Sep 17 00:00:00 2001 From: Adam Sharp Date: Wed, 25 Oct 2017 10:33:53 -0400 Subject: [PATCH 8/8] Clear upload temp file URL on failure --- ios/RNFetchBlobNetwork.m | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 076259f46..f0c08eadb 100644 --- a/ios/RNFetchBlobNetwork.m +++ b/ios/RNFetchBlobNetwork.m @@ -295,6 +295,7 @@ - (NSURLSessionUploadTask *) uploadTaskWithBodyOfRequest:(NSURLRequest *)req ses } uploadTempFile = [tempDir URLByAppendingPathComponent:taskId]; if (![req.HTTPBody writeToURL:uploadTempFile options:NSDataWritingAtomic error:error]) { + uploadTempFile = nil; return nil; } return [session uploadTaskWithRequest:req fromFile:uploadTempFile];