mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Added handling to non existent paths
This commit is contained in:
parent
f2dbd98f71
commit
c10a5a8a6c
2 changed files with 91 additions and 71 deletions
6
stat.js
6
stat.js
|
@ -39,7 +39,7 @@ function Load() {
|
||||||
var prevData = utils.ReadFile(statFile);
|
var prevData = utils.ReadFile(statFile);
|
||||||
data = JSON.parse(prevData);
|
data = JSON.parse(prevData);
|
||||||
} catch (e) {
|
} 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);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ function Load() {
|
||||||
var prevVData = utils.ReadFile(vStatFile);
|
var prevVData = utils.ReadFile(vStatFile);
|
||||||
vData = JSON.parse(prevVData);
|
vData = JSON.parse(prevVData);
|
||||||
} catch (e) {
|
} 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);
|
console.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ function AddVisitStat(name) {
|
||||||
("0" + (m.getMonth() + 1)).slice(-2) + "/" +
|
("0" + (m.getMonth() + 1)).slice(-2) + "/" +
|
||||||
("0" + m.getDate()).slice(-2);
|
("0" + m.getDate()).slice(-2);
|
||||||
if (vData[now] == undefined)
|
if (vData[now] == undefined)
|
||||||
vData[now] = {};
|
vData[now] = [];
|
||||||
if (vData[now][name] == undefined)
|
if (vData[now][name] == undefined)
|
||||||
vData[now][name] = 0;
|
vData[now][name] = 0;
|
||||||
vData[now][name]++;
|
vData[now][name]++;
|
||||||
|
|
30
utils.js
30
utils.js
|
@ -12,20 +12,40 @@ var fs = require('fs');
|
||||||
var logger = require('./logger.js');
|
var logger = require('./logger.js');
|
||||||
|
|
||||||
const recievedFile = "stats/recieved";
|
const recievedFile = "stats/recieved";
|
||||||
|
const publicFile = "public/data/public";
|
||||||
const staticFile = "public/data/static";
|
const staticFile = "public/data/static";
|
||||||
const manFile = "public/man.html";
|
const manFile = "public/man.html";
|
||||||
const logFile = "stats/logs";
|
const logFile = "stats/logs";
|
||||||
const dataFile = "public/data.json";
|
|
||||||
|
|
||||||
function ReadFile(name) {
|
function ReadFile(name) {
|
||||||
|
if (!fs.existsSync(name))
|
||||||
|
throw "No such file: " + name;
|
||||||
return fs.readFileSync(name, "utf8");
|
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) {
|
function WriteFile(content, path) {
|
||||||
|
CreatePath(path);
|
||||||
fs.writeFileSync(path, content);
|
fs.writeFileSync(path, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
function WriteFileAsync(content, path) {
|
function WriteFileAsync(content, path) {
|
||||||
|
CreatePath(path);
|
||||||
fs.writeFile(path, content, function(err) {
|
fs.writeFile(path, content, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
|
logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
|
||||||
|
@ -34,6 +54,7 @@ function WriteFileAsync(content, path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function AppendToFile(data, file) {
|
function AppendToFile(data, file) {
|
||||||
|
CreatePath(file);
|
||||||
fs.appendFile(file, "\n" + data, function(err) {
|
fs.appendFile(file, "\n" + data, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
logger.Log("[ERR ]: Error writing log file: " + file + " (sync)", logger.GetColor("redbg"));
|
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"));
|
logger.Log("[ERR ]: Error backing up recieved file!", logger.GetColor("redbg"));
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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) {
|
} 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);
|
console.log(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue