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
});