Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel upload #51

Merged
merged 7 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ Upload.startUpload(options).then((uploadId) => {
Upload.addListener('error', uploadId, (data) => {
console.log(`Error: ${data.error}%`)
})
Upload.addListener('cancelled', uploadId, (data) => {
console.log(`Cancelled!`)
})
Upload.addListener('completed', uploadId, (data) => {
console.log('Completed!')
})
Expand Down
23 changes: 22 additions & 1 deletion android/src/main/java/com/vydia/UploaderModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
.addFileToUpload(filePath, options.getString("field"));
}


request.setMethod(method)
.setMaxRetries(2)
.setDelegate(statusDelegate);
Expand All @@ -216,7 +217,27 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
}

String uploadId = request.startUpload();
promise.resolve(customUploadId != null ? customUploadId : uploadId);
promise.resolve(uploadId);
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
promise.reject(exc);
}
}

/*
* Cancels file upload
* Accepts upload ID as a first argument, this upload will be cancelled
* Event "cancelled" will be fired when upload is cancelled.
*/
@ReactMethod
public void cancelUpload(String cancelUploadId, final Promise promise) {
if (!(cancelUploadId instanceof String)) {
promise.reject(new IllegalArgumentException("Upload ID must be a string"));
return;
}
try {
UploadService.stopUpload(cancelUploadId);
promise.resolve(true);
} catch (Exception exc) {
Log.e(TAG, exc.getMessage(), exc);
promise.reject(exc);
Expand Down
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const eventPrefix = 'RNFileUploader-'
if (NativeModules.VydiaRNFileUploader) {
NativeModule.addListener(eventPrefix + 'progress')
NativeModule.addListener(eventPrefix + 'error')
NativeModule.addListener(eventPrefix + 'cancelled')
NativeModule.addListener(eventPrefix + 'completed')
}

Expand Down Expand Up @@ -73,12 +74,32 @@ It is recommended to add listeners in the .then of this promise.
*/
export const startUpload = (options: StartUploadArgs): Promise<string> => NativeModule.startUpload(options)

/*
Cancels active upload by string ID of the upload.

Upload ID is returned in a promise after a call to startUpload method,
use it to cancel started upload.

Event "cancelled" will be fired when upload is cancelled.

Returns a promise with boolean true if operation was successfully completed.
Will reject if there was an internal error or ID format is invalid.

*/
export const cancelUpload = (cancelUploadId: string): Promise<boolean> => {
if (typeof cancelUploadId !== 'string') {
return Promise.reject(new Error('Upload ID must be a string'));
}
return NativeModule.cancelUpload(cancelUploadId);
}

/*
Listens for the given event on the given upload ID (resolved from startUpload).
If you don't supply a value for uploadId, the event will fire for all uploads.
Events (id is always the upload ID):
progress - { id: string, progress: int (0-100) }
error - { id: string, error: string }
cancelled - { id: string, error: string }
completed - { id: string }
*/
export const addListener = (eventType: UploadEvent, uploadId: string, listener: Function) => {
Expand All @@ -89,4 +110,4 @@ export const addListener = (eventType: UploadEvent, uploadId: string, listener:
})
}

export default { startUpload, addListener, getFileInfo }
export default { startUpload, cancelUpload, addListener, getFileInfo }
30 changes: 27 additions & 3 deletions ios/VydiaRNFileUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ - (void)_sendEventWithName:(NSString *)eventName body:(id)body {
}

- (NSArray<NSString *> *)supportedEvents {
return @[@"RNFileUploader-progress", @"RNFileUploader-error", @"RNFileUploader-completed"];
return @[
@"RNFileUploader-progress",
@"RNFileUploader-error",
@"RNFileUploader-cancelled",
@"RNFileUploader-completed"
];
}

/*
Expand Down Expand Up @@ -159,6 +164,22 @@ - (NSString *)guessMIMETypeFromFileName: (NSString *)fileName {
}
}

/*
* Cancels file upload
* Accepts upload ID as a first argument, this upload will be cancelled
* Event "cancelled" will be fired when upload is cancelled.
*/
RCT_EXPORT_METHOD(cancelUpload: (NSString *)cancelUploadId resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
[_urlSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionTask *uploadTask in uploadTasks) {
if (uploadTask.taskDescription == cancelUploadId) {
[uploadTask cancel];
}
}
}];
resolve([NSNumber numberWithBool:YES]);
}

- (NSData *)createBodyWithBoundary:(NSString *)boundary
path:(NSString *)path
fieldName:(NSString *)fieldName {
Expand Down Expand Up @@ -193,7 +214,6 @@ - (NSURLSession *)urlSession: (int) thisUploadId{
return _urlSession;
}


#pragma NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session
Expand Down Expand Up @@ -223,7 +243,11 @@ - (void)URLSession:(NSURLSession *)session
else
{
[data setObject:error.localizedDescription forKey:@"error"];
[self _sendEventWithName:@"RNFileUploader-error" body:data];
if (error.code == NSURLErrorCancelled) {
[self _sendEventWithName:@"RNFileUploader-cancelled" body:data];
} else {
[self _sendEventWithName:@"RNFileUploader-error" body:data];
}
}
}

Expand Down