mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-17 15:53:29 +02:00
feat: implement downloadImageToFile for streaming downloads with axios
This commit is contained in:
23
src/utils/downloadImageToFile.js
Normal file
23
src/utils/downloadImageToFile.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user