Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Ability to upload request body as iOS Upload Task #576

Open
wants to merge 8 commits into
base: 0.11.0
Choose a base branch
from
1 change: 1 addition & 0 deletions ios/RNFetchBlob.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
A15C300F1CD25C330074CB35 /* Products */,
8BD9ABDFAF76406291A798F2 /* Libraries */,
);
indentWidth = 4;
sourceTree = "<group>";
};
A15C300F1CD25C330074CB35 /* Products */ = {
Expand Down
46 changes: 44 additions & 2 deletions ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ @interface RNFetchBlobNetwork ()
ResponseFormat responseFormat;
BOOL * followRedirect;
BOOL backgroundTask;
BOOL uploadTask;
NSURL * uploadTempFile;
}

@end
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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];
}
Expand All @@ -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
{
Expand Down Expand Up @@ -545,6 +586,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom


callback(@[ errMsg, rnfbRespType, respStr]);
[self removeUploadTempFile];

@synchronized(taskTable, uploadProgressTable, progressTable)
{
Expand Down