Added handling to non existent paths

This commit is contained in:
YourFriendlyNeighborhoodDealer 2018-12-21 10:33:35 +01:00
parent f2dbd98f71
commit c10a5a8a6c
2 changed files with 91 additions and 71 deletions

View file

@ -39,7 +39,7 @@ function Load() {
var prevData = utils.ReadFile(statFile);
data = JSON.parse(prevData);
} catch (e) {
logger.Log("[STAT]: Error at loading logs!", logger.GetColor("redbg"));
logger.Log("[STAT]: Error at loading logs! (@ first run its normal)", logger.GetColor("redbg"));
console.log(e);
}
@ -47,7 +47,7 @@ function Load() {
var prevVData = utils.ReadFile(vStatFile);
vData = JSON.parse(prevVData);
} catch (e) {
logger.Log("[STAT]: Error at loading visit logs!", logger.GetColor("redbg"));
logger.Log("[STAT]: Error at loading visit logs! (@ first run its normal)", logger.GetColor("redbg"));
console.log(e);
}
}
@ -84,7 +84,7 @@ function AddVisitStat(name) {
("0" + (m.getMonth() + 1)).slice(-2) + "/" +
("0" + m.getDate()).slice(-2);
if (vData[now] == undefined)
vData[now] = {};
vData[now] = [];
if (vData[now][name] == undefined)
vData[now][name] = 0;
vData[now][name]++;

View file

@ -12,20 +12,40 @@ var fs = require('fs');
var logger = require('./logger.js');
const recievedFile = "stats/recieved";
const publicFile = "public/data/public";
const staticFile = "public/data/static";
const manFile = "public/man.html";
const logFile = "stats/logs";
const dataFile = "public/data.json";
function ReadFile(name) {
if (!fs.existsSync(name))
throw "No such file: " + name;
return fs.readFileSync(name, "utf8");
}
function CreatePath(path) {
if (fs.existsSync(path))
return;
var p = path.split("/");
var currDir = p[0];
for (var i = 1; i < p.length; i++) {
console.log(currDir);
if (!fs.existsSync(currDir)) {
fs.mkdirSync(currDir);
}
currDir += "/" + p[i];
}
fs.writeFileSync(path, "");
}
function WriteFile(content, path) {
CreatePath(path);
fs.writeFileSync(path, content);
}
function WriteFileAsync(content, path) {
CreatePath(path);
fs.writeFile(path, content, function(err) {
if (err) {
logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
@ -34,6 +54,7 @@ function WriteFileAsync(content, path) {
}
function AppendToFile(data, file) {
CreatePath(file);
fs.appendFile(file, "\n" + data, function(err) {
if (err)
logger.Log("[ERR ]: Error writing log file: " + file + " (sync)", logger.GetColor("redbg"));
@ -57,12 +78,11 @@ function WriteBackup() {
logger.Log("[ERR ]: Error backing up recieved file!", logger.GetColor("redbg"));
console.log(e);
}
try {
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString());
WriteFileAsync(ReadFile(publicFile), 'public/backs/public_' + new Date().toString());
//logger.Log('[SAVE]: Public questions backup wrote');
} catch (e) {
logger.Log("[ERR ]: Error backing up data json file!", logger.GetColor("redbg"));
logger.Log("[ERR ]: Error backing up public file!", logger.GetColor("redbg"), true);
console.log(e);
}
}