[CB-836] Abort functionality added to FileTransfer
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/33afb307 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/33afb307 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/33afb307 Branch: refs/heads/master Commit: 33afb30726b9480368ee315eb19c7651650ba340 Parents: 4f75831 Author: Cory Thompson <coryjthomp...@gmail.com> Authored: Thu Aug 9 10:33:32 2012 +1000 Committer: Andrew Grieve <agri...@chromium.org> Committed: Thu Sep 20 22:32:26 2012 -0400 ---------------------------------------------------------------------- CordovaLib/Classes/CDVFileTransfer.h | 1 + CordovaLib/Classes/CDVFileTransfer.m | 32 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/33afb307/CordovaLib/Classes/CDVFileTransfer.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVFileTransfer.h b/CordovaLib/Classes/CDVFileTransfer.h old mode 100644 new mode 100755 index d81cd13..67e3bd0 --- a/CordovaLib/Classes/CDVFileTransfer.h +++ b/CordovaLib/Classes/CDVFileTransfer.h @@ -63,6 +63,7 @@ extern NSString* const kOptionsKeyCookie; @property (nonatomic, strong) CDVFileTransfer* command; @property (nonatomic, assign) CDVFileTransferDirection direction; @property (nonatomic, copy) NSString* callbackId; +@property (nonatomic, copy) NSString* objectId; @property (nonatomic, copy) NSString* source; @property (nonatomic, copy) NSString* target; @property (assign) int responseCode; // atomic http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/33afb307/CordovaLib/Classes/CDVFileTransfer.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVFileTransfer.m b/CordovaLib/Classes/CDVFileTransfer.m old mode 100644 new mode 100755 index 3115f34..b7e8e8c --- a/CordovaLib/Classes/CDVFileTransfer.m +++ b/CordovaLib/Classes/CDVFileTransfer.m @@ -63,6 +63,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { return totalBytesWritten; } +static NSMutableArray* _abortTriggered = nil; @implementation CDVFileTransfer - (NSString*) escapePathComponentForUrlString:(NSString*)urlString @@ -239,11 +240,13 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { - (CDVFileTransferDelegate*) delegateForUploadCommand:(CDVInvokedUrlCommand *)command { NSString* target = [command.arguments objectAtIndex:0]; NSString* server = [command.arguments objectAtIndex:1]; + NSString* objectId = [command.arguments objectAtIndex:9]; CDVFileTransferDelegate* delegate = [[CDVFileTransferDelegate alloc] init]; delegate.command = self; - delegate.direction = CDV_TRANSFER_UPLOAD; delegate.callbackId = command.callbackId; + delegate.direction = CDV_TRANSFER_UPLOAD; + delegate.objectId = objectId; delegate.source = server; delegate.target = target; return delegate; @@ -273,10 +276,20 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { } } +- (void) abort:(CDVInvokedUrlCommand*)command { + NSString* objectId = [command.arguments objectAtIndex:0]; + if(_abortTriggered == nil){ + _abortTriggered = [NSMutableArray array]; + } + [_abortTriggered addObject:objectId]; +>>>>>>> Abort functionality added to FileTransfer +} + - (void) download:(CDVInvokedUrlCommand*)command { DLog(@"File Transfer downloading file..."); NSString * sourceUrl = [command.arguments objectAtIndex:0]; NSString * filePath = [command.arguments objectAtIndex:1]; + NSString * objectId = [command.arguments objectAtIndex:2]; CDVPluginResult *result = nil; CDVFileTransferError errorCode = 0; @@ -312,6 +325,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { delegate.command = self; delegate.direction = CDV_TRANSFER_DOWNLOAD; delegate.callbackId = command.callbackId; + delegate.objectId = objectId; delegate.source = sourceUrl; delegate.target = filePath; @@ -349,7 +363,7 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { @implementation CDVFileTransferDelegate -@synthesize callbackId, source, target, responseData, command, bytesWritten, direction, responseCode; +@synthesize callbackId, source, target, responseData, command, bytesWritten, direction, responseCode, objectId; - (void)connectionDidFinishLoading:(NSURLConnection *)connection @@ -446,10 +460,24 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) { - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { + if([_abortTriggered containsObject:self.objectId]) + { + NSLog(@"Aborted File Transfer"); + [connection cancel]; + [_abortTriggered removeObject:self.objectId]; + return; + } [self.responseData appendData:data]; } - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { + if([_abortTriggered containsObject:self.objectId]) + { + NSLog(@"Aborted File Transfer"); + [connection cancel]; + [_abortTriggered removeObject:self.objectId]; + return; + } self.bytesWritten = totalBytesWritten; } /* TESTING ONLY CODE