Updated the config.js routes to use the new validation system

This commit is contained in:
Mathias Wagner
2024-05-20 16:45:30 +02:00
parent f6a91b4dec
commit 1d04eedd47
+4 -28
View File
@@ -1,7 +1,6 @@
const app = require('express').Router();
const config = require('../controller/config');
const timer = require('../tasks/timer');
const cron = require('cron-validator');
const password = require('../middlewares/password');
app.get("/", password(true), async (req, res) => {
@@ -21,34 +20,11 @@ app.get("/", password(true), async (req, res) => {
});
app.patch("/:key", password(false), async (req, res) => {
if (!req.body.value?.toString()) return res.status(400).json({message: "You need to provide the new value"});
const value = await config.validateInput(req.params.key, req.body?.value);
if (Object.keys(value).length !== 1) return res.status(400).json({message: value});
if ((req.params.key === "ping" || req.params.key === "download" || req.params.key === "upload") && isNaN(req.body.value))
return res.status(400).json({message: "You need to provide a number in order to change this"});
if ((req.params.key === "ooklaId" || req.params.key === "libreId") && (isNaN(req.body.value) && req.body.value !== "none"))
return res.status(400).json({message: "You need to provide a number in order to change this"});
if (req.params.key === "passwordLevel" && !["none", "read"].includes(req.body.value))
return res.status(400).json({message: "You need to provide either none or read-access"});
if (req.params.key === "provider" && !["ookla", "libre", "cloudflare"].includes(req.body.value))
return res.status(400).json({message: "You need to provide a valid provider"});
if (req.params.key === "ping")
req.body.value = req.body.value.toString().split(".")[0];
if (req.params.key === "password" && req.body.value !== "none") req.body.value = await require('bcrypt').hash(req.body.value, 10);
if (req.params.key === "cron" && !cron.isValidCron(req.body.value.toString()))
return res.status(500).json({message: "Not a valid cron expression"});
if (!await config.updateValue(req.params.key, req.body.value.toString()))
return res.status(404).json({message: "The provided key does not exist"});
if (process.env.PREVIEW_MODE === "true" && (req.params.key === "password" || req.params.key === "passwordLevel"))
return res.status(403).json({message: "You can't change the password in preview mode"});
if (!await config.updateValue(req.params.key, value.value))
return res.status(500).json({message: `Error updating the key '${req.params.key}'`});
if (req.params.key === "cron") {
timer.stopTimer();