Skip to content

Commit

Permalink
Feat: Copy object (#30)
Browse files Browse the repository at this point in the history
* Add "copy" method

* Reword description of "move" method
  • Loading branch information
haydn committed Feb 13, 2022
1 parent 904d1b9 commit 1993d19
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/lib/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ export class StorageFileApi {
}

/**
* Moves an existing file, optionally renaming it at the same time.
* Moves an existing file.
*
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-new.png`.
*/
async move(
fromPath: string,
Expand All @@ -184,6 +184,29 @@ export class StorageFileApi {
}
}

/**
* Copies an existing file.
*
* @param fromPath The original file path, including the current file name. For example `folder/image.png`.
* @param toPath The new file path, including the new file name. For example `folder/image-copy.png`.
*/
async copy(
fromPath: string,
toPath: string
): Promise<{ data: { message: string } | null; error: Error | null }> {
try {
const data = await post(
this.fetch,
`${this.url}/object/copy`,
{ bucketId: this.bucketId, sourceKey: fromPath, destinationKey: toPath },
{ headers: this.headers }
)
return { data, error: null }
} catch (error) {
return { data: null, error }
}
}

/**
* Create signed url to download file without requiring permissions. This URL can be valid for a set number of seconds.
*
Expand Down Expand Up @@ -238,7 +261,9 @@ export class StorageFileApi {
*
* @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
*/
getPublicUrl(path: string): {
getPublicUrl(
path: string
): {
data: { publicURL: string } | null
error: Error | null
publicURL: string | null
Expand Down

0 comments on commit 1993d19

Please sign in to comment.