diff --git a/filesystem/README.md b/filesystem/README.md index aaea339f9..5261aafbc 100644 --- a/filesystem/README.md +++ b/filesystem/README.md @@ -282,7 +282,7 @@ Rename a file or directory ### copy(...) ```typescript -copy(options: CopyOptions) => Promise +copy(options: CopyOptions) => Promise ``` Copy a file or directory @@ -291,6 +291,8 @@ Copy a file or directory | ------------- | --------------------------------------------------- | | **`options`** | CopyOptions | +**Returns:** Promise<CopyResult> + **Since:** 1.0.0 -------------------- @@ -462,6 +464,13 @@ Required on Android, only when using `Directory.Documents`< | **`toDirectory`** | Directory | The `Directory` containing the destination file or directory. If not supplied will use the 'directory' parameter as the destination | 1.0.0 | +#### CopyResult + +| Prop | Type | Description | Since | +| --------- | ------------------- | -------------------------------------- | ----- | +| **`uri`** | string | The uri where the file was copied into | 4.0.0 | + + #### PermissionStatus | Prop | Type | diff --git a/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java b/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java index 1757269af..dbac2a9f9 100644 --- a/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java +++ b/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/Filesystem.java @@ -83,7 +83,7 @@ public String[] readdir(String path, String directory) throws DirectoryNotFoundE return files; } - public boolean copy(String from, String directory, String to, String toDirectory, boolean doRename) + public File copy(String from, String directory, String to, String toDirectory, boolean doRename) throws IOException, CopyFailedException { if (toDirectory == null) { toDirectory = directory; @@ -100,7 +100,7 @@ public boolean copy(String from, String directory, String to, String toDirectory } if (toObject.equals(fromObject)) { - return true; + return toObject; } if (!fromObject.exists()) { @@ -130,7 +130,7 @@ public boolean copy(String from, String directory, String to, String toDirectory copyRecursively(fromObject, toObject); } - return true; + return toObject; } public InputStream getInputStream(String path, String directory) throws IOException { diff --git a/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java b/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java index f3e8c3cbe..115ca8320 100644 --- a/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java +++ b/filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java @@ -366,8 +366,14 @@ private void _copy(PluginCall call, Boolean doRename) { } } try { - implementation.copy(from, directory, to, toDirectory, doRename); - call.resolve(); + File file = implementation.copy(from, directory, to, toDirectory, doRename); + if (!doRename) { + JSObject result = new JSObject(); + result.put("uri", Uri.fromFile(file).toString()); + call.resolve(result); + } else { + call.resolve(); + } } catch (CopyFailedException ex) { call.reject(ex.getMessage()); } catch (IOException ex) { diff --git a/filesystem/ios/Plugin/FilesystemPlugin.swift b/filesystem/ios/Plugin/FilesystemPlugin.swift index 167a7b6db..69372ccf4 100644 --- a/filesystem/ios/Plugin/FilesystemPlugin.swift +++ b/filesystem/ios/Plugin/FilesystemPlugin.swift @@ -307,7 +307,9 @@ public class FilesystemPlugin: CAPPlugin { } do { try implementation.copy(at: fromUrl, to: toUrl) - call.resolve() + call.resolve([ + "uri": toUrl.absoluteString + ]) } catch let error as NSError { handleError(call, error.localizedDescription, error) } diff --git a/filesystem/src/definitions.ts b/filesystem/src/definitions.ts index 2c53f4a0e..e657f8bf0 100644 --- a/filesystem/src/definitions.ts +++ b/filesystem/src/definitions.ts @@ -422,6 +422,15 @@ export interface StatResult { uri: string; } +export interface CopyResult { + /** + * The uri where the file was copied into + * + * @since 4.0.0 + */ + uri: string; +} + export interface FilesystemPlugin { /** * Read a file from disk @@ -498,7 +507,7 @@ export interface FilesystemPlugin { * * @since 1.0.0 */ - copy(options: CopyOptions): Promise; + copy(options: CopyOptions): Promise; /** * Check read/write permissions. diff --git a/filesystem/src/web.ts b/filesystem/src/web.ts index ec32109cd..21a9df145 100644 --- a/filesystem/src/web.ts +++ b/filesystem/src/web.ts @@ -3,6 +3,7 @@ import { WebPlugin } from '@capacitor/core'; import type { AppendFileOptions, CopyOptions, + CopyResult, DeleteFileOptions, FilesystemPlugin, GetUriOptions, @@ -414,7 +415,8 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { * @return a promise that resolves with the rename result */ async rename(options: RenameOptions): Promise { - return this._copy(options, true); + await this._copy(options, true); + return; } /** @@ -422,7 +424,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { * @param options the options for the copy operation * @return a promise that resolves with the copy result */ - async copy(options: CopyOptions): Promise { + async copy(options: CopyOptions): Promise { return this._copy(options, false); } @@ -440,7 +442,10 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { * @param doRename whether to perform a rename or copy operation * @return a promise that resolves with the result */ - private async _copy(options: CopyOptions, doRename = false): Promise { + private async _copy( + options: CopyOptions, + doRename = false, + ): Promise { let { toDirectory } = options; const { to, from, directory: fromDirectory } = options; @@ -458,7 +463,9 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { // Test that the "to" and "from" locations are different if (fromPath === toPath) { - return; + return { + uri: toPath, + }; } if (isPathParent(fromPath, toPath)) { @@ -531,7 +538,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { } // Write the file to the new location - await this.writeFile({ + const writeResult = await this.writeFile({ path: to, directory: toDirectory, data: file.data, @@ -543,7 +550,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { } // Resolve promise - return; + return writeResult; } case 'directory': { if (toObj) { @@ -596,6 +603,9 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin { } } } + return { + uri: toPath, + }; } }