feat: implement downloadImageToFile for streaming downloads with axios

This commit is contained in:
xtrullor73
2024-05-25 00:41:40 -07:00
parent 68ae0c105b
commit b3fd524e36

View File

@@ -0,0 +1,23 @@
import axios from 'axios';
import fs from 'fs';
// This function needs to be something like downloadImageToFile
export default async function downloadImageToFile(url, downloadPath) {
try {
const response = await axios.get(url, {
responseType: 'stream',
});
const writer = fs.createWriteStream(downloadPath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', () => resolve(downloadPath));
writer.on('error', reject);
});
} catch (e) {
console.error('Error downloading the image:', e);
throw e;
}
}