Initial commit for implemeenting wget and refactoring some Server-related code

This commit is contained in:
danielyxie
2018-09-27 16:49:23 -05:00
parent 62672a4e2a
commit 427cdb48ba
4 changed files with 105 additions and 138 deletions
+28
View File
@@ -2261,6 +2261,34 @@ function NetscriptFunctions(workerScript) {
yesNoBoxCreate(txt);
});
},
wget : async function(url, target, ip=workerScript.serverIp) {
if (!isScriptFilename(target) && !target.endsWith(".txt")) {
workerSript.log(`ERROR: wget() failed because of an invalid target file: ${target}. Target file must be a script or text file`);
return false;
}
var s = safeGetServer(ip, "wget");
$.get(url, function(data) {
let res;
if (isScriptFilename(target)) {
res = s.writeToScriptFile(target, data);
} else {
res = s.writeToTextFile(target, data);
}
if (!res.success) {
workerScript.log("ERROR: wget() failed");
return false;
}
if (res.overwritten) {
workerScript.log(`wget() successfully retrieved content and overwrote ${target} on ${ip}`);
return true;
}
workerScript.log(`wget successfully retrieved content to new file ${target} on ${ip}`);
return true;
}, 'text').fail(function(e) {
workerScript.log("ERROR: wget() failed: " + JSON.stringify(e));
return false;
});
},
getFavorToDonate: function() {
if (workerScript.checkingRam) {
return updateStaticRam("getFavorToDonate", CONSTANTS.ScriptGetFavorToDonate);