Skip to content

Commit e1a00bd

Browse files
authored
feat(Filesystem): make writeFile return the file uri (#2484)
1 parent 2ec6cf5 commit e1a00bd

File tree

7 files changed

+17
-9
lines changed

7 files changed

+17
-9
lines changed

android/capacitor/src/main/java/com/getcapacitor/plugin/Filesystem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ private void saveFile(PluginCall call, File file, String data) {
282282
MediaScannerConnection.scanFile(getContext(), new String[] {file.getAbsolutePath()}, null, null);
283283
}
284284
Log.d(getLogTag(), "File '" + file.getAbsolutePath() + "' saved!");
285-
call.success();
285+
JSObject result = new JSObject();
286+
result.put("uri", Uri.fromFile(file).toString());
287+
call.success(result);
286288
} else {
287289
call.error("FILE_NOTCREATED");
288290
}

core/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/core-plugin-definitions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ export interface FileReadResult {
754754
export interface FileDeleteResult {
755755
}
756756
export interface FileWriteResult {
757+
uri: string;
757758
}
758759
export interface FileAppendResult {
759760
}

core/src/web/filesystem.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
176176
content: !encoding && data.indexOf(',') >= 0 ? data.split(',')[1] : data,
177177
};
178178
await this.dbRequest('put', [pathObj]);
179-
return {};
179+
return {
180+
uri: pathObj.path
181+
};
180182
}
181183

182184
/**

electron/src/electron/filesystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu
7171
return;
7272
}
7373

74-
resolve();
74+
resolve({uri: lookupPath});
7575
});
7676
});
7777
}

example/src/pages/filesystem/filesystem.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ export class FilesystemPage {
2727
console.log('ionViewDidLoad FilesystemPage');
2828
}
2929

30-
fileWrite() {
30+
async fileWrite() {
3131
try {
32-
Plugins.Filesystem.writeFile({
32+
const result = await Plugins.Filesystem.writeFile({
3333
path: 'secrets/text.txt',
3434
data: "This is a test",
3535
directory: FilesystemDirectory.Documents,
3636
encoding: FilesystemEncoding.UTF8
3737
});
38+
console.log('Wrote file', result);
3839
} catch(e) {
3940
console.error('Unable to write file (press mkdir first, silly)', e);
4041
}
41-
console.log('Wrote file');
4242
}
4343

4444
async fileRead() {
@@ -131,12 +131,13 @@ export class FilesystemPage {
131131

132132
async directoryTest() {
133133
try {
134-
await Plugins.Filesystem.writeFile({
134+
const result = await Plugins.Filesystem.writeFile({
135135
path: 'text.txt',
136136
data: "This is a test",
137137
directory: FilesystemDirectory.Data,
138138
encoding: FilesystemEncoding.UTF8
139139
});
140+
console.log('wrote file', result);
140141
let stat = await Plugins.Filesystem.stat({
141142
path: 'text.txt',
142143
directory: FilesystemDirectory.Data

ios/Capacitor/Capacitor/Plugins/Filesystem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public class CAPFilesystemPlugin : CAPPlugin {
115115
return
116116
}
117117
}
118-
call.success()
118+
call.success([
119+
"uri": fileUrl.absoluteString
120+
])
119121
} catch let error as NSError {
120122
handleError(call, error.localizedDescription, error)
121123
}

0 commit comments

Comments
 (0)