mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
utils.js refactor
This commit is contained in:
parent
d4cd019f29
commit
e5fd0aba13
1 changed files with 79 additions and 90 deletions
169
utils/utils.js
169
utils/utils.js
|
@ -1,109 +1,98 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ReadFile: ReadFile,
|
ReadFile: ReadFile,
|
||||||
WriteFile: WriteFile,
|
WriteFile: WriteFile,
|
||||||
writeFileAsync: WriteFileAsync,
|
writeFileAsync: WriteFileAsync,
|
||||||
AppendToFile: AppendToFile,
|
AppendToFile: AppendToFile,
|
||||||
Beep: Beep,
|
Beep: Beep,
|
||||||
WriteBackup: WriteBackup,
|
WriteBackup: WriteBackup,
|
||||||
FileExists: FileExists,
|
FileExists: FileExists,
|
||||||
CreatePath: CreatePath,
|
CreatePath: CreatePath,
|
||||||
GetAllFilesFromFolder: GetAllFilesFromFolder
|
GetAllFilesFromFolder: GetAllFilesFromFolder
|
||||||
};
|
|
||||||
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
var logger = require('./logger.js');
|
|
||||||
|
|
||||||
const recievedFile = "stats/recieved";
|
|
||||||
const manFile = "public/man.html";
|
|
||||||
const logFile = "stats/logs";
|
|
||||||
const dataFile = "public/data.json";
|
|
||||||
|
|
||||||
function ReadFile(name) {
|
|
||||||
if (!FileExists(name))
|
|
||||||
throw "No such file: " + name;
|
|
||||||
return fs.readFileSync(name, "utf8");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function FileExists(path) {
|
var fs = require('fs')
|
||||||
return fs.existsSync(path);
|
|
||||||
|
var logger = require('../utils/logger.js')
|
||||||
|
|
||||||
|
const recievedFile = '../stats/recieved'
|
||||||
|
const manFile = '../public/man.html'
|
||||||
|
const logFile = '../stats/logs'
|
||||||
|
const dataFile = '../public/data.json'
|
||||||
|
|
||||||
|
function ReadFile (name) {
|
||||||
|
if (!FileExists(name)) { throw 'No such file: ' + name }
|
||||||
|
return fs.readFileSync(name, 'utf8')
|
||||||
}
|
}
|
||||||
|
|
||||||
function CreatePath(path, onlyPath) {
|
function FileExists (path) {
|
||||||
if (FileExists(path))
|
return fs.existsSync(path)
|
||||||
return;
|
|
||||||
|
|
||||||
var p = path.split("/");
|
|
||||||
var currDir = p[0];
|
|
||||||
for (var i = 1; i < p.length; i++) {
|
|
||||||
if (currDir != "" && !fs.existsSync(currDir)) {
|
|
||||||
try {
|
|
||||||
fs.mkdirSync(currDir);
|
|
||||||
} catch (e) { console.log("Failed to make " + currDir + " directory... "); }
|
|
||||||
}
|
|
||||||
currDir += "/" + p[i];
|
|
||||||
}
|
|
||||||
if (onlyPath == undefined || onlyPath == false)
|
|
||||||
fs.writeFileSync(path, "");
|
|
||||||
else
|
|
||||||
fs.mkdirSync(path);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function WriteFile(content, path) {
|
function CreatePath (path, onlyPath) {
|
||||||
CreatePath(path);
|
if (FileExists(path)) { return }
|
||||||
fs.writeFileSync(path, content);
|
|
||||||
|
var p = path.split('/')
|
||||||
|
var currDir = p[0]
|
||||||
|
for (var i = 1; i < p.length; i++) {
|
||||||
|
if (currDir != '' && !fs.existsSync(currDir)) {
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(currDir)
|
||||||
|
} catch (e) { console.log('Failed to make ' + currDir + ' directory... ') }
|
||||||
|
}
|
||||||
|
currDir += '/' + p[i]
|
||||||
|
}
|
||||||
|
if (onlyPath == undefined || onlyPath == false) { fs.writeFileSync(path, '') } else { fs.mkdirSync(path) }
|
||||||
}
|
}
|
||||||
|
|
||||||
function WriteFileAsync(content, path) {
|
function WriteFile (content, path) {
|
||||||
CreatePath(path);
|
CreatePath(path)
|
||||||
fs.writeFile(path, content, function(err) {
|
fs.writeFileSync(path, content)
|
||||||
if (err) {
|
|
||||||
logger.Log("Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function AppendToFile(data, file) {
|
function WriteFileAsync (content, path) {
|
||||||
CreatePath(file);
|
CreatePath(path)
|
||||||
fs.appendFile(file, "\n" + data, function(err) {
|
fs.writeFile(path, content, function (err) {
|
||||||
if (err)
|
if (err) {
|
||||||
logger.Log("Error writing log file: " + file + " (sync)", logger.GetColor("redbg"));
|
logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg'))
|
||||||
});
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function Beep() {
|
function AppendToFile (data, file) {
|
||||||
try {
|
CreatePath(file)
|
||||||
process.stdout.write('\x07');
|
fs.appendFile(file, '\n' + data, function (err) {
|
||||||
} catch (e) {
|
if (err) { logger.Log('Error writing log file: ' + file + ' (sync)', logger.GetColor('redbg')) }
|
||||||
console.log("error beepin");
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function WriteBackup() {
|
function Beep () {
|
||||||
try {
|
try {
|
||||||
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString());
|
process.stdout.write('\x07')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log("Error backing up data json file!", logger.GetColor("redbg"));
|
console.log('error beepin')
|
||||||
console.log(e);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function GetAllFilesFromFolder(dir) {
|
function WriteBackup () {
|
||||||
|
try {
|
||||||
var results = [];
|
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString())
|
||||||
|
} catch (e) {
|
||||||
fs.readdirSync(dir).forEach(function(file) {
|
logger.Log('Error backing up data json file!', logger.GetColor('redbg'))
|
||||||
|
console.log(e)
|
||||||
file = dir + '/' + file;
|
}
|
||||||
var stat = fs.statSync(file);
|
|
||||||
|
|
||||||
if (stat && stat.isDirectory()) {
|
|
||||||
results = results.concat(_getAllFilesFromFolder(file));
|
|
||||||
} else results.push(file);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function GetAllFilesFromFolder (dir) {
|
||||||
|
var results = []
|
||||||
|
|
||||||
|
fs.readdirSync(dir).forEach(function (file) {
|
||||||
|
file = dir + '/' + file
|
||||||
|
var stat = fs.statSync(file)
|
||||||
|
|
||||||
|
if (stat && stat.isDirectory()) {
|
||||||
|
results = results.concat(_getAllFilesFromFolder(file))
|
||||||
|
} else results.push(file)
|
||||||
|
})
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue