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 */ = { diff --git a/ios/RNFetchBlobNetwork.m b/ios/RNFetchBlobNetwork.m index 31348478c..f0c08eadb 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 @@ -154,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 @@ -171,6 +184,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]; @@ -244,8 +258,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options } __block NSURLSessionDataTask * task; - if (path && req.HTTPMethod == @"POST") { - task = [session uploadTaskWithRequest:req fromFile:path]; + if (path && [req.HTTPMethod isEqualToString:@"POST"]) { + task = [session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:path]]; + } else if (uploadTask && [req.HTTPBody length] > 0) { + NSError *error; + task = [self uploadTaskWithBodyOfRequest:req session:session inBackground:backgroundTask error:&error]; + if (!task) { + callback(@[error.localizedDescription]); + return; + } } else { task = [session dataTaskWithRequest:req]; } @@ -260,6 +281,26 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options } +- (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]]; + 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]) { + uploadTempFile = nil; + 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 { @@ -545,6 +586,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom callback(@[ errMsg, rnfbRespType, respStr]); + [self removeUploadTempFile]; @synchronized(taskTable, uploadProgressTable, progressTable) {