Skip to content

Commit

Permalink
feat(Filesystem): make writeFile return the file uri (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Feb 25, 2020
1 parent 2ec6cf5 commit e1a00bd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ export interface FileReadResult {
export interface FileDeleteResult {
}
export interface FileWriteResult {
uri: string;
}
export interface FileAppendResult {
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/web/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion electron/src/electron/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu
return;
}

resolve();
resolve({uri: lookupPath});
});
});
}
Expand Down
9 changes: 5 additions & 4 deletions example/src/pages/filesystem/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion ios/Capacitor/Capacitor/Plugins/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e1a00bd

Please sign in to comment.