const logger = require("../utils/logger.util.js"); const path = require("path"); const fs = require("fs"); const uuid = require("uuid"); const express = require('express'); const app_conf = require("../config/app.conf.js"); const map_conf = require("../config/map.conf.js"); const messages = require("../config/messages.conf.js"); const map = require("../utils/map.util.js"); const router = express.Router(); function errorPageRenderer(res, emoji, message) { res.render("error", { page_title: app_conf.name, program: app_conf.program, app_url: app_conf.app_url, 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, error_message: message }) } router.get("/disable", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), ""); res.status(200).send(); }); router.get("/enable", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); var requested_map = req.query["element"]; if (requested_map && map.mapExists(requested_map)) { fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), requested_map); res.status(200).send(); } else { res.status(400).send(); } }); router.delete("/remove", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); if (req.query["element"]) { try { 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(); logger.error(error.message); } } else { res.status(400).send(); } }); router.post("/upload", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); if (!req.files || !req.files.file) { res.status(422).send(); } else { const uploaded_file = req.files.file; 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(); } else { res.status(201).send(); } }); } }); router.get("/download", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); if (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(); } }); router.post("/add", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); if (req.body) { 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 { res.status(400).send(); } }); router.post("/edit", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); if (req.query["element"] && req.body) { try { await map.updateMap(req.query["element"], req.body["meta"], req.body["throttle"], req.body["motor"]); res.status(200).send(); } catch (error) { res.status(500).send(); logger.error(error.message); } } else { res.status(400).send(); } }); router.get("/add", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); res.render("add", { page_title: app_conf.name, program: app_conf.program, app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "add.js"), chartjs: app_conf.paths.chartjs.web + "/chart.umd.js", steps: map_conf.steps, max_rpm: map_conf.max_rpm, max_throttle: map_conf.max_throttle }); }); router.get("/edit", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); const requested_map = req.query["element"]; if (requested_map) { try { if (map.mapExists(requested_map)) { res.render("edit", { page_title: app_conf.name, program: app_conf.program, app_url: app_conf.app_url, css_path: path.join(app_conf.paths.css.web, "stylesheet.css"), js_path: path.join(app_conf.paths.js.web, "edit.js"), chartjs: app_conf.paths.chartjs.web + "/chart.umd.js", map: JSON.stringify(await map.parseMapJson(requested_map, 0)), map_meta: JSON.stringify(await map.parseMapJson(requested_map, 1)), steps: map_conf.steps, max_rpm: map_conf.max_rpm, max_throttle: map_conf.max_throttle }); } else { errorPageRenderer(res, "🔍❌", messages.not_found); } } catch (error) { errorPageRenderer(res, "🌋💥", messages.internal_server_error); logger.error(error.message); } } else { errorPageRenderer(res, "✋⛔", messages.bad_request); } }); router.get("/", async (req, res) => { logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`); try { const maps = await map.getAvailableMaps(); var selected_map_name = ""; 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) { for (m of Object.keys(maps)) { table_data += `