Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created March 6, 2019 23:37
Show Gist options
  • Save senthilmpro/5c4418e47b9a70d97522a1f99ce8b0ff to your computer and use it in GitHub Desktop.
Save senthilmpro/5c4418e47b9a70d97522a1f99ce8b0ff to your computer and use it in GitHub Desktop.
Node.js downloader.js
var fs = require('fs');
var axios = require('axios');
const async = require('async');
const path = require('path');
const DL_INTERVAL_TIMER = 500; //in milliseconds.
module.exports = {
createFolder: function (dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, 0766, function (err) {
console.log(err);
});
}
},
downloadFiles: function (arr, FOLDER_LOCATION) {
if (!arr) {
return;
}
if (arr && arr.length < 1) {
console.log("EMPTY ARRAY ", arr);
return;
} else {
this.createFolder(FOLDER_LOCATION);
async.eachLimit(arr, 1, (reqUrl, next) => {
this.downloadFile(reqUrl, FOLDER_LOCATION).then(() => {
setTimeout(function () {
next();
}, DL_INTERVAL_TIMER);
})
});
}
},
downloadFile: async function (requestUrl, FOLDER_LOCATION) {
if (!requestUrl) {
console.log("url not found");
return;
}
var response = null;
try {
response = await axios({
method: 'GET',
url: requestUrl,
responseType: 'stream'
});
} catch (ex) {
return new Promise((resolve, reject) => { resolve() });
}
var fileName = this.getFilenameFromHeaders(response) || "default-" + (new Date().getTime()) + ".jpg";
var fullPath = path.resolve(FOLDER_LOCATION, fileName);
response.data.pipe(fs.createWriteStream(fullPath));
return new Promise((resolve, reject) => {
response.data.on('end', () => {
console.log('done : ' + requestUrl);
resolve()
})
response.data.on('error', () => {
reject()
})
});
},
getFilenameFromHeaders: function (resp) {
var regexp = /filename=\"(.*)\"/gi;
var str = resp.headers['content-disposition'];
if (!str) {
return resp.config.url.split("/").pop();
}
return regexp.exec(resp.headers['content-disposition'])[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment