Program parallel downloader
This commit is contained in:
parent
60d6e4c0eb
commit
6764bb6e07
@ -1,6 +1,7 @@
|
|||||||
const path = require("node:path");
|
const path = require("node:path");
|
||||||
const fs = require("node:fs");
|
const fs = require("node:fs");
|
||||||
const crypto = require("node:crypto");
|
const crypto = require("node:crypto");
|
||||||
|
const https = require("node:https");
|
||||||
|
|
||||||
function checkFileAgainstSHA1(baseDirectory, fileEntry) {
|
function checkFileAgainstSHA1(baseDirectory, fileEntry) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
@ -32,8 +33,68 @@ function checkFileAgainstSHA1(baseDirectory, fileEntry) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
function downloadFile(url, filePath) {
|
||||||
"checkFiles": function (manifest) {
|
return new Promise(function (resolve, reject) {
|
||||||
|
let fileOutputStream = fs.createWriteStream(filePath);
|
||||||
|
|
||||||
|
let request = https.request(url, {
|
||||||
|
"method": "GET"
|
||||||
|
}, function (response) {
|
||||||
|
if (response.statusCode !== 200) return reject(new Error("Got a non-OK response while trying to download a file."));
|
||||||
|
|
||||||
|
fileOutputStream.on("end", function () {
|
||||||
|
resolve(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
response.pipe(fileOutputStream);
|
||||||
|
});
|
||||||
|
|
||||||
|
request.on("error", function (error) {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
request.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function processDownloadQueue(baseDirectory, fileEntries) {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
let runningDownloads = 0;
|
||||||
|
|
||||||
|
for (let d = 0; d < module.exports.concurrentDownloads; d++) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
"concurrentDownloads": 8,
|
||||||
|
"checkFiles": function (manifest, baseDirectory) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
let checkTasks = [];
|
||||||
|
|
||||||
|
for (let f = 0; f < manifest.files.length; f++) {
|
||||||
|
let fileEntry = manifest.files[f];
|
||||||
|
|
||||||
|
checkTasks.push(checkFileAgainstSHA1(baseDirectory, fileEntry));
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.all(checkTasks).then(function (results) {
|
||||||
|
resolve(results);
|
||||||
|
}).catch(function (error) {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
"downloadFiles": function (baseDirectory, fileEntries) {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
processDownloadQueue(baseDirectory, fileEntries.filter(function (entry) {
|
||||||
|
return !entry.ok;
|
||||||
|
})).then(function () {
|
||||||
|
resolve(true);
|
||||||
|
}).catch(function (error) {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user