Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions download.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mkdirpromise(destination)
function downloadAllFiles(files, to) {
let prms = Promise.resolve();
files.forEach(function (file) {
prms = prms.then(() => downloadWithProgress(folderUrl + file.path, path.join(to, file.path)));
prms = prms.then(() => downloadWithProgress(folderUrl + file.path, path.join(to, file.path), file.md5));
});
return prms;
}
Expand All @@ -51,4 +51,4 @@ function mkdirpromise(destination) {
resolve();
});
});
}
}
21 changes: 15 additions & 6 deletions src/helper/downloadWithProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const url = require('url'),
mkdirp = require('mkdirp'),
path = require('path'),
http = require('http');
md5sum = require('./md5');

module.exports = function mkdirAndDownload(fileUrl, destination) {
return mkdirpromise(path.dirname(destination)).then(() => download(fileUrl, destination));
module.exports = function mkdirAndDownload(fileUrl, destination, md5correct) {
return mkdirpromise(path.dirname(destination)).then(() => download(fileUrl, destination, md5correct));
};

function download(fileUrl, destination) {
function download(fileUrl, destination, md5correct) {
return new Promise((resolve, reject) => {

const p = url.parse(fileUrl);
Expand All @@ -20,19 +21,27 @@ function download(fileUrl, destination) {
console.log('Starting to download ' + destination + ', with a total of ' + len + ' bytes');
let downloaded = 0;
let downloadedPerc = '0.0';

md5 = new md5sum.ArrayBuffer();
res.on('data', function (chunk) {
file.write(chunk);
downloaded += chunk.length;
let newDlPerc = '' + (100.0 * downloaded / len).toFixed(1);

md5.append(chunk);
if (newDlPerc !== downloadedPerc) {
process.stdout.write(destination + ": " + newDlPerc + "%\n");
}
downloadedPerc = newDlPerc;
}).on('end', function () {
file.end();
md5val = md5.end()
console.log('md5 = '+md5val);
console.log('md5 correct = '+md5correct);
console.log('downloaded to: ' + destination);
if (md5val !== md5correct) {
console.log('Error! MD5 does not match.');
console.log(md5val);
console.log(md5correct);
}
resolve(destination);
}).on('error', function (err) {
reject(err);
Expand All @@ -51,4 +60,4 @@ function mkdirpromise(destination) {
resolve();
});
});
}
}
Loading