added download method to filetransfer, interface is the same like on android pull request 42
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/2ff82683 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/2ff82683 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/2ff82683 Branch: refs/heads/master Commit: 2ff82683d3c4f61ba90bd122a1d51d72c8b63830 Parents: 7fb9188 Author: Alexander Keller <a...@aflx.de> Authored: Mon Nov 28 09:03:30 2011 +0100 Committer: Alexander Keller <a...@aflx.de> Committed: Mon Nov 28 09:03:30 2011 +0100 ---------------------------------------------------------------------- PhoneGapLib/Classes/FileTransfer.h | 7 ++ PhoneGapLib/Classes/FileTransfer.m | 72 +++++++++++++++++++++ PhoneGapLib/javascripts/core/filetransfer.js | 4 + 3 files changed, 83 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/2ff82683/PhoneGapLib/Classes/FileTransfer.h ---------------------------------------------------------------------- diff --git a/PhoneGapLib/Classes/FileTransfer.h b/PhoneGapLib/Classes/FileTransfer.h index b7d0fa0..7a752d0 100644 --- a/PhoneGapLib/Classes/FileTransfer.h +++ b/PhoneGapLib/Classes/FileTransfer.h @@ -21,8 +21,15 @@ typedef int FileTransferError; @interface PGFileTransfer : PGPlugin { } + +@property (nonatomic, copy) NSString* callbackID; + - (void) upload:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; +- (void) download:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; +-(void) downloadFile:(NSMutableArray*)arguments; +-(void) downloadSuccess:(NSMutableArray*)arguments; +-(void) downloadFail:(NSMutableArray*)arguments; @end http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/2ff82683/PhoneGapLib/Classes/FileTransfer.m ---------------------------------------------------------------------- diff --git a/PhoneGapLib/Classes/FileTransfer.m b/PhoneGapLib/Classes/FileTransfer.m index 82e8afd..d84e8f0 100644 --- a/PhoneGapLib/Classes/FileTransfer.m +++ b/PhoneGapLib/Classes/FileTransfer.m @@ -12,6 +12,8 @@ @implementation PGFileTransfer +@synthesize callbackID; + - (void) upload:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { NSString* callbackId = [arguments objectAtIndex:0]; NSString* fileKey = (NSString*)[options objectForKey:@"fileKey"]; @@ -134,6 +136,76 @@ } +- (void) download:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { + NSLog(@"File Transfer downloading file..."); + + [self performSelectorInBackground:@selector(downloadFile:) withObject:arguments]; +} + +-(void) downloadFile:(NSMutableArray*)arguments { + NSString * callbackId = [arguments objectAtIndex:0]; + NSString * sourceUrl = [arguments objectAtIndex:1]; + NSString * filePath = [arguments objectAtIndex:2]; + + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:sourceUrl] ]; + + NSLog(@"Write file %@", filePath); + NSError *error=[[[NSError alloc]init] autorelease]; + + @try { + NSString * parentPath = [ filePath stringByDeletingLastPathComponent ]; + + // check if the path exists => create directories if needed + if(![[NSFileManager defaultManager] fileExistsAtPath:parentPath ]) [[NSFileManager defaultManager] createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:nil]; + + BOOL response = [data writeToFile:filePath options:NSDataWritingFileProtectionNone error:&error]; + + if ( response == NO ) { + // send our results back to the main thread + NSArray * results = [NSArray arrayWithObjects: callbackId, [error description], nil]; + [self performSelectorOnMainThread:@selector(downloadFail:) withObject:results waitUntilDone:YES]; + } else { + // jump back to main thread + [self performSelectorOnMainThread:@selector(downloadSuccess:) withObject:arguments waitUntilDone:YES]; + } + } + @catch (id exception) { + NSLog(@"File Transfer Error %@", [error description]); + + // jump back to main thread + [self performSelectorOnMainThread:@selector(fail:) withObject:[error description] waitUntilDone:YES]; + } + + [pool drain]; +} + +-(void) downloadSuccess:(NSMutableArray *)arguments +{ + NSString * callbackId = [arguments objectAtIndex:0]; + NSString * filePath = [arguments objectAtIndex:2]; + + NSLog(@"File Transfert Download success"); + + PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString: + [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + + [self writeJavascript: [pluginResult toSuccessCallbackString:callbackId]]; +} + +-(void) downloadFail:(NSMutableArray *)arguments +{ + NSString * callbackId = [arguments objectAtIndex:0]; + NSString * error = [arguments objectAtIndex:1]; + + NSLog(@"File Transfer Error: %@", [error description]); + + PluginResult* pluginResult = [PluginResult resultWithStatus:PGCommandStatus_OK messageAsString: + [error stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + + [self writeJavascript: [pluginResult toErrorCallbackString:callbackId]]; +} + @end http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/2ff82683/PhoneGapLib/javascripts/core/filetransfer.js ---------------------------------------------------------------------- diff --git a/PhoneGapLib/javascripts/core/filetransfer.js b/PhoneGapLib/javascripts/core/filetransfer.js index ade2cae..0f53adf 100644 --- a/PhoneGapLib/javascripts/core/filetransfer.js +++ b/PhoneGapLib/javascripts/core/filetransfer.js @@ -91,6 +91,10 @@ FileTransfer.prototype._castUploadResult = function(pluginResult) { return pluginResult; } +FileTransfer.prototype.download = function(sourceUrl, filePath, successCallback, errorCallback) { + PhoneGap.exec(successCallback, errorCallback, 'com.phonegap.filetransfer', 'download', [sourceUrl, filePath]); +}; + /** * Options to customize the HTTP request used to upload files. * @param fileKey {String} Name of file request parameter.