From 853db73634f4f87dbf32a45745bee6a9bc49ab45 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 13 Feb 2024 00:27:16 -0500 Subject: [PATCH] feat: add downloadBlob method --- src/methods/download.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/methods/download.ts b/src/methods/download.ts index c5755c5..bc68182 100644 --- a/src/methods/download.ts +++ b/src/methods/download.ts @@ -157,3 +157,41 @@ export async function downloadProof( ): Promise { return this.downloadData(`${cid}.obao`, customOptions); } + +/** + * Downloads a blob from the given cid. This will capture a 301 redirect to the actual blob location, then download the blob. + * @param this - S5Client + * @param cid - 46-character cid, or a valid cid URL. + * @param [customOptions] - Additional settings that can optionally be set. + * @returns - The data + */ +export async function downloadBlob( + this: S5Client, + cid: string, + customOptions: CustomDownloadOptions = {}, +): Promise { + const config = optionsToConfig(this, DEFAULT_DOWNLOAD_OPTIONS, customOptions); + + let location: string | null = null; + + await getS5BlobCid(cid, { + ...config, + responseType: "arraybuffer", + beforeRedirect: (config, responseDetails) => { + location = responseDetails.headers["location"]; + }, + }); + + if (!location) { + throw new Error("Failed to download blob"); + } + + return await customInstance( + { + url: `/s5/blob/${cid}`, + method: "GET", + responseType: "arraybuffer", + }, + config, + ); +}