Skip to content

Commit

Permalink
feat: Add iOS group app id support & fix .d.ts (#198)
Browse files Browse the repository at this point in the history
* feat: add iOS group app id support

* Fix d.ts for multipart options and callbacks for listeners
  • Loading branch information
Palid authored Jun 9, 2020
1 parent 9f6deb4 commit 6729599
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Returns a promise with the string ID of the upload. Will reject if there is a c
|`parameters`|object|Optional||Additional form fields to include in the HTTP request. Only used when `type: 'multipart`||
|`notification`|Notification object (see below)|Optional||Android only. |`{ enabled: true, onProgressTitle: "Uploading...", autoClear: true }`|
|`useUtf8Charset`|boolean|Optional||Android only. Set to true to use `utf-8` as charset. ||
|`appGroup`|string|Optional|iOS only. App group ID needed for share extensions to be able to properly call the library. See: https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati

### Notification Object (Android Only)
|Name|Type|Required|Description|Example|
Expand Down
19 changes: 13 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ declare module "react-native-background-upload" {
};
// Android notification settings
notification?: Partial<NotificationOptions>
/**
* AppGroup defined in XCode for extensions. Necessary when trying to upload things via this library
* in the context of ShareExtension.
*/
appGroup?: string;
// Necessary only for multipart type upload
field?: string
}

export interface MultipartUploadOptions extends UploadOptions {
Expand All @@ -102,13 +109,13 @@ declare module "react-native-background-upload" {

export type UploadListenerEvent = 'progress' | 'error' | 'completed' | 'cancelled'


export default class Upload {
static startUpload(options: UploadOptions): Promise<uploadId>
static addListener(event: UploadListenerEvent, uploadId: uploadId, data: object): void
static addListener(event: 'progress', uploadId: uploadId, data: ProgressData): void
static addListener(event: 'error', uploadId: uploadId, data: ErrorData): void
static addListener(event: 'completed', uploadId: uploadId, data: CompletedData): void
static addListener(event: 'cancelled', uploadId: uploadId, data: EventData): void
static startUpload(options: UploadOptions | MultipartUploadOptions): Promise<uploadId>
static addListener(event: 'progress', uploadId: uploadId, callback: (data: ProgressData ) => void): void
static addListener(event: 'error', uploadId: uploadId, callback: (data: ErrorData) => void): void
static addListener(event: 'completed', uploadId: uploadId, callback: (data: CompletedData) => void): void
static addListener(event: 'cancelled', uploadId: uploadId, callback: (data: EventData) => void): void
static getFileInfo(path: string): Promise<FileInfo>
static cancelUpload(uploadId: uploadId): Promise<boolean>
}
Expand Down
12 changes: 8 additions & 4 deletions ios/VydiaRNFileUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
NSString *uploadType = options[@"type"] ?: @"raw";
NSString *fieldName = options[@"field"];
NSString *customUploadId = options[@"customUploadId"];
NSString *appGroup = options[@"appGroup"];
NSDictionary *headers = options[@"headers"];
NSDictionary *parameters = options[@"parameters"];

Expand All @@ -158,7 +159,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
if (requestUrl == nil) {
return reject(@"RN Uploader", @"URL not compliant with RFC 2396", nil);
}

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

Expand Down Expand Up @@ -197,14 +198,14 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
NSData *httpBody = [self createBodyWithBoundary:uuidStr path:fileURI parameters: parameters fieldName:fieldName];
[request setHTTPBody: httpBody];

uploadTask = [[self urlSession] uploadTaskWithStreamedRequest:request];
uploadTask = [[self urlSession: appGroup] uploadTaskWithStreamedRequest:request];
} 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]];
uploadTask = [[self urlSession: appGroup] uploadTaskWithRequest:request fromFile:[NSURL URLWithString: fileURI]];
}

uploadTask.taskDescription = customUploadId ? customUploadId : [NSString stringWithFormat:@"%i", thisUploadId];
Expand Down Expand Up @@ -266,9 +267,12 @@ - (NSData *)createBodyWithBoundary:(NSString *)boundary
return httpBody;
}

- (NSURLSession *)urlSession {
- (NSURLSession *)urlSession: (NSString *) groupId {
if (_urlSession == nil) {
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:BACKGROUND_SESSION_ID];
if (groupId != nil && ![groupId isEqualToString:@""]) {
sessionConfiguration.sharedContainerIdentifier = groupId;
}
_urlSession = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
}

Expand Down

0 comments on commit 6729599

Please sign in to comment.