From e1a00bdfc6cf53b9e2669783a14fe99950bc0a2f Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Tue, 25 Feb 2020 12:15:08 +0100 Subject: [PATCH] feat(Filesystem): make writeFile return the file uri (#2484) --- .../main/java/com/getcapacitor/plugin/Filesystem.java | 4 +++- core/package-lock.json | 2 +- core/src/core-plugin-definitions.ts | 1 + core/src/web/filesystem.ts | 4 +++- electron/src/electron/filesystem.ts | 2 +- example/src/pages/filesystem/filesystem.ts | 9 +++++---- ios/Capacitor/Capacitor/Plugins/Filesystem.swift | 4 +++- 7 files changed, 17 insertions(+), 9 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 90d12b0c2d..d6e5ab253c 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java @@ -282,7 +282,9 @@ private void saveFile(PluginCall call, File file, String data) { MediaScannerConnection.scanFile(getContext(), new String[] {file.getAbsolutePath()}, null, null); } Log.d(getLogTag(), "File '" + file.getAbsolutePath() + "' saved!"); - call.success(); + JSObject result = new JSObject(); + result.put("uri", Uri.fromFile(file).toString()); + call.success(result); } else { call.error("FILE_NOTCREATED"); } diff --git a/core/package-lock.json b/core/package-lock.json index 59c4f28017..d070792ad1 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -1,6 +1,6 @@ { "name": "@capacitor/core", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 00070343fd..7da5febccf 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -754,6 +754,7 @@ export interface FileReadResult { export interface FileDeleteResult { } export interface FileWriteResult { + uri: string; } export interface FileAppendResult { } diff --git a/core/src/web/filesystem.ts b/core/src/web/filesystem.ts index 0e2232cf01..72d9cd346c 100644 --- a/core/src/web/filesystem.ts +++ b/core/src/web/filesystem.ts @@ -176,7 +176,9 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin { content: !encoding && data.indexOf(',') >= 0 ? data.split(',')[1] : data, }; await this.dbRequest('put', [pathObj]); - return {}; + return { + uri: pathObj.path + }; } /** diff --git a/electron/src/electron/filesystem.ts b/electron/src/electron/filesystem.ts index 5cfff5e6d9..014b8f748e 100644 --- a/electron/src/electron/filesystem.ts +++ b/electron/src/electron/filesystem.ts @@ -71,7 +71,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu return; } - resolve(); + resolve({uri: lookupPath}); }); }); } diff --git a/example/src/pages/filesystem/filesystem.ts b/example/src/pages/filesystem/filesystem.ts index 71196f0b2f..b7e256f108 100644 --- a/example/src/pages/filesystem/filesystem.ts +++ b/example/src/pages/filesystem/filesystem.ts @@ -27,18 +27,18 @@ export class FilesystemPage { console.log('ionViewDidLoad FilesystemPage'); } - fileWrite() { + async fileWrite() { try { - Plugins.Filesystem.writeFile({ + const result = await Plugins.Filesystem.writeFile({ path: 'secrets/text.txt', data: "This is a test", directory: FilesystemDirectory.Documents, encoding: FilesystemEncoding.UTF8 }); + console.log('Wrote file', result); } catch(e) { console.error('Unable to write file (press mkdir first, silly)', e); } - console.log('Wrote file'); } async fileRead() { @@ -131,12 +131,13 @@ export class FilesystemPage { async directoryTest() { try { - await Plugins.Filesystem.writeFile({ + const result = await Plugins.Filesystem.writeFile({ path: 'text.txt', data: "This is a test", directory: FilesystemDirectory.Data, encoding: FilesystemEncoding.UTF8 }); + console.log('wrote file', result); let stat = await Plugins.Filesystem.stat({ path: 'text.txt', directory: FilesystemDirectory.Data diff --git a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift index 889c1e9ae2..d8bc56a2dc 100644 --- a/ios/Capacitor/Capacitor/Plugins/Filesystem.swift +++ b/ios/Capacitor/Capacitor/Plugins/Filesystem.swift @@ -115,7 +115,9 @@ public class CAPFilesystemPlugin : CAPPlugin { return } } - call.success() + call.success([ + "uri": fileUrl.absoluteString + ]) } catch let error as NSError { handleError(call, error.localizedDescription, error) }