Skip to content

Commit

Permalink
incrementally write to file in downloadURL
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Mar 1, 2024
1 parent 4d783e7 commit acc6615
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,21 @@ export async function downloadURL(url: string, saveLocation: string) {
return `Failed to download <${url}>\n${response.status} ${response.statusText}`;
}

const buffer = await response.arrayBuffer().catch(console.error);
if (!response.body) return "Failed to extract contents from response";

if (!buffer) return "Failed to extract contents from response";
await Bun.write(absSaveLocation, buffer);
try {
const writer = Bun.file(absSaveLocation).writer();

for await (const chunk of response.body) {
writer.write(chunk as Uint8Array);
}

await writer.flush();
await writer.end();
} catch (err) {
console.error(err);
return `Failed to write to file`;
}
}

/**
Expand Down

0 comments on commit acc6615

Please sign in to comment.