Skip to content

Commit

Permalink
feat(filesystem): Return path of copied file (#931)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Apr 26, 2022
1 parent 54ef85e commit 310f583
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
11 changes: 10 additions & 1 deletion filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Rename a file or directory
### copy(...)

```typescript
copy(options: CopyOptions) => Promise<void>
copy(options: CopyOptions) => Promise<CopyResult>
```

Copy a file or directory
Expand All @@ -291,6 +291,8 @@ Copy a file or directory
| ------------- | --------------------------------------------------- |
| **`options`** | <code><a href="#copyoptions">CopyOptions</a></code> |

**Returns:** <code>Promise&lt;<a href="#copyresult">CopyResult</a>&gt;</code>

**Since:** 1.0.0

--------------------
Expand Down Expand Up @@ -462,6 +464,13 @@ Required on Android, only when using <a href="#directory">`Directory.Documents`<
| **`toDirectory`** | <code><a href="#directory">Directory</a></code> | The <a href="#directory">`Directory`</a> 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`** | <code>string</code> | The uri where the file was copied into | 4.0.0 |


#### PermissionStatus

| Prop | Type |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion filesystem/ios/Plugin/FilesystemPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 10 additions & 1 deletion filesystem/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -498,7 +507,7 @@ export interface FilesystemPlugin {
*
* @since 1.0.0
*/
copy(options: CopyOptions): Promise<void>;
copy(options: CopyOptions): Promise<CopyResult>;

/**
* Check read/write permissions.
Expand Down
22 changes: 16 additions & 6 deletions filesystem/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WebPlugin } from '@capacitor/core';
import type {
AppendFileOptions,
CopyOptions,
CopyResult,
DeleteFileOptions,
FilesystemPlugin,
GetUriOptions,
Expand Down Expand Up @@ -414,15 +415,16 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
* @return a promise that resolves with the rename result
*/
async rename(options: RenameOptions): Promise<void> {
return this._copy(options, true);
await this._copy(options, true);
return;
}

/**
* Copy a file or directory
* @param options the options for the copy operation
* @return a promise that resolves with the copy result
*/
async copy(options: CopyOptions): Promise<void> {
async copy(options: CopyOptions): Promise<CopyResult> {
return this._copy(options, false);
}

Expand All @@ -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<void> {
private async _copy(
options: CopyOptions,
doRename = false,
): Promise<CopyResult> {
let { toDirectory } = options;
const { to, from, directory: fromDirectory } = options;

Expand All @@ -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)) {
Expand Down Expand Up @@ -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,
Expand All @@ -543,7 +550,7 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
}

// Resolve promise
return;
return writeResult;
}
case 'directory': {
if (toObj) {
Expand Down Expand Up @@ -596,6 +603,9 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
}
}
}
return {
uri: toPath,
};
}
}

Expand Down

0 comments on commit 310f583

Please sign in to comment.