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): add recursive option to writeFile #2487

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 @@ -192,6 +192,7 @@ public void writeFile(PluginCall call) {
saveCall(call);
String path = call.getString("path");
String data = call.getString("data");
Boolean recursive = call.getBoolean("recursive", false);

if (path == null) {
Log.e(getLogTag(), "No path or filename retrieved from call");
Expand All @@ -215,8 +216,10 @@ public void writeFile(PluginCall call) {
if (androidDir.exists() || androidDir.mkdirs()) {
// path might include directories as well
File fileObject = new File(androidDir, path);
if (fileObject.getParentFile().exists() || fileObject.getParentFile().mkdirs()) {
if (fileObject.getParentFile().exists() || (recursive && fileObject.getParentFile().mkdirs())) {
saveFile(call, fileObject, data);
} else {
call.error("Parent folder doesn't exist");
}
} else {
Log.e(getLogTag(), "Not able to create '" + directory + "'!");
Expand All @@ -235,8 +238,10 @@ public void writeFile(PluginCall call) {
// do not know where the file is being store so checking the permission to be secure
// TODO to prevent permission checking we need a property from the call
if (isStoragePermissionGranted(PluginRequestCodes.FILESYSTEM_REQUEST_WRITE_FILE_PERMISSIONS, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (fileObject.getParentFile().exists() || fileObject.getParentFile().mkdirs()) {
if (fileObject.getParentFile().exists() || (recursive && fileObject.getParentFile().mkdirs())) {
saveFile(call, fileObject, data);
} else {
call.error("Parent folder doesn't exist");
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ export interface FileWriteOptions {
* Pass FilesystemEncoding.UTF8 to write data as string
*/
encoding?: FilesystemEncoding;
/**
* Whether to create any missing parent directories.
* Defaults to false
*/
recursive?: boolean;
}

export interface FileAppendOptions {
Expand Down
3 changes: 2 additions & 1 deletion core/src/web/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
async writeFile(options: FileWriteOptions): Promise<FileWriteResult> {
const path: string = this.getPath(options.directory, options.path);
const data = options.data;
const doRecursive = options.recursive;

let occupiedEntry = await this.dbRequest('get', [path]) as EntryObj;
if (occupiedEntry && occupiedEntry.type === 'directory')
Expand All @@ -162,7 +163,7 @@ export class FilesystemPluginWeb extends WebPlugin implements FilesystemPlugin {
const subDirIndex = parentPath.indexOf('/', 1);
if (subDirIndex !== -1) {
const parentArgPath = parentPath.substr(subDirIndex);
await this.mkdir({path: parentArgPath, directory: options.directory, recursive: true});
await this.mkdir({path: parentArgPath, directory: options.directory, recursive: doRecursive});
}
}
const now = Date.now();
Expand Down
20 changes: 14 additions & 6 deletions electron/src/electron/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ export class FilesystemPluginElectron extends WebPlugin implements FilesystemPlu
const base64Data = options.data.indexOf(',') >= 0 ? options.data.split(',')[1] : options.data;
data = Buffer.from(base64Data, 'base64');
}
this.NodeFS.writeFile(lookupPath, data, options.encoding || 'binary', (err: any) => {
if (err) {
reject(err);
return;
const dstDirectory = this.Path.dirname(lookupPath);
this.NodeFS.stat(dstDirectory, (err: any) => {
if(err) {
const doRecursive = options.recursive;
if (doRecursive) {
this.NodeFS.mkdirSync(dstDirectory, {recursive: doRecursive});
}
}

resolve({uri: lookupPath});
this.NodeFS.writeFile(lookupPath, data, options.encoding || 'binary', (err: any) => {
if (err) {
reject(err);
return;
}
resolve({uri: lookupPath});
});
});
});
}
Expand Down
9 changes: 9 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class CAPFilesystemPlugin : CAPPlugin {
*/
@objc func writeFile(_ call: CAPPluginCall) {
let encoding = call.getString("encoding")
let recursive = call.get("recursive", Bool.self, false)!
// TODO: Allow them to switch encoding
guard let file = call.get("path", String.self) else {
handleError(call, "path must be provided and must be a string.")
Expand All @@ -104,6 +105,14 @@ public class CAPFilesystemPlugin : CAPPlugin {
}

do {
if !FileManager.default.fileExists(atPath: fileUrl.deletingLastPathComponent().absoluteString) {
if recursive {
try FileManager.default.createDirectory(at: fileUrl.deletingLastPathComponent(), withIntermediateDirectories: recursive, attributes: nil)
} else {
handleError(call, "Parent folder doesn't exist");
return
}
}
if encoding != nil {
try data.write(to: fileUrl, atomically: false, encoding: .utf8)
} else {
Expand Down