feat: add downloadBlob method

This commit is contained in:
Derrick Hammer 2024-02-13 00:27:16 -05:00
parent 3c9022d8c7
commit 853db73634
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 38 additions and 0 deletions

View File

@ -157,3 +157,41 @@ export async function downloadProof(
): Promise<ArrayBuffer> {
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<ArrayBuffer> {
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<ArrayBuffer>(
{
url: `/s5/blob/${cid}`,
method: "GET",
responseType: "arraybuffer",
},
config,
);
}