Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Filesystem): make writeFile return the file uri #2484

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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