20 lines
562 B
JavaScript
20 lines
562 B
JavaScript
const fs = require("fs");
|
|
const { Readable } = require('stream');
|
|
const { finished } = require('stream/promises');
|
|
const path = require("path");
|
|
|
|
|
|
/**
|
|
* Download a file at url to local disk filePath
|
|
* @param {String} url
|
|
* @param {String} filePath
|
|
*
|
|
* greetz https://stackoverflow.com/a/51302466/1004931
|
|
*/
|
|
const download = (async (url, filePath) => {
|
|
const res = await fetch(url);
|
|
const fileStream = fs.createWriteStream(filePath, { flags: 'wx' });
|
|
await finished(Readable.fromWeb(res.body).pipe(fileStream));
|
|
});
|
|
|
|
module.exports = download; |