mirror of
https://github.com/gnmyt/myspeed.git
synced 2026-04-29 16:29:39 -05:00
Updated the config.js routes to use the new validation system
This commit is contained in:
+4
-28
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user