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);
|
||||
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]++;
|
||||
|
|
156
utils.js
156
utils.js
|
@ -1,68 +1,88 @@
|
|||
module.exports = {
|
||||
ReadFile: ReadFile,
|
||||
WriteFile: WriteFile,
|
||||
writeFileAsync: WriteFileAsync,
|
||||
AppendToFile: AppendToFile,
|
||||
Beep: Beep,
|
||||
WriteBackup: WriteBackup
|
||||
};
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var logger = require('./logger.js');
|
||||
|
||||
const recievedFile = "stats/recieved";
|
||||
const staticFile = "public/data/static";
|
||||
const manFile = "public/man.html";
|
||||
const logFile = "stats/logs";
|
||||
const dataFile = "public/data.json";
|
||||
|
||||
function ReadFile(name) {
|
||||
return fs.readFileSync(name, "utf8");
|
||||
}
|
||||
|
||||
function WriteFile(content, path) {
|
||||
fs.writeFileSync(path, content);
|
||||
}
|
||||
|
||||
function WriteFileAsync(content, path) {
|
||||
fs.writeFile(path, content, function(err) {
|
||||
if (err) {
|
||||
logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function AppendToFile(data, file) {
|
||||
fs.appendFile(file, "\n" + data, function(err) {
|
||||
if (err)
|
||||
logger.Log("[ERR ]: Error writing log file: " + file + " (sync)", logger.GetColor("redbg"));
|
||||
});
|
||||
}
|
||||
|
||||
function Beep() {
|
||||
try {
|
||||
process.stdout.write('\x07');
|
||||
} catch (e) {
|
||||
console.log("error beepin");
|
||||
}
|
||||
}
|
||||
|
||||
function WriteBackup() {
|
||||
try {
|
||||
WriteFileAsync(ReadFile(recievedFile), 'public/backs/recieved_' + new Date().toString());
|
||||
//logger.Log('[SAVE]: New questions backup wrote');
|
||||
|
||||
} catch (e) {
|
||||
logger.Log("[ERR ]: Error backing up recieved file!", logger.GetColor("redbg"));
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
try {
|
||||
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString());
|
||||
|
||||
} catch (e) {
|
||||
logger.Log("[ERR ]: Error backing up data json file!", logger.GetColor("redbg"));
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
module.exports = {
|
||||
ReadFile: ReadFile,
|
||||
WriteFile: WriteFile,
|
||||
writeFileAsync: WriteFileAsync,
|
||||
AppendToFile: AppendToFile,
|
||||
Beep: Beep,
|
||||
WriteBackup: WriteBackup
|
||||
};
|
||||
|
||||
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";
|
||||
|
||||
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"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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"));
|
||||
});
|
||||
}
|
||||
|
||||
function Beep() {
|
||||
try {
|
||||
process.stdout.write('\x07');
|
||||
} catch (e) {
|
||||
console.log("error beepin");
|
||||
}
|
||||
}
|
||||
|
||||
function WriteBackup() {
|
||||
try {
|
||||
WriteFileAsync(ReadFile(recievedFile), 'public/backs/recieved_' + new Date().toString());
|
||||
//logger.Log('[SAVE]: New questions backup wrote');
|
||||
|
||||
} catch (e) {
|
||||
logger.Log("[ERR ]: Error backing up recieved file!", logger.GetColor("redbg"));
|
||||
console.log(e);
|
||||
}
|
||||
try {
|
||||
WriteFileAsync(ReadFile(publicFile), 'public/backs/public_' + new Date().toString());
|
||||
//logger.Log('[SAVE]: Public questions backup wrote');
|
||||
} catch (e) {
|
||||
logger.Log("[ERR ]: Error backing up public file!", logger.GetColor("redbg"), true);
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue