Skip to content

Commit e4dbcf2

Browse files
committed
Merge pull request #6 from jpfiset/progress_callback
Progress callback
2 parents f2fc6c5 + 64d1f55 commit e4dbcf2

4 files changed

+50
-8
lines changed

Diff for: CouchDBAttachmentUploader.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
@interface CouchDBAttachmentUploadDelegate : NSObject {
2222
NSString* successCallback;
2323
NSString* failureCallback;
24+
NSString* progressCallback;
2425
CouchDBAttachmentUploader *uploader;
2526
}
2627

2728
@property (nonatomic, copy) NSString *successCallback;
2829
@property (nonatomic, copy) NSString *failureCallback;
30+
@property (nonatomic, copy) NSString *progressCallback;
2931
@property (nonatomic, retain) NSMutableData *responseData;
3032
@property (nonatomic, retain) CouchDBAttachmentUploader *uploader;
3133

Diff for: CouchDBAttachmentUploader.m

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@implementation CouchDBAttachmentUploadDelegate
1212

13-
@synthesize successCallback, failureCallback, responseData, uploader;
13+
@synthesize successCallback, failureCallback, progressCallback, responseData, uploader;
1414

1515
- (id)init {
1616
self = [super init];
@@ -43,9 +43,16 @@ - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData *)data {
4343
[responseData appendData:data];
4444
}
4545

46+
- (void)connection:(NSURLConnection*)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
47+
if( self.progressCallback ) {
48+
[uploader writeJavascript:[NSString stringWithFormat:@"%@(%d,%d);",self.progressCallback,totalBytesWritten,totalBytesExpectedToWrite]];
49+
}
50+
}
51+
4652
- (void)dealloc {
4753
[successCallback release];
4854
[failureCallback release];
55+
[progressCallback release];
4956
[responseData release];
5057
[uploader release];
5158
[super dealloc];
@@ -61,7 +68,8 @@ - (void)upload:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
6168
{
6269
NSUInteger argc = [arguments count];
6370

64-
if(argc < 5) {
71+
if(argc < 6) {
72+
NSLog(@"CouchDBAttachmentUploader called with invalid number of arguments: %d",argc);
6573
return;
6674
}
6775

@@ -71,6 +79,7 @@ - (void)upload:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
7179
NSString *docRevision = [arguments objectAtIndex:3];
7280
NSString *successCallback = [arguments objectAtIndex:4];
7381
NSString *failureCallback = [arguments objectAtIndex:5];
82+
NSString *progressCallback = [arguments objectAtIndex:6];
7483
NSString *contentType = [options valueForKey:@"contentType"];
7584
NSString *httpMethod = [[options valueForKey:@"method"] uppercaseString];
7685
NSString *attachmentName = [options valueForKey:@"attachmentName"];
@@ -139,6 +148,7 @@ - (void)upload:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
139148
delegate.uploader = self;
140149
delegate.successCallback = successCallback;
141150
delegate.failureCallback = failureCallback;
151+
delegate.progressCallback = progressCallback;
142152

143153
NSURLConnection * conn = [NSURLConnection connectionWithRequest:req delegate:delegate];
144154
if( nil == conn ) {

Diff for: README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ Enables a Cordova app to push binary attachments straight to a CouchDB database
1313

1414
###Usage
1515

16-
window.plugins.CouchDBAttachmentUploader.upload($('#id_camera_image').attr('src'),
16+
window.plugins.CouchDBAttachmentUploader.upload(
17+
$('#id_camera_image').attr('src'),
1718
'http://127.0.0.1:5984/couchdb',
1819
doc.id,
1920
doc.rev,
20-
function() {
21+
function(info) {
2122
//success callback
2223
},
2324
function(error) {
2425
//failure callback
2526
},
26-
{contentType: 'image/jpeg',
27-
method: 'put',
28-
attachmentName: 'photo.jpg'});
27+
{
28+
contentType: 'image/jpeg',
29+
method: 'put',
30+
attachmentName: 'photo.jpg',
31+
progress: function(bytesUploaded, bytesTotal){}
32+
}
33+
);
2934

Diff for: cordova.plugin.couchdb-attachment-upload.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88
var CouchDBAttachmentUploader = function() {}
99

1010
CouchDBAttachmentUploader.prototype.upload = function(filepath, couchURI, docID, docRevision, success, failure, options) {
11+
12+
var request = {};
13+
var progress = window.plugins.CouchDBAttachmentUploader.dummyProgress;
14+
15+
// Process options
16+
for(var key in options) {
17+
var value = options[key];
18+
19+
if( key === 'progress' && typeof(value) === 'function' ) {
20+
// Callback for progress
21+
progress = value;
22+
23+
} else if( 'contentType' === key
24+
|| 'method' === key
25+
|| 'attachmentName' === key
26+
){
27+
// Pass these options to request
28+
request[key] = value;
29+
};
30+
};
31+
32+
// Prepare callbacks
1133
var key = 'f' + this.callbackIdx++;
1234
window.plugins.CouchDBAttachmentUploader.callbackMap[key] = {
1335
success: function(result) {
@@ -22,14 +44,17 @@ CouchDBAttachmentUploader.prototype.upload = function(filepath, couchURI, docID,
2244
failure(result);
2345
delete window.plugins.CouchDBAttachmentUploader.callbackMap[key]
2446
}
47+
,progress: progress
2548
}
2649
var callback = 'window.plugins.CouchDBAttachmentUploader.callbackMap.' + key;
2750
return cordova.exec('CouchDBAttachmentUploader.upload', filepath, couchURI, docID,
28-
docRevision, callback + '.success', callback + '.failure', options);
51+
docRevision, callback + '.success', callback + '.failure',
52+
callback + '.progress', request);
2953
}
3054

3155
CouchDBAttachmentUploader.prototype.callbackMap = {};
3256
CouchDBAttachmentUploader.prototype.callbackIdx = 0;
57+
CouchDBAttachmentUploader.prototype.dummyProgress = function(bytes, total){};
3358

3459
cordova.addConstructor(function() {
3560
if(!window.plugins) {

0 commit comments

Comments
 (0)