Use config files

This commit is contained in:
2025-06-07 09:43:09 +02:00
parent 84d9b56fce
commit 3b6f5e7c1f
4 changed files with 31 additions and 28 deletions
+11 -14
View File
@@ -14,6 +14,7 @@ const router = express.Router();
function errorPageRenderer(res, emoji, message) {
res.render("error", {
page_title: app_conf.name,
program: app_conf.program,
css_path: path.join(app_conf.paths.css.web, "stylesheet.css"),
js_path: path.join(app_conf.paths.js.web, "error.js"),
error_emoji: emoji,
@@ -24,7 +25,7 @@ function errorPageRenderer(res, emoji, message) {
router.get("/disable", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
fs.writeFileSync(map_conf.selected_map_path, "");
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "");
res.status(200).send();
});
@@ -35,7 +36,7 @@ router.get("/enable", async (req, res) => {
var requested_map = req.query["element"];
if (requested_map && map.mapExists(requested_map)) {
fs.writeFileSync(map_conf.selected_map_path, requested_map);
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), requested_map);
res.status(200).send();
} else {
@@ -48,7 +49,7 @@ router.delete("/remove", async (req, res) => {
if (req.query["element"]) {
try {
fs.unlinkSync(`app/storage/${req.query["element"]}`);
fs.unlinkSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, req.query["element"]));
res.status(200).send();
} catch (error) {
res.status(500).send();
@@ -67,12 +68,7 @@ router.post("/upload", async (req, res) => {
} else {
const uploaded_file = req.files.file;
let map_name = path.parse(uploaded_file["name"])["name"];
if (map.mapExists(uploaded_file["name"])) {
map_name += `_${uuid.v4()}`;
}
fs.copyFile(uploaded_file.tempFilePath, `app/storage/${map_name}.csv`, (error) => {
fs.copyFile(uploaded_file.tempFilePath, path.join(path.dirname(require.main.filename), map_conf.map_storage_path, `${map_conf.prefix}_${uuid.v4()}.${map_conf.extension}`), (error) => {
if (error) {
logger.error(error);
res.status(500).send();
@@ -87,7 +83,7 @@ router.get("/download", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
if (req.query["element"]) {
res.download(`app/storage/${req.query["element"]}`);
res.download(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, req.query["element"]));
} else {
res.status(400).send();
}
@@ -97,7 +93,7 @@ router.post("/add", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
if (req.body) {
await map.addMap(`map_${uuid.v4()}.csv`, req.body["meta"], req.body["throttle"], req.body["motor"]);
await map.addMap(`${map_conf.prefix}_${uuid.v4()}.${map_conf.extension}`, req.body["meta"], req.body["throttle"], req.body["motor"]);
res.status(200).send();
} else {
@@ -175,12 +171,12 @@ router.get("/", async (req, res) => {
const maps = await map.getAvailableMaps();
var selected_map_name = "";
if (fs.existsSync(map_conf.selected_map_path)) {
selected_map_name = fs.readFileSync(map_conf.selected_map_path, "utf-8");
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
}
var table_data = "";
if (Object.keys(maps).length) {
var table_data = "";
for (m of Object.keys(maps)) {
table_data += `<tr><td>${m}</td>`;
if ("name" in maps[m]) {
@@ -225,6 +221,7 @@ router.get("/", async (req, res) => {
router.get("/about", (req, res) => {
res.render("about", {
page_title: app_conf.name,
program: app_conf.program,
css_path: path.join(app_conf.paths.css.web, "stylesheet.css"),
version: app_conf.version
});
+11 -11
View File
@@ -33,7 +33,7 @@ async function addMap(filename, meta, throttle, motor) {
try {
await csv.writeToString(map).then(data => total += data);
fs.writeFileSync(`app/storage/${filename}`, total);
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename), total);
} catch (error) {
throw new Error(error);
}
@@ -45,10 +45,10 @@ async function updateMap(filename, meta, throttle, motor) {
try {
await csv.writeToString(map).then(data => total += data);
fs.writeFileSync(`app/storage/${filename}`, total);
fs.renameSync(`app/storage/${filename}`, `app/storage/map_${uuid.v4()}.csv`)
if (fs.existsSync("app/storage/selected")) {
fs.unlinkSync("app/storage/selected");
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename), total);
fs.renameSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename), path.join(path.dirname(require.main.filename), map_conf.map_storage_path, `${map_conf.prefix}_${uuid.v4()}.${map_conf.extension}`));
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
fs.unlinkSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"));
}
} catch (error) {
throw new Error(error);
@@ -109,7 +109,7 @@ function mapExists(filename) {
if (path.extname(filename) != ".csv") {
return false;
}
return fs.existsSync(`app/storage/${filename}`);
return fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename));
}
// mode: comments 1 or data 0
@@ -118,12 +118,12 @@ function parseMap(filename, mode) {
return new Promise((resolve, reject) => {
if (mode) {
csv.parseFile(`app/storage/${filename}`)
csv.parseFile(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename))
.on('error', error => reject(error))
.on('data', row => row[0][0] === "#" ? result.push(row[0].slice(1)) : resolve(result))
.on('end', () => resolve(result));
} else {
csv.parseFile(`app/storage/${filename}`, { comment: "#" })
csv.parseFile(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, filename), { comment: "#" })
.on('error', error => reject(error))
.on('data', row => result.push(row))
.on('end', () => resolve(result));
@@ -155,10 +155,10 @@ async function getAvailableMaps() {
var available_maps = {};
try {
var files = fs.readdirSync("app/storage");
var files = fs.readdirSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path));
for (const file of files) {
if (path.extname(file) == ".csv") {
if (path.extname(file) == `.${map_conf.extension}`) {
available_maps[file] = {};
try {
@@ -177,7 +177,7 @@ async function getAvailableMaps() {
} catch (error) {
if (error.code === "ENOENT") {
logger.info("No storage directory found. Creating directory.");
fs.mkdirSync("app/storage");
fs.mkdirSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path));
}
}
+4 -1
View File
@@ -17,7 +17,10 @@
<body>
<div class="control_div" style="display: flex; flex-direction: row; justify-content: space-between;">
<label style="font-size: 200%;">CRTelemetry SQLite3</label>
<label style="font-size: 200%;">
<%= page_title%>
<%= program %>
</label>
</div>
<div class="main_div">
<center>
+5 -2
View File
@@ -17,9 +17,12 @@
</head>
<body>
<input type="file" id="file_input" accept=".db" style="display: none;" />
<input type="file" id="file_input" accept=".csv" style="display: none;" />
<div class="control_div" style="display: flex; flex-direction: row; justify-content: space-between;">
<label style="font-size: 200%;">CRTelemetry SQLite3</label>
<label style="font-size: 200%;">
<%= page_title%>
<%= program %>
</label>
<div style="display: flex;align-items: center;">
<label id="add" style="cursor: pointer; margin-right: 10px;"><span>&#43; </span>Add</label>
<label id="upload" style="cursor: pointer; margin-left: 10px; margin-right: 10px;"><span>&#129093;