diff --git a/src/utils/downloadImageToFile.js b/src/utils/downloadImageToFile.js new file mode 100644 index 0000000..9dcf454 --- /dev/null +++ b/src/utils/downloadImageToFile.js @@ -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; + } +}