From 094e3d86a9c5094f3a34a0b0faba8cdd51f28e9f Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Sun, 21 Jul 2019 15:46:47 +0100 Subject: [PATCH 01/15] Android implementation --- .../com/getcapacitor/plugin/Filesystem.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java index f8d11932e..2a45b9271 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -364,7 +364,13 @@ public void rmdir(PluginCall call) { return; } - boolean deleted = fileObject.delete(); + boolean deleted = false; + + try { + deleteRecursively(fileObject); + deleted = true; + } catch (IOException ignored) { + } if(deleted == false) { call.error("Unable to delete directory, unknown reason"); @@ -437,6 +443,25 @@ public void stat(PluginCall call) { } } + /** + * Helper function to recursively delete a directory + * + * @param file The file or directory to recursively delete + * @throws IOException + */ + private static void deleteRecursively(File file) throws IOException { + if (file.isFile()) { + file.delete(); + return; + } + + for (File f : file.listFiles()) { + deleteRecursively(f); + } + + file.delete(); + } + @PluginMethod() public void rename(PluginCall call) { saveCall(call); From f451bc493fb836308b83b66074c83ea3b39db184 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Sun, 21 Jul 2019 15:47:01 +0100 Subject: [PATCH 02/15] PWA implementation --- core/src/web/filesystem.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index b8f9794ed..df09821d3 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -269,8 +269,15 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { if (entry === undefined) throw Error('Folder does not exist.'); let entries = await this.dbIndexRequest('by_folder', 'getAllKeys', [IDBKeyRange.only(path)]); - if (entries.length !== 0) - throw Error('Folder is not empty.'); + + for (const entry of entries) { + let entryObj = await this.stat({path: entry, directory: options.directory}); + if (entryObj.type === 'file') { + await this.deleteFile({path: entry, directory: options.directory}); + } else { + await this.rmdir({path: entry, directory: options.directory}); + } + } await this.dbRequest('delete', [path]); return {}; From 790abcd643487436cacd9342c3231dafed4c3921 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Sun, 21 Jul 2019 15:49:36 +0100 Subject: [PATCH 03/15] Electron implementation --- electron/src/electron/filesystem.ts | 55 ++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/electron/src/electron/filesystem.ts b/electron/src/electron/filesystem.ts index 592ac58dd..4f3d4304f 100644 --- a/electron/src/electron/filesystem.ts +++ b/electron/src/electron/filesystem.ts @@ -16,6 +16,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu NodeFS:any = null; fileLocations:any = null; + Path:any = null; constructor() { super({ @@ -34,6 +35,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu this.fileLocations[FilesystemDirectory.Documents] = path.join(os.homedir(), `Documents`) + path.sep; this.NodeFS = require('fs'); + this.Path = path; } readFile(options: FileReadOptions): Promise{ @@ -117,19 +119,42 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu } rmdir(options: RmdirOptions): Promise { - return new Promise((resolve, reject) => { - if(Object.keys(this.fileLocations).indexOf(options.directory) === -1) - reject(`${options.directory} is currently not supported in the Electron implementation.`); - let lookupPath = this.fileLocations[options.directory] + options.path; - this.NodeFS.rmdir(lookupPath, (err:any) => { - if(err) { - reject(err); - return; + let {path, directory} = options; + + if (Object.keys(this.fileLocations).indexOf(directory) === -1) + return Promise.reject(`${directory} is currently not supported in the Electron implementation.`); + + return this.stat({path, directory}) + .then((stat) => { + if (stat.type === 'directory') { + return this.readdir({path, directory}) + .then((readDirResult) => { + if (!readDirResult.files.length) { + return new Promise((resolve, reject) => { + let lookupPath = this.fileLocations[directory] + path; + + this.NodeFS.rmdir(lookupPath, (err: any) => { + if (err) { + reject(err); + return; + } + + resolve(); + }); + }); + } else { + return Promise.all(readDirResult.files.map((f) => { + return this.rmdir({path: this.Path.join(path, f), directory}); + })) + .then(() => { + return this.rmdir({path, directory}); + }); + } + }); + } else { + return this.deleteFile({path, directory}); } - - resolve(); }); - }); } readdir(options: ReaddirOptions): Promise { @@ -168,7 +193,13 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu return; } - resolve({type: 'Not Available', size: stats.size, ctime: stats.ctimeMs, mtime: stats.mtimeMs, uri: lookupPath}); + resolve({ + type: (stats.isDirectory() ? 'directory' : (stats.isFile() ? 'file' : 'Not available')), + size: stats.size, + ctime: stats.ctimeMs, + mtime: stats.mtimeMs, + uri: lookupPath + }); }); }); } From cc665c6a3cb290ec14e2dfc62f29331217647dbf Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Sun, 21 Jul 2019 16:23:58 +0100 Subject: [PATCH 04/15] Fixes to PWA implementation --- core/src/web/filesystem.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index df09821d3..49efc8185 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -259,27 +259,34 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { } /** - * Remove a directory + * Recursively remove a directory and all its contents * @param options the options for the directory remove */ async rmdir(options: RmdirOptions): Promise { - const path: string = this.getPath(options.directory, options.path); + let {path, directory} = options; + const fullPath: string = this.getPath(directory, path); + + let entry = await this.dbRequest('get', [fullPath]) as EntryObj; - let entry = await this.dbRequest('get', [path]) as EntryObj; if (entry === undefined) throw Error('Folder does not exist.'); - let entries = await this.dbIndexRequest('by_folder', 'getAllKeys', [IDBKeyRange.only(path)]); - for (const entry of entries) { - let entryObj = await this.stat({path: entry, directory: options.directory}); + if (entry.type !== 'directory') + throw Error('Requested path is not a directory'); + + let readDirResult = await this.readdir({path, directory}); + + for (const entry of readDirResult.files) { + let entryPath = `${path}/${entry}`; + let entryObj = await this.stat({path: entryPath, directory}); if (entryObj.type === 'file') { - await this.deleteFile({path: entry, directory: options.directory}); + await this.deleteFile({path: entryPath, directory}); } else { - await this.rmdir({path: entry, directory: options.directory}); + await this.rmdir({path: entryPath, directory}); } } - await this.dbRequest('delete', [path]); + await this.dbRequest('delete', [fullPath]); return {}; } From ea1cbb1db1038351862206f7299e0009bd611c36 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 11:42:21 +0100 Subject: [PATCH 05/15] Add a "recursive" parameter --- core/src/core-plugin-definitions.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index b004065d3..ab158241e 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -652,6 +652,10 @@ export interface RmdirOptions { * The FilesystemDirectory to remove the directory from */ directory?: FilesystemDirectory; + /** + * Whether to recursively remove the contents of the directory (defaults to false) + */ + recursive?: boolean; } export interface ReaddirOptions { From f85a895bcdfa33d243f8a71caf6879486663d4d3 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 11:42:52 +0100 Subject: [PATCH 06/15] Do not delete a non-empty folder if the recursive flag is not set --- core/src/web/filesystem.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index 49efc8185..59ea160f8 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -263,7 +263,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { * @param options the options for the directory remove */ async rmdir(options: RmdirOptions): Promise { - let {path, directory} = options; + let {path, directory, recursive} = options; const fullPath: string = this.getPath(directory, path); let entry = await this.dbRequest('get', [fullPath]) as EntryObj; @@ -276,6 +276,9 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { let readDirResult = await this.readdir({path, directory}); + if (readDirResult.files.length !== 0 && !recursive) + throw Error('Folder is not empty'); + for (const entry of readDirResult.files) { let entryPath = `${path}/${entry}`; let entryObj = await this.stat({path: entryPath, directory}); From 20be84ab7edb7ebae56fd490cabeceb81c0ab313 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 11:43:28 +0100 Subject: [PATCH 07/15] Do not attempt to delete a directory if the recursive option is not set and the folder has content --- ios/Capacitor/Capacitor/Plugins/Filesystem.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift index 71ec4bfe3..d7be19b3f 100644 --- a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift +++ b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift @@ -253,6 +253,16 @@ public class CAPFilesystemPlugin : CAPPlugin { return } + let recursiveOption = call.get("recursive", Bool.self, false)! + + do { + let directoryContents = try FileManager.default.contentsOfDirectory(at: fileUrl, includingPropertiesForKeys: nil, options: []) + if (directoryContents.count != 0 && !recursiveOption) { + handleError(call, "Folder is not empty") + return + } + } catch {} + do { try FileManager.default.removeItem(at: fileUrl) call.success() From 998e5746ad5a3a963b9c496028d8f6f966a52a94 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 12:15:56 +0100 Subject: [PATCH 08/15] Add recursive check option --- .../src/main/java/com/getcapacitor/plugin/Filesystem.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java index 2a45b9271..051975c64 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -354,6 +354,7 @@ public void rmdir(PluginCall call) { saveCall(call); String path = call.getString("path"); String directory = getDirectoryParameter(call); + Boolean recursive = call.getBoolean("recursive", false); File fileObject = getFileObject(path, directory); @@ -364,6 +365,11 @@ public void rmdir(PluginCall call) { return; } + if (fileObject.isDirectory() && fileObject.listFiles().length != 0 && !recursive) { + call.error("Directory is not empty"); + return; + } + boolean deleted = false; try { From 46d72e2b9694961006a9983ba29549cc4f76b94a Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 12:33:37 +0100 Subject: [PATCH 09/15] Electron support for non-recursive rmdir --- electron/src/electron/filesystem.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/electron/src/electron/filesystem.ts b/electron/src/electron/filesystem.ts index 4f3d4304f..53d39fefb 100644 --- a/electron/src/electron/filesystem.ts +++ b/electron/src/electron/filesystem.ts @@ -119,7 +119,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu } rmdir(options: RmdirOptions): Promise { - let {path, directory} = options; + let {path, directory, recursive} = options; if (Object.keys(this.fileLocations).indexOf(directory) === -1) return Promise.reject(`${directory} is currently not supported in the Electron implementation.`); @@ -129,6 +129,10 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu if (stat.type === 'directory') { return this.readdir({path, directory}) .then((readDirResult) => { + if (readDirResult.files.length !== 0 && !recursive) { + return Promise.reject(`${path} is not empty.`); + } + if (!readDirResult.files.length) { return new Promise((resolve, reject) => { let lookupPath = this.fileLocations[directory] + path; From 755c9855abeaf7c81d7cdae5edab11b747282a2a Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 12:44:44 +0100 Subject: [PATCH 10/15] Add a hint that the recursive option exists --- site/docs-md/apis/filesystem/index.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/site/docs-md/apis/filesystem/index.md b/site/docs-md/apis/filesystem/index.md index 41e7905a8..5318f7fa2 100644 --- a/site/docs-md/apis/filesystem/index.md +++ b/site/docs-md/apis/filesystem/index.md @@ -83,7 +83,8 @@ async rmdir() { try { let ret = await Filesystem.rmdir({ path: 'secrets', - directory: FilesystemDirectory.Documents + directory: FilesystemDirectory.Documents, + recursive: false, }); } catch(e) { console.error('Unable to remove directory', e); From 8e83436dff9a3e3982ecafbf12c727741621f391 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 2 Aug 2019 13:56:49 +0100 Subject: [PATCH 11/15] Back out whitespace changes --- .../com/getcapacitor/plugin/Filesystem.java | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java index 99e481936..ef2532f19 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -51,7 +51,7 @@ private Charset getEncoding(String encoding) { return null; } - switch (encoding) { + switch(encoding) { case "utf8": return StandardCharsets.UTF_8; case "utf16": @@ -64,7 +64,7 @@ private Charset getEncoding(String encoding) { private File getDirectory(String directory) { Context c = bridge.getContext(); - switch (directory) { + switch(directory) { case "APPLICATION": return c.getFilesDir(); case "DOCUMENTS": @@ -94,7 +94,7 @@ private File getFileObject(String path, String directory) { if (androidDirectory == null) { return null; } else { - if (!androidDirectory.exists()) { + if(!androidDirectory.exists()) { androidDirectory.mkdir(); } } @@ -158,32 +158,32 @@ public void readFile(PluginCall call) { String encoding = call.getString("encoding"); Charset charset = this.getEncoding(encoding); - if (encoding != null && charset == null) { + if(encoding != null && charset == null) { call.error("Unsupported encoding provided: " + encoding); return; } if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_READ_FILE_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { - try { - InputStream is = getInputStream(file, directory); - String dataStr; - if (charset != null) { - dataStr = readFileAsString(is, charset.name()); - } else { - dataStr = readFileAsBase64EncodedData(is); - } + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_READ_FILE_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { + try { + InputStream is = getInputStream(file, directory); + String dataStr; + if (charset != null) { + dataStr = readFileAsString(is, charset.name()); + } else { + dataStr = readFileAsBase64EncodedData(is); + } - JSObject ret = new JSObject(); - ret.putOpt("data", dataStr); - call.success(ret); - } catch (FileNotFoundException ex) { - call.error("File does not exist", ex); - } catch (IOException ex) { - call.error("Unable to read file", ex); - } catch (JSONException ex) { - call.error("Unable to return value for reading file", ex); - } + JSObject ret = new JSObject(); + ret.putOpt("data", dataStr); + call.success(ret); + } catch (FileNotFoundException ex) { + call.error("File does not exist", ex); + } catch (IOException ex) { + call.error("Unable to read file", ex); + } catch(JSONException ex) { + call.error("Unable to return value for reading file", ex); + } } } @@ -265,7 +265,7 @@ private void saveFile(PluginCall call, File file, String data) { } } else { //remove header from dataURL - if (data.indexOf(",") != -1) { + if(data.indexOf(",") != -1) { data = data.split(",")[1]; } try (FileOutputStream fos = new FileOutputStream(file, append)) { @@ -279,7 +279,7 @@ private void saveFile(PluginCall call, File file, String data) { if (success) { // update mediaStore index only if file was written to external storage if (isPublicDirectory(getDirectoryParameter(call))) { - MediaScannerConnection.scanFile(getContext(), new String[]{file.getAbsolutePath()}, null, null); + MediaScannerConnection.scanFile(getContext(), new String[] {file.getAbsolutePath()}, null, null); } Log.d(getLogTag(), "File '" + file.getAbsolutePath() + "' saved!"); call.success(); @@ -306,14 +306,14 @@ public void deleteFile(PluginCall call) { File fileObject = getFileObject(file, directory); if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_DELETE_FILE_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_DELETE_FILE_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { if (!fileObject.exists()) { call.error("File does not exist"); return; } boolean deleted = fileObject.delete(); - if (deleted == false) { + if(deleted == false) { call.error("Unable to delete file"); } else { call.success(); @@ -336,14 +336,14 @@ public void mkdir(PluginCall call) { } if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_WRITE_FOLDER_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_WRITE_FOLDER_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { boolean created = false; if (intermediate) { created = fileObject.mkdirs(); } else { created = fileObject.mkdir(); } - if (created == false) { + if(created == false) { call.error("Unable to create directory, unknown reason"); } else { call.success(); @@ -361,7 +361,7 @@ public void rmdir(PluginCall call) { File fileObject = getFileObject(path, directory); if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_DELETE_FOLDER_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_DELETE_FOLDER_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { if (!fileObject.exists()) { call.error("Directory does not exist"); return; @@ -380,7 +380,7 @@ public void rmdir(PluginCall call) { } catch (IOException ignored) { } - if (deleted == false) { + if(deleted == false) { call.error("Unable to delete directory, unknown reason"); } else { call.success(); @@ -396,8 +396,8 @@ public void readdir(PluginCall call) { File fileObject = getFileObject(path, directory); - if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_READ_FOLDER_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { + if (!isPublicDirectory(directory) + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_READ_FOLDER_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { if (fileObject != null && fileObject.exists()) { String[] files = fileObject.list(); @@ -405,7 +405,7 @@ public void readdir(PluginCall call) { ret.put("files", JSArray.from(files)); call.success(ret); } else { - call.error("Directory does not exist"); + call.error("Directory does not exist"); } } } @@ -419,7 +419,7 @@ public void getUri(PluginCall call) { File fileObject = getFileObject(path, directory); if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_URI_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_URI_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { JSObject data = new JSObject(); data.put("uri", Uri.fromFile(fileObject).toString()); call.success(data); @@ -435,7 +435,7 @@ public void stat(PluginCall call) { File fileObject = getFileObject(path, directory); if (!isPublicDirectory(directory) - || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_STAT_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { + || isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_STAT_PERMISSIONS, Manifest.permission.READ_EXTERNAL_STORAGE)) { if (!fileObject.exists()) { call.error("File does not exist"); return; @@ -597,16 +597,16 @@ public void copy(PluginCall call) { /** * Checks the the given permission and requests them if they are not already granted. * @param permissionRequestCode the request code see {@link PluginRequestCodes} - * @param permission the permission string + * @param permission the permission string * @return Returns true if the permission is granted and false if it is denied. */ private boolean isStoragePermissionGranted(int permissionRequestCode, String permission) { if (hasPermission(permission)) { - Log.v(getLogTag(), "Permission '" + permission + "' is granted"); + Log.v(getLogTag(),"Permission '" + permission + "' is granted"); return true; } else { - Log.v(getLogTag(), "Permission '" + permission + "' denied. Asking user for it."); - pluginRequestPermissions(new String[]{permission}, permissionRequestCode); + Log.v(getLogTag(),"Permission '" + permission + "' denied. Asking user for it."); + pluginRequestPermissions(new String[] {permission}, permissionRequestCode); return false; } } @@ -631,10 +631,10 @@ private boolean isPublicDirectory(String directory) { protected void handleRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.handleRequestPermissionsResult(requestCode, permissions, grantResults); - Log.d(getLogTag(), "handling request perms result"); + Log.d(getLogTag(),"handling request perms result"); if (getSavedCall() == null) { - Log.d(getLogTag(), "No stored plugin call for permissions request result"); + Log.d(getLogTag(),"No stored plugin call for permissions request result"); return; } @@ -643,7 +643,7 @@ protected void handleRequestPermissionsResult(int requestCode, String[] permissi for (int i = 0; i < grantResults.length; i++) { int result = grantResults[i]; String perm = permissions[i]; - if (result == PackageManager.PERMISSION_DENIED) { + if(result == PackageManager.PERMISSION_DENIED) { Log.d(getLogTag(), "User denied storage permission: " + perm); savedCall.error(PERMISSION_DENIED_ERROR); this.freeSavedCall(); From 11ead328093c6109f4f66c83ef264b249317aa09 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 2 Aug 2019 17:41:45 +0200 Subject: [PATCH 12/15] call handleError on catch --- ios/Capacitor/Capacitor/Plugins/Filesystem.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift index 3791f7c8a..89721d560 100644 --- a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift +++ b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift @@ -261,7 +261,9 @@ public class CAPFilesystemPlugin : CAPPlugin { handleError(call, "Folder is not empty") return } - } catch {} + } catch { + handleError(call, error.localizedDescription, error) + } do { try FileManager.default.removeItem(at: fileUrl) From caca2bfbcf2f868f383237415c5d16523c27af5f Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 2 Aug 2019 17:47:15 +0200 Subject: [PATCH 13/15] Update web rmdir doc --- core/src/web/filesystem.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index eb002675d..955cb0fe4 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -274,7 +274,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { } /** - * Recursively remove a directory and all its contents + * Remove a directory * @param options the options for the directory remove */ async rmdir(options: RmdirOptions): Promise { From f6e74caf00f05f6c6ebb1bb6a8973526f7a4a845 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 2 Aug 2019 18:26:08 +0200 Subject: [PATCH 14/15] Pass recursive option to rmdir --- core/src/web/filesystem.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index 955cb0fe4..a3fcb3112 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -300,7 +300,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { if (entryObj.type === 'file') { await this.deleteFile({path: entryPath, directory}); } else { - await this.rmdir({path: entryPath, directory}); + await this.rmdir({path: entryPath, directory, recursive}); } } From 8a86fc1c263ae1cfc332744b67ac81c3a267d19d Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 2 Aug 2019 18:39:10 +0200 Subject: [PATCH 15/15] Add recursive option to recursive rmdir in electron --- electron/src/electron/filesystem.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/electron/src/electron/filesystem.ts b/electron/src/electron/filesystem.ts index 7f874274f..70c08c0a0 100644 --- a/electron/src/electron/filesystem.ts +++ b/electron/src/electron/filesystem.ts @@ -149,10 +149,10 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu }); } else { return Promise.all(readDirResult.files.map((f) => { - return this.rmdir({path: this.Path.join(path, f), directory}); + return this.rmdir({path: this.Path.join(path, f), directory, recursive}); })) .then(() => { - return this.rmdir({path, directory}); + return this.rmdir({path, directory, recursive}); }); } });