Skip to content

Commit

Permalink
feat: add optional overwrite param to upload methods
Browse files Browse the repository at this point in the history
adds new overwrite param to function signatures uploadFile and uploadMultipleFiles
  • Loading branch information
tracy-codes committed Jul 12, 2024
1 parent ecad379 commit e13fa12
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/methods/upload-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import nacl from "tweetnacl";
*
* @param {web3.PublicKey} key - Publickey of Storage Account.
* @param {File | ShadowFile} data - File or ShadowFile object, file extensions should be included in the name property of ShadowFiles.
* @param {Boolean} overwrite - If true, overwrites if existing. Default is false.
* @returns {ShadowUploadResponse} File location and transaction signature.
*/
export default async function uploadFile(
key: web3.PublicKey,
data: File | ShadowFile
data: File | ShadowFile,
overwrite: Boolean = false
): Promise<ShadowUploadResponse> {
let fileErrors = [];
let fileBuffer: Buffer;
Expand Down Expand Up @@ -82,6 +84,9 @@ export default async function uploadFile(
form.append("message", encodedMsg);
form.append("storage_account", key.toString());
form.append("signer", this.wallet.publicKey.toString());
if(overwrite) {
form.append("overwrite", "true");
}
} catch (e) {
return Promise.reject(new Error(e.message));
}
Expand Down
7 changes: 6 additions & 1 deletion src/methods/upload-multiple-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface FileData {
* @param {web3.PublicKey} key - Storage account PublicKey to upload the files to.
* @param {FileList | ShadowFile[]} data[] - Array of Files or ShadowFile objects to be uploaded
* @param {Number} concurrent - Number of files to concurrently upload. Default: 3
* @param {Boolean} overwrite - If true, overwrites if existing. Default is false.
* @param {Function} callback - Callback function for every batch of files uploaded. A number will be passed into the callback like `callback(num)` indicating the number of files that were confirmed in that specific batch.
* @returns {ShadowBatchUploadResponse[]} - File names, locations and transaction signatures for uploaded files.
*/
Expand All @@ -30,7 +31,8 @@ export default async function uploadMultipleFiles(
key: web3.PublicKey,
data: FileList | ShadowFile[],
concurrent = 3,
callback?: Function
overwrite: Boolean = false,
callback?: Function,
): Promise<ShadowBatchUploadResponse[]> {
let fileData: Array<FileData> = [];
const fileErrors: Array<object> = [];
Expand Down Expand Up @@ -218,6 +220,9 @@ export default async function uploadMultipleFiles(
fd.append("storage_account", key.toString());
fd.append("signer", this.wallet.publicKey.toString());
fd.append("fileNames", allFileNames.toString());
if (overwrite) {
fd.append("overwrite", "true");
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 7200000);
const response = await fetch(`${SHDW_DRIVE_ENDPOINT}/upload`, {
Expand Down

0 comments on commit e13fa12

Please sign in to comment.