From acbc5b44a05fe4b83663702ef8350b083deb9202 Mon Sep 17 00:00:00 2001 From: jrouault Date: Thu, 9 Jul 2015 17:28:54 +0900 Subject: [PATCH 1/3] Delete files now specifying URI instead of file URI. --- README.md | 16 ++--- platforms/android/assets/www/index.html | 45 ++++-------- .../plugin/WizAssets/WizAssetsPlugin.java | 32 ++++----- .../Plugins/WizAssetsPlugin/WizAssetsPlugin.m | 71 ++++++++----------- platforms/ios/www/index.html | 45 ++++-------- 5 files changed, 80 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index 8620a9e..06b41f2 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ PhoneGap plugin for managing application assets with javascript asset maps. Incl ### downloadFile() -**wizAssets.downloadFile(String URL, String filePathToBeStoredWithFilename, Function success, Function fail);** +**wizAssets.downloadFile(String URL, String URI, Function success, Function fail);** - downloads a file to native App directory @ ./ + gameDir+ / +filePathToBeStoredWithFilename
-- A success returns URI string like; file://documents/settings/img/cards/card001.jpg
+- A success returns a file URI string like; file://documents/settings/img/cards/card001.jpg
- example; ``` @@ -48,17 +48,17 @@ wizAssets.deleteFile("file://documents/settings/img/cards/card001.jpg", successC ### deleteFiles() -**wizAssets.deleteFiles(Array manyURIs, Function success, Function fail );** +**wizAssets.deleteFiles(Array URIs, Function success, Function fail );** -- delete all URIs in Array like; [ "file://documents/settings/img/cards/card001.jpg", "file://documents/settings/img/cards/card002.jpg " .. ]
-- if you do not specify a filename only dir, then all contents of dir will be deleted; file://documents/settings/img/cards
+- delete all URIs in Array like; [ "img/cards/card001.jpg", "img/cards/card002.jpg " .. ]
+- if you do not specify a filename only dir, then all contents of dir will be deleted; img/cards
- the array CAN contain one URI string ### getFileURI() -**wizAssets.getFileURI(String filePathWithFilename, Function success, Function fail);** +**wizAssets.getFileURI(String URI, Function success, Function fail);** -- A success returns URI string like file://documents/settings/img/cards/card001.jpg
+- A success returns a file URI string like file://documents/settings/img/cards/card001.jpg
- example; ``` @@ -69,7 +69,7 @@ wizAssets.getFileURI("img/ui/logo.jpg", successCallback, failCallback); **wizAssets.getFileURIs(Function success, Function fail);** -- A success returns URI hashmap such as +- A success returns a file URI hashmap such as ``` { diff --git a/platforms/android/assets/www/index.html b/platforms/android/assets/www/index.html index fe1bdca..c8c90d1 100644 --- a/platforms/android/assets/www/index.html +++ b/platforms/android/assets/www/index.html @@ -372,36 +372,24 @@ ); } - function deleteFile(path, download_button, delete_button, img) { + function deleteFile(uri, download_button, delete_button, img) { - // Define success callback for getting the URI. - function getFileURISuccessCallback(fileUri) { - - // Define success callback for deleting the URI. - function deleteFileSuccessCallback() { - // Hide the image, enable download button, and disable delete button - hideImage(img); - document.getElementById(download_button).disabled = false; - document.getElementById(delete_button).disabled = true; - alert("Delete Success:\n" + path + "\nURI:\n" + fileUri); - } - - // Define error callback for deleting the URI. - function deleteFileErrorCallback(error) { - alert("Delete Fail:\n" + error + "\nFile was probably already deleted."); - } - - // Delete the URI (and invoke appropriate callback). - window.wizAssets.deleteFile(fileUri, deleteFileSuccessCallback, deleteFileErrorCallback); + // Define success callback for deleting the URI. + function deleteFileSuccessCallback() { + // Hide the image, enable download button, and disable delete button + hideImage(img); + document.getElementById(download_button).disabled = false; + document.getElementById(delete_button).disabled = true; + alert("Delete Success:\n" + uri); } - // Define error callback for getting the URI. - function getFileURIErrorCallback(error) { - alert("Failed to get file URI error: " + error); + // Define error callback for deleting the URI. + function deleteFileErrorCallback(error) { + alert("Delete Fail:\n" + error + "\nFile was probably already deleted."); } - // Get the URI (and invoke appropriate callback). - window.wizAssets.getFileURI(path, getFileURISuccessCallback, getFileURIErrorCallback); + // Delete the URI (and invoke appropriate callback). + window.wizAssets.deleteFile(uri, deleteFileSuccessCallback, deleteFileErrorCallback); } function getFileURI(path) { @@ -459,11 +447,8 @@ } // Delete the URIs (and invoke appropriate callback). - alert( "Deleting " + Object.keys(fileUriMap).length + " files" ); - var files = []; - for ( var key in fileUriMap) { - files.push( fileUriMap[key] ); - } + var files = Object.keys(fileUriMap); + alert("Deleting " + files.length + " files"); window.wizAssets.deleteFiles(files, deleteFilesSuccessCallback, deleteFilesErrorCallback); } diff --git a/platforms/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java b/platforms/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java index 588479f..99fc575 100755 --- a/platforms/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java +++ b/platforms/android/src/jp/wizcorp/phonegap/plugin/WizAssets/WizAssetsPlugin.java @@ -127,16 +127,16 @@ public void run() { // Delete all files from given array Log.d(TAG, "[deleteFiles] *********** "); - deleteAssets(args, false, new DeleteAssetsCallback(callbackContext)); + deleteFiles(args, callbackContext); return true; } else if (action.equals(DELETE_FILE_ACTION)) { Log.d(TAG, "[deleteFile] *********** " + args.getString(0)); - String filePath = args.getString(0); + String uri = args.getString(0); try { - deleteAsset(filePath, false); + deleteAsset(uri); } catch (IOException e) { callbackContext.error("Deleting file failed."); return true; @@ -163,17 +163,15 @@ private String buildAssetFilePathFromUri(String uri) { return pathToStorage + uri; } - private void deleteAssets(JSONArray files, boolean isUri, DeleteAssetsCallback callback) { - AsyncDelete asyncDelete = new AsyncDelete(callback, isUri); - asyncDelete.execute(files); + private void deleteFiles(JSONArray uris, CallbackContext callbackContext) { + DeleteAssetsCallback callback = new DeleteAssetsCallback(callbackContext); + AsyncDelete asyncDelete = new AsyncDelete(callback); + asyncDelete.execute(uris); } - private void deleteAsset(String filePath, boolean isUri) throws IOException { - // If file is in bundle we cannot delete so ignore and protect whole cache folder from being deleted - if (filePath != "" && !filePath.contains("www/assets")) { - if (isUri) { - filePath = buildAssetFilePathFromUri(filePath); - } + private void deleteAsset(String uri) throws IOException { + if (uri != "") { + String filePath = buildAssetFilePathFromUri(uri); File file = new File(filePath); boolean isDirectory = file.isDirectory(); boolean deleteSucceed = deleteFile(file); @@ -247,7 +245,6 @@ public String getErrorMessage(int errorCode) { private class AsyncDelete extends AsyncTask { private DeleteAssetsCallback callback; - private boolean isUri; private static final int NO_ERROR = 0; private static final int JSON_TYPE_ERROR = -1; @@ -263,9 +260,8 @@ private class AsyncDelete extends AsyncTask { private static final String CALLBACK_ERROR_MESSAGE = "Call to delete callback failed."; // Constructor - public AsyncDelete(DeleteAssetsCallback callback, boolean isUri) { + public AsyncDelete(DeleteAssetsCallback callback) { this.callback = callback; - this.isUri = isUri; } protected Integer doInBackground(JSONArray... jsonArrays) { @@ -276,10 +272,10 @@ protected Integer doInBackground(JSONArray... jsonArrays) { // We only process one array, no more than one JSON array should be passed JSONArray jsonArray = jsonArrays[0]; - int countFiles = jsonArray.length(); - for (int i = 0; i < countFiles; i++) { + int countUris = jsonArray.length(); + for (int i = 0; i < countUris; i++) { try { - deleteAsset(jsonArray.getString(i), isUri); + deleteAsset(jsonArray.getString(i)); } catch (JSONException e) { return JSON_TYPE_ERROR; } catch (IOException e) { diff --git a/platforms/ios/HelloCordova/Plugins/WizAssetsPlugin/WizAssetsPlugin.m b/platforms/ios/HelloCordova/Plugins/WizAssetsPlugin/WizAssetsPlugin.m index 4a581e3..a180807 100755 --- a/platforms/ios/HelloCordova/Plugins/WizAssetsPlugin/WizAssetsPlugin.m +++ b/platforms/ios/HelloCordova/Plugins/WizAssetsPlugin/WizAssetsPlugin.m @@ -265,9 +265,9 @@ - (void)getFileURIs:(CDVInvokedUrlCommand *)command { */ - (void)deleteFile:(CDVInvokedUrlCommand *)command { CDVPluginResult *pluginResult; - NSString *filePath = [command.arguments objectAtIndex:0]; + NSString *uri = [command.arguments objectAtIndex:0]; - if ([self deleteAsset:filePath isUri:NO error:nil]) { + if ([self deleteAsset:uri error:nil]) { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Deleting file failed."]; @@ -280,38 +280,21 @@ - (void)deleteFile:(CDVInvokedUrlCommand *)command { * deleteFiles - delete all resources specified in array from app folder */ - (void)deleteFiles:(CDVInvokedUrlCommand *)command { - [self deleteAssets:command isUri:NO]; + [self performSelectorInBackground:@selector(backgroundDelete:) withObject:command]; } -/* - * deleteAssets - delete all resources specified in array from app folder - */ -- (void)deleteAssets:(CDVInvokedUrlCommand *)command isUri:(BOOL)isUri { - NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys: - command, @"command", - [NSNumber numberWithBool:isUri], @"isUri", - nil]; - [self performSelectorInBackground:@selector(backgroundDelete:) withObject:args]; -} - -- (void)backgroundDelete:(NSDictionary *)args { +- (void)backgroundDelete:(CDVInvokedUrlCommand *)command { // Create a pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - CDVInvokedUrlCommand *command = [args objectForKey:@"command"]; - BOOL isUri = [[args objectForKey:@"isUri"] boolValue]; - - NSMutableArray *fileArray = [[NSMutableArray alloc] initWithArray:command.arguments copyItems:YES]; + NSMutableArray *uris = [[NSMutableArray alloc] initWithArray:command.arguments copyItems:YES]; NSError *error = nil; - for (int i=0; i< [fileArray count]; i++) { - NSString *filePath = [fileArray objectAtIndex:i]; - if (![self deleteAsset:filePath isUri:isUri error:nil]) { - error = [NSError errorWithDomain:assetsErrorKey code:100 userInfo:nil]; - break; - } + for (int i=0; i< [uris count]; i++) { + NSString *uri = [uris objectAtIndex:i]; + [self deleteAsset:uri error:&error]; } - [fileArray release]; + [uris release]; NSArray *callbackData = [[NSArray alloc] initWithObjects:command.callbackId, error, nil]; @@ -347,26 +330,28 @@ - (void)completeDelete:(NSArray *)callbackdata { /* * deleteAsset - delete resource specified in string from app folder */ -- (BOOL)deleteAsset:(NSString *)filePath isUri:(BOOL)isUri error:(NSError **)error { - NSFileManager *filemgr = [NSFileManager defaultManager]; - - if (filePath && [filePath length] > 0) { - // Check if the file is not in the bundle.. - NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; - if ([filePath rangeOfString:bundlePath].location == NSNotFound) { - if (isUri) { - filePath = [self buildAssetFilePathFromUri:filePath]; - } +- (BOOL)deleteAsset:(NSString *)uri error:(NSError **)error { + NSFileManager *fileManager = [NSFileManager defaultManager]; - NSError *localError = nil; - if ([filemgr fileExistsAtPath:filePath] && ![filemgr removeItemAtPath:filePath error:&localError] && error != NULL) { - *error = [NSError errorWithDomain:assetsErrorKey code:200 userInfo:nil]; - return NO; - } - } else if (error != NULL) { + if (!uri || [uri length] == 0) { + if (error != nil) { *error = [NSError errorWithDomain:assetsErrorKey code:200 userInfo:nil]; - return NO; } + return NO; + } + + NSError *localError = nil; + NSString *filePath = [self buildAssetFilePathFromUri:uri]; + if (![fileManager removeItemAtPath:filePath error:&localError]) { + // File didn't exist in the first place, it's not an error + if ([[localError domain] isEqualToString:NSCocoaErrorDomain] && [localError code] == NSFileNoSuchFileError) { + return YES; + } + // File deletion failed, it's an error + if (error != nil) { + *error = localError; + } + return NO; } return YES; } diff --git a/platforms/ios/www/index.html b/platforms/ios/www/index.html index fe1bdca..c8c90d1 100644 --- a/platforms/ios/www/index.html +++ b/platforms/ios/www/index.html @@ -372,36 +372,24 @@ ); } - function deleteFile(path, download_button, delete_button, img) { + function deleteFile(uri, download_button, delete_button, img) { - // Define success callback for getting the URI. - function getFileURISuccessCallback(fileUri) { - - // Define success callback for deleting the URI. - function deleteFileSuccessCallback() { - // Hide the image, enable download button, and disable delete button - hideImage(img); - document.getElementById(download_button).disabled = false; - document.getElementById(delete_button).disabled = true; - alert("Delete Success:\n" + path + "\nURI:\n" + fileUri); - } - - // Define error callback for deleting the URI. - function deleteFileErrorCallback(error) { - alert("Delete Fail:\n" + error + "\nFile was probably already deleted."); - } - - // Delete the URI (and invoke appropriate callback). - window.wizAssets.deleteFile(fileUri, deleteFileSuccessCallback, deleteFileErrorCallback); + // Define success callback for deleting the URI. + function deleteFileSuccessCallback() { + // Hide the image, enable download button, and disable delete button + hideImage(img); + document.getElementById(download_button).disabled = false; + document.getElementById(delete_button).disabled = true; + alert("Delete Success:\n" + uri); } - // Define error callback for getting the URI. - function getFileURIErrorCallback(error) { - alert("Failed to get file URI error: " + error); + // Define error callback for deleting the URI. + function deleteFileErrorCallback(error) { + alert("Delete Fail:\n" + error + "\nFile was probably already deleted."); } - // Get the URI (and invoke appropriate callback). - window.wizAssets.getFileURI(path, getFileURISuccessCallback, getFileURIErrorCallback); + // Delete the URI (and invoke appropriate callback). + window.wizAssets.deleteFile(uri, deleteFileSuccessCallback, deleteFileErrorCallback); } function getFileURI(path) { @@ -459,11 +447,8 @@ } // Delete the URIs (and invoke appropriate callback). - alert( "Deleting " + Object.keys(fileUriMap).length + " files" ); - var files = []; - for ( var key in fileUriMap) { - files.push( fileUriMap[key] ); - } + var files = Object.keys(fileUriMap); + alert("Deleting " + files.length + " files"); window.wizAssets.deleteFiles(files, deleteFilesSuccessCallback, deleteFilesErrorCallback); } From f83a67fae2eba3378774c2a90c745b80234eaa8b Mon Sep 17 00:00:00 2001 From: jrouault Date: Mon, 13 Jul 2015 12:45:48 +0900 Subject: [PATCH 2/3] Rename URI to localURI for clarity. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 06b41f2..af89c60 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ PhoneGap plugin for managing application assets with javascript asset maps. Incl ### downloadFile() -**wizAssets.downloadFile(String URL, String URI, Function success, Function fail);** +**wizAssets.downloadFile(String remoteURL, String localURI, Function success, Function fail);** - downloads a file to native App directory @ ./ + gameDir+ / +filePathToBeStoredWithFilename
- A success returns a file URI string like; file://documents/settings/img/cards/card001.jpg
@@ -36,7 +36,7 @@ wizAssets.downloadFile("http://google.com/logo.jpg", "img/ui/logo.jpg", successC ### deleteFile() -**wizAssets.deleteFile(string URI, Function success, Function fail);** +**wizAssets.deleteFile(string localURI, Function success, Function fail);** - deletes the file specified by the URI
- if the URI does not exist fail will be called with error NotFoundError
@@ -48,7 +48,7 @@ wizAssets.deleteFile("file://documents/settings/img/cards/card001.jpg", successC ### deleteFiles() -**wizAssets.deleteFiles(Array URIs, Function success, Function fail );** +**wizAssets.deleteFiles(Array localURIs, Function success, Function fail );** - delete all URIs in Array like; [ "img/cards/card001.jpg", "img/cards/card002.jpg " .. ]
- if you do not specify a filename only dir, then all contents of dir will be deleted; img/cards
@@ -56,7 +56,7 @@ wizAssets.deleteFile("file://documents/settings/img/cards/card001.jpg", successC ### getFileURI() -**wizAssets.getFileURI(String URI, Function success, Function fail);** +**wizAssets.getFileURI(String localURI, Function success, Function fail);** - A success returns a file URI string like file://documents/settings/img/cards/card001.jpg
- example; From 5ceb39b6bb07352c625324c1654c4652c8f13040 Mon Sep 17 00:00:00 2001 From: jrouault Date: Mon, 13 Jul 2015 16:13:29 +0900 Subject: [PATCH 3/3] Renamed API parameters in documentation for clarity. --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index af89c60..8b1d58d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ # Description -PhoneGap plugin for managing application assets with javascript asset maps. Includes( iOS background threaded) downloadFile, getFileURI, getFileURIs, deleteFile. +PhoneGap plugin for managing application assets with javascript asset maps. Includes (iOS background threaded) downloadFile, getFileURI, getFileURIs, deleteFile, deleteFiles. ## Install (with Plugman) @@ -24,23 +24,23 @@ PhoneGap plugin for managing application assets with javascript asset maps. Incl ### downloadFile() -**wizAssets.downloadFile(String remoteURL, String localURI, Function success, Function fail);** +**wizAssets.downloadFile(String remoteURL, String assetId, Function success, Function fail);** - downloads a file to native App directory @ ./ + gameDir+ / +filePathToBeStoredWithFilename
-- A success returns a file URI string like; file://documents/settings/img/cards/card001.jpg
+- A success returns a local URL string like; file://documents/settings/img/cards/card001.jpg
- example; ``` wizAssets.downloadFile("http://google.com/logo.jpg", "img/ui/logo.jpg", successCallback, failCallback); ``` -### deleteFile() +### deleteFile() -**wizAssets.deleteFile(string localURI, Function success, Function fail);** +**wizAssets.deleteFile(string assetId, Function success, Function fail);** -- deletes the file specified by the URI
-- if the URI does not exist fail will be called with error NotFoundError
-- if the URI cannot be deleted (i.e. file resides in read-only memory) fail will be called with error NotModificationAllowedError +- deletes the file specified by the asset id
+- if the asset id does not exist fail will be called with error NotFoundError
+- if the asset id cannot be deleted (i.e. file resides in read-only memory) fail will be called with error NotModificationAllowedError ``` wizAssets.deleteFile("file://documents/settings/img/cards/card001.jpg", successCallback, failCallback); @@ -48,17 +48,17 @@ wizAssets.deleteFile("file://documents/settings/img/cards/card001.jpg", successC ### deleteFiles() -**wizAssets.deleteFiles(Array localURIs, Function success, Function fail );** +**wizAssets.deleteFiles(Array assetIds, Function success, Function fail );** -- delete all URIs in Array like; [ "img/cards/card001.jpg", "img/cards/card002.jpg " .. ]
-- if you do not specify a filename only dir, then all contents of dir will be deleted; img/cards
-- the array CAN contain one URI string +- delete files specified by their asset id in Array like; [ "img/cards/card001.jpg", "img/cards/card002.jpg " .. ]
+- if an asset id uses a path format and matches a folder, then the folder content will be deleted; img/cards
+- the array CAN contain one asset id ### getFileURI() -**wizAssets.getFileURI(String localURI, Function success, Function fail);** +**wizAssets.getFileURI(String assetId, Function success, Function fail);** -- A success returns a file URI string like file://documents/settings/img/cards/card001.jpg
+- A success returns a local URL string like file://documents/settings/img/cards/card001.jpg
- example; ``` @@ -69,7 +69,7 @@ wizAssets.getFileURI("img/ui/logo.jpg", successCallback, failCallback); **wizAssets.getFileURIs(Function success, Function fail);** -- A success returns a file URI hashmap such as +- A success returns a hashmap of asset id matching its local URL such as ``` {