You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.1 KiB
47 lines
1.1 KiB
'use strict'
|
|
|
|
var http = require('http');
|
|
var fs = require('fs');
|
|
var Promise = require('es6-promise').Promise;
|
|
|
|
module.exports = {
|
|
|
|
downloadedCount: 0,
|
|
|
|
downloadedSize: 0,
|
|
|
|
/**
|
|
*
|
|
*/
|
|
mkdir: function(path) {
|
|
try {
|
|
fs.mkdirSync(path);
|
|
} catch(e) {
|
|
if (e.code !== 'EEXIST'){
|
|
console.log(e);
|
|
}
|
|
}
|
|
},
|
|
|
|
/**
|
|
*
|
|
*/
|
|
download: function(url, filename) {
|
|
return new Promise(function(resolve, reject) {
|
|
var file = fs.createWriteStream(filename);
|
|
|
|
http.get(url, function(response) {
|
|
var pipe = response.pipe(file);
|
|
|
|
pipe.on('finish', function() {
|
|
module.exports.downloadedSize += pipe.bytesWritten;
|
|
module.exports.downloadedCount++;
|
|
|
|
console.log(url + ' --> ' + filename + ' --> ' + pipe.bytesWritten + ' bytes | ' + module.exports.downloadedSize + ' bytes total | ' + module.exports.downloadedCount + ' file(s)');
|
|
|
|
resolve(pipe.bytesWritten);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
};
|
|
|