Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Improve checking and fix issue where pre-existing files weren't returned
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoTheThird committed Sep 27, 2019
1 parent 12cf39e commit 9d76ebf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 67 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "system-image-node-module",
"version": "1.0.8",
"version": "1.0.9",
"description": "UBports System image client/server module",
"main": "./src/module.js",
"scripts": {
Expand Down
50 changes: 28 additions & 22 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ class Client {
}
}
}, 1000);
common.checkFiles(urls).then((_urls) => {
Promise.all(_urls.map((file) => {
return new Promise(function(resolve, reject) {
Promise.all(urls.map((file) => {
return new Promise(function(resolve, reject) {
common.checksumFile(file).then(() => {
next(++filesDownloaded, urls.length);
resolve();
return;
}).catch(() => {
download(file.url, file.path).on("response", (res) => {
var totalSize = eval(res.headers['content-length']);
overallSize += totalSize;
Expand All @@ -112,33 +116,35 @@ class Client {
});
}).then(() => {
common.checksumFile(file).then(() => {
next(++filesDownloaded, _urls.length);
next(++filesDownloaded, urls.length);
resolve();
return;
}).catch((err) => {
reject(err);
return;
});
});
});
})).then(() => {
var files = _this.getFilePushArray(urls);
files.push({
src: _this.createInstallCommandsFile(
_this.createInstallCommands(
latest.files,
options.installerCheck,
options.wipe,
options.enable
),
options.device
});
})).then(() => {
var files = _this.getFilePushArray(urls);
files.push({
src: _this.createInstallCommandsFile(
_this.createInstallCommands(
latest.files,
options.installerCheck,
options.wipe,
options.enable
),
dest: ubuntuPushDir + ubuntuCommandFile
});
resolve(files);
return;
}).catch((err) => {
reject(err);
return;
options.device
),
dest: ubuntuPushDir + ubuntuCommandFile
});
resolve(files);
return;
}).catch((err) => {
reject(err);
return;
});
});
});
Expand Down
59 changes: 16 additions & 43 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,30 @@ function getRandomInt(min, max) {

function checksumFile(file) {
return new Promise(function(resolve, reject) {
if (!file.checksum) {
// No checksum so return true;
resolve();
return;
} else {
checksum.file(path.join(file.path, path.basename(file.url)), {
algorithm: "sha256"
}, function(err, sum) {
console.log("checked: " +path.basename(file.url), sum === file.checksum);
if (sum === file.checksum) resolve()
else reject()
});
}
});
}

function checkFiles(urls) {
return new Promise(function(resolve, reject) {
var urls_ = [];
var next = () => {
if (urls.length <= 1) {
resolve(urls_)
fs.access(path.join(file.path, path.basename(file.url)), (err) => {
if (err) {
reject();
} else {
urls.shift();
check()
}
}
var check = () => {
fs.access(path.join(urls[0].path, path.basename(urls[0].url)), (err) => {
if (err) {
console.log("Not existing " + path.join(urls[0].path, path.basename(urls[0].url)));
urls_.push(urls[0]);
next();
if (!file.checksum) {
// No checksum so return true;
resolve();
return;
} else {
checksumFile(urls[0]).then(() => {
console.log(path.join(urls[0].path, path.basename(urls[0].url)) + " exists with the expected checksum, so the download will be skipped.");
next();
}).catch(() => {
console.log("Checksum mismatch on " + path.join(urls[0].path, path.basename(urls[0].url)) + ". This file will be downloaded again.");
urls_.push(urls[0]);
next();
checksum.file(path.join(file.path, path.basename(file.url)), {
algorithm: "sha256"
}, function(err, sum) {
console.log("checked: " +path.basename(file.url), sum === file.checksum);
if (sum === file.checksum) resolve()
else reject()
});
}
})
}
check();
}
});
});
}


module.exports = {
getRandomInt: getRandomInt,
checkFiles: checkFiles,
checksumFile: checksumFile
};

0 comments on commit 9d76ebf

Please sign in to comment.