Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
add extractArchive bool parameter:
Browse files Browse the repository at this point in the history
 - make it possible de specify non-zip files in sync method
 - backup the destination file in the specified location
  • Loading branch information
revolunet committed Jul 28, 2015
1 parent a52ed4c commit f4c9d1e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
73 changes: 53 additions & 20 deletions src/ios/ContentSync.m
Expand Up @@ -38,16 +38,20 @@ - (void)sync:(CDVInvokedUrlCommand*)command {

NSString* type = [command argumentAtIndex:2];
BOOL local = [type isEqualToString:@"local"];
BOOL extractArchive = [[command argumentAtIndex:7 withDefault:@(YES)] boolValue];

// if local mode, check file existence before download
// we check the existence of file defined via option.id in the "Library" folder
if(local == YES) {
NSString* appId = [command argumentAtIndex:1];
NSLog(@"Requesting local copy of %@", appId);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
NSURL *libraryDirectoryUrl = [URLs objectAtIndex:0];

NSURL *appPath = [libraryDirectoryUrl URLByAppendingPathComponent:appId];


// if local file found, return success directly
if([fileManager fileExistsAtPath:[appPath path]]) {
NSLog(@"Found local copy %@", [appPath path]);
CDVPluginResult *pluginResult = nil;
Expand All @@ -60,24 +64,27 @@ - (void)sync:(CDVInvokedUrlCommand*)command {
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

// copy some cordova assets from bundle to destination folder
// allow to load external apps
BOOL copyCordovaAssets = [[command argumentAtIndex:4 withDefault:@(NO)] boolValue];
BOOL copyRootApp = [[command argumentAtIndex:5 withDefault:@(NO)] boolValue];

if(copyRootApp == YES || copyCordovaAssets == YES) {
CDVPluginResult *pluginResult = nil;
NSError* error = nil;

NSLog(@"Creating app directory %@", [appPath path]);
[fileManager createDirectoryAtPath:[appPath path] withIntermediateDirectories:NO attributes:nil error:&error];

NSError* errorSetting = nil;
BOOL success = [appPath setResourceValue: [NSNumber numberWithBool: YES]
forKey: NSURLIsExcludedFromBackupKey error: &errorSetting];

if(success == NO) {
NSLog(@"WARNING: %@ might be backed up to iCloud!", [appPath path]);
}

if(error != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsInt:LOCAL_ERR];
NSLog(@"%@", [error localizedDescription]);
Expand All @@ -102,11 +109,13 @@ - (void)sync:(CDVInvokedUrlCommand*)command {

__weak ContentSync* weakSelf = self;

// start download
[self.commandDelegate runInBackground:^{
[weakSelf startDownload:command extractArchive:YES];
[weakSelf startDownload:command extractArchive:extractArchive];
}];
}

// download a file directly, without local check and extraction
- (void) download:(CDVInvokedUrlCommand*)command {
__weak ContentSync* weakSelf = self;

Expand All @@ -115,6 +124,7 @@ - (void) download:(CDVInvokedUrlCommand*)command {
}];
}

// start a download task
- (void)startDownload:(CDVInvokedUrlCommand*)command extractArchive:(BOOL)extractArchive {

CDVPluginResult* pluginResult = nil;
Expand Down Expand Up @@ -234,35 +244,58 @@ - (void) URLSession:(NSURLSession*)session downloadTask:(NSURLSessionDownloadTas
NSURL *libraryDirectory = [URLs objectAtIndex:0];

NSURL *originalURL = [[downloadTask originalRequest] URL];

ContentSyncTask* sTask = [self findSyncDataByDownloadTask:downloadTask];


// appId is options.id
NSString* appId = [sTask.command.arguments objectAtIndex:1];
// output dir for the ZIP
NSURL *extractURL = [libraryDirectory URLByAppendingPathComponent:appId];

// where the requested file is stored
NSURL *sourceURL = [libraryDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];
NSError *errorCopy;

// if the url should not be extracted (not a ZIP), we download it directly to the target directory
// ensure the directory exists before download
if (sTask.extractArchive == NO) {
sourceURL = extractURL;
NSURL* folder = [extractURL URLByDeletingLastPathComponent];
[fileManager createDirectoryAtURL:folder withIntermediateDirectories:YES attributes:nil error:nil];
}

// delete file destination if already exists
[fileManager removeItemAtURL:sourceURL error:NULL];

// copy tmp download file to destination
NSError *errorCopy;
BOOL success = [fileManager copyItemAtURL:downloadURL toURL:sourceURL error:&errorCopy];

// once file download and saved in the expected destination
if(success) {
ContentSyncTask* sTask = [self findSyncDataByDownloadTask:downloadTask];

if(sTask) {
if(sTask.extractArchive == YES) {
sTask.archivePath = [sourceURL path];
// FIXME there is probably a better way to do this
NSString* appId = [sTask.command.arguments objectAtIndex:1];
NSURL *extractURL = [libraryDirectory URLByAppendingPathComponent:appId];
NSString* type = [sTask.command argumentAtIndex:2 withDefault:@"replace"];

// copy root app right before we extract
// copy root app cordova stuff right before we extract if copyRootApp=true
if([[[sTask command] argumentAtIndex:5 withDefault:@(NO)] boolValue] == YES) {
NSLog(@"Copying Cordova Root App to %@ as requested", [extractURL path]);
if(![self copyCordovaAssets:[extractURL path] copyRootApp:YES]) {
NSLog(@"Error copying Cordova Root App");
};
}

// launch unzip process
CDVInvokedUrlCommand* command = [CDVInvokedUrlCommand commandFromJson:[NSArray arrayWithObjects:sTask.command.callbackId, @"Zip", @"unzip", [NSMutableArray arrayWithObjects:[sourceURL absoluteString], [extractURL absoluteString], type, nil], nil]];
[self unzip:command];
} else {
sTask.archivePath = [sourceURL absoluteString];
// if no extraction needed, fire the cordova success callback
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:2];
[message setObject:[extractURL absoluteString] forKey:@"localPath"];
[message setObject:@"true" forKey:@"cached"];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];

[self.commandDelegate sendPluginResult:pluginResult callbackId:sTask.command.callbackId];
return;
}
}
} else {
Expand Down Expand Up @@ -317,7 +350,7 @@ - (void)unzip:(CDVInvokedUrlCommand*)command {
NSURL* destinationURL = [NSURL URLWithString:[command argumentAtIndex:1]];
NSString* type = [command argumentAtIndex:2 withDefault:@"replace"];
BOOL replace = [type isEqualToString:@"replace"];

NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:[destinationURL path]] && replace == YES) {
NSLog(@"%@ already exists. Deleting it since type is set to `replace`", [destinationURL path]);
Expand Down Expand Up @@ -383,7 +416,7 @@ - (void) zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_in
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:sTask.command.callbackId];
// END

// Do not BACK UP folder to iCloud
NSURL* appURL = [NSURL fileURLWithPath: path];
NSError* error = nil;
Expand Down
4 changes: 2 additions & 2 deletions www/index.js
Expand Up @@ -67,7 +67,7 @@ var ContentSync = function(options) {
if (typeof options.copyRootApp === 'undefined') {
options.copyRootApp = false;
}

if (typeof options.timeout === 'undefined') {
options.timeout = 15.0;
}
Expand All @@ -93,7 +93,7 @@ var ContentSync = function(options) {

// wait at least one process tick to allow event subscriptions
setTimeout(function() {
exec(success, fail, 'Sync', 'sync', [options.src, options.id, options.type, options.headers, options.copyCordovaAssets, options.copyRootApp, options.timeout]);
exec(success, fail, 'Sync', 'sync', [options.src, options.id, options.type, options.headers, options.copyCordovaAssets, options.copyRootApp, options.timeout, options.extractArchive]);
}, 10);
};

Expand Down

0 comments on commit f4c9d1e

Please sign in to comment.