From e13fa12484f9c68c543552858e90e7c03eb9d795 Mon Sep 17 00:00:00 2001 From: Tracy <31976538+tracy-codes@users.noreply.github.com> Date: Fri, 12 Jul 2024 10:37:29 -0700 Subject: [PATCH] feat: add optional overwrite param to upload methods adds new overwrite param to function signatures uploadFile and uploadMultipleFiles --- src/methods/upload-file.ts | 7 ++++++- src/methods/upload-multiple-files.ts | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/methods/upload-file.ts b/src/methods/upload-file.ts index 704923f..2c20594 100644 --- a/src/methods/upload-file.ts +++ b/src/methods/upload-file.ts @@ -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 { let fileErrors = []; let fileBuffer: Buffer; @@ -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)); } diff --git a/src/methods/upload-multiple-files.ts b/src/methods/upload-multiple-files.ts index c955edc..9903fdd 100644 --- a/src/methods/upload-multiple-files.ts +++ b/src/methods/upload-multiple-files.ts @@ -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. */ @@ -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 { let fileData: Array = []; const fileErrors: Array = []; @@ -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`, {