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): Return path of copied file #931

Merged
merged 3 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -364,8 +364,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