Skip to content

Commit

Permalink
Merge pull request #46 from ovr/more-features
Browse files Browse the repository at this point in the history
Parameters support & UP CC
  • Loading branch information
StevePotter committed Mar 12, 2018
2 parents f8e865a + 800dd2f commit c308eef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
21 changes: 21 additions & 0 deletions android/src/main/java/com/vydia/UploaderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
request.setNotificationConfig(new UploadNotificationConfig());
}

if (options.hasKey("parameters")) {
if (requestType.equals("raw")) {
promise.reject(new IllegalArgumentException("Parameters supported only in multipart type"));
return;
}

ReadableMap parameters = options.getMap("parameters");
ReadableMapKeySetIterator keys = parameters.keySetIterator();

while (keys.hasNextKey()) {
String key = keys.nextKey();

if (parameters.getType(key) != ReadableType.String) {
promise.reject(new IllegalArgumentException("Parameters must be string key/values. Value was invalid for '" + key + "'"));
return;
}

request.addParameter(key, parameters.getString(key));
}
}

if (options.hasKey("headers")) {
ReadableMap headers = options.getMap("headers");
ReadableMapKeySetIterator keys = headers.keySetIterator();
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type StartUploadArgs = {
// This option is needed for multipart type
field?: string,
customUploadId?: string,
// parameters are supported only in multipart type
parameters?: { [string]: string },
headers?: Object,
notification?: NotificationArgs
}
Expand Down
23 changes: 21 additions & 2 deletions ios/VydiaRNFileUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,15 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
NSString *fieldName = options[@"field"];
NSString *customUploadId = options[@"customUploadId"];
NSDictionary *headers = options[@"headers"];
NSDictionary *parameters = options[@"parameters"];

@try {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: uploadUrl]];
NSURL *requestUrl = [NSURL URLWithString: uploadUrl];
if (requestUrl == nil) {
@throw @"Request cannot be nil";
}

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl];
[request setHTTPMethod: method];

[headers enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull val, BOOL * _Nonnull stop) {
Expand Down Expand Up @@ -188,12 +194,17 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
NSString *uuidStr = [[NSUUID UUID] UUIDString];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", uuidStr] forHTTPHeaderField:@"Content-Type"];

NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI fieldName:fieldName];
NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI parameters: parameters fieldName:fieldName];
[request setHTTPBody: httpBody];

// I am sorry about warning, but Upload tasks from NSData are not supported in background sessions.
uploadTask = [[self urlSession] uploadTaskWithRequest:request fromData: nil];
} else {
if (parameters.count > 0) {
reject(@"RN Uploader", @"Parameters supported only in multipart type", nil);
return;
}

uploadTask = [[self urlSession] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]];
}

Expand Down Expand Up @@ -225,6 +236,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri

- (NSData *)createBodyWithBoundary:(NSString *)boundary
path:(NSString *)path
parameters:(NSDictionary *)parameters
fieldName:(NSString *)fieldName {

NSMutableData *httpBody = [NSMutableData data];
Expand All @@ -237,6 +249,12 @@ - (NSData *)createBodyWithBoundary:(NSString *)boundary
NSString *filename = [path lastPathComponent];
NSString *mimetype = [self guessMIMETypeFromFileName:path];

[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];

[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, filename] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimetype] dataUsingEncoding:NSUTF8StringEncoding]];
Expand All @@ -253,6 +271,7 @@ - (NSURLSession *)urlSession {
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:BACKGROUND_SESSION_ID];
_urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
}

return _urlSession;
}

Expand Down

0 comments on commit c308eef

Please sign in to comment.