Skip to content

Commit c308eef

Browse files
authored
Merge pull request #46 from ovr/more-features
Parameters support & UP CC
2 parents f8e865a + 800dd2f commit c308eef

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

android/src/main/java/com/vydia/UploaderModule.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
203203
request.setNotificationConfig(new UploadNotificationConfig());
204204
}
205205

206+
if (options.hasKey("parameters")) {
207+
if (requestType.equals("raw")) {
208+
promise.reject(new IllegalArgumentException("Parameters supported only in multipart type"));
209+
return;
210+
}
211+
212+
ReadableMap parameters = options.getMap("parameters");
213+
ReadableMapKeySetIterator keys = parameters.keySetIterator();
214+
215+
while (keys.hasNextKey()) {
216+
String key = keys.nextKey();
217+
218+
if (parameters.getType(key) != ReadableType.String) {
219+
promise.reject(new IllegalArgumentException("Parameters must be string key/values. Value was invalid for '" + key + "'"));
220+
return;
221+
}
222+
223+
request.addParameter(key, parameters.getString(key));
224+
}
225+
}
226+
206227
if (options.hasKey("headers")) {
207228
ReadableMap headers = options.getMap("headers");
208229
ReadableMapKeySetIterator keys = headers.keySetIterator();

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export type StartUploadArgs = {
1818
// This option is needed for multipart type
1919
field?: string,
2020
customUploadId?: string,
21+
// parameters are supported only in multipart type
22+
parameters?: { [string]: string },
2123
headers?: Object,
2224
notification?: NotificationArgs
2325
}

ios/VydiaRNFileUploader.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,15 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
151151
NSString *fieldName = options[@"field"];
152152
NSString *customUploadId = options[@"customUploadId"];
153153
NSDictionary *headers = options[@"headers"];
154+
NSDictionary *parameters = options[@"parameters"];
154155

155156
@try {
156-
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: uploadUrl]];
157+
NSURL *requestUrl = [NSURL URLWithString: uploadUrl];
158+
if (requestUrl == nil) {
159+
@throw @"Request cannot be nil";
160+
}
161+
162+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestUrl];
157163
[request setHTTPMethod: method];
158164

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

191-
NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI fieldName:fieldName];
197+
NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI parameters: parameters fieldName:fieldName];
192198
[request setHTTPBody: httpBody];
193199

194200
// I am sorry about warning, but Upload tasks from NSData are not supported in background sessions.
195201
uploadTask = [[self urlSession] uploadTaskWithRequest:request fromData: nil];
196202
} else {
203+
if (parameters.count > 0) {
204+
reject(@"RN Uploader", @"Parameters supported only in multipart type", nil);
205+
return;
206+
}
207+
197208
uploadTask = [[self urlSession] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]];
198209
}
199210

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

226237
- (NSData *)createBodyWithBoundary:(NSString *)boundary
227238
path:(NSString *)path
239+
parameters:(NSDictionary *)parameters
228240
fieldName:(NSString *)fieldName {
229241

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

252+
[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
253+
[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
254+
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
255+
[httpBody appendData:[[NSString stringWithFormat:@"%@\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
256+
}];
257+
240258
[httpBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
241259
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fieldName, filename] dataUsingEncoding:NSUTF8StringEncoding]];
242260
[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimetype] dataUsingEncoding:NSUTF8StringEncoding]];
@@ -253,6 +271,7 @@ - (NSURLSession *)urlSession {
253271
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:BACKGROUND_SESSION_ID];
254272
_urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
255273
}
274+
256275
return _urlSession;
257276
}
258277

0 commit comments

Comments
 (0)