Adding modules
This commit is contained in:
191
commands/themeData.cjs
Normal file
191
commands/themeData.cjs
Normal file
@@ -0,0 +1,191 @@
|
||||
const { pool } = require("../triggerBackend.cjs");
|
||||
const escapeString = require("sql-escape-string");
|
||||
const fs = require("node:fs");
|
||||
|
||||
const getThemes = async (_req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = "SELECT * FROM trigger_database.themes;";
|
||||
conn.query(query).then((rows) => {
|
||||
res.status(200).send(rows);
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const addTheme = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `INSERT INTO trigger_database.themes (name, backgroundImage, backgroundColor, searchColor, primaryColor, accentColor, borderColor, themeFont) VALUES (${escapeString(req.body.name)},${escapeString(req.body.backgroundImage)},${escapeString(req.body.backgroundColor)},${escapeString(req.body.searchColor)},${escapeString(req.body.primaryColor)},${escapeString(req.body.accentColor)},${escapeString(req.body.borderColor)},${escapeString(req.body.themeFont)})`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const updateTheme = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `UPDATE trigger_database.themes SET name = ${escapeString(req.body.name)}, backgroundImage = ${escapeString(req.body.backgroundImage)}, backgroundColor = ${escapeString(req.body.backgroundColor)}, searchColor = ${escapeString(req.body.searchColor)}, primaryColor = ${escapeString(req.body.primaryColor)}, accentColor = ${escapeString(req.body.accentColor)}, borderColor = ${escapeString(req.body.borderColor)}, randomEnabled = ${req.body.randomEnabled ? 1 : 0}, textShadowsEnabled = ${req.body.textShadowsEnabled ? 1 : 0}, appShadowsEnabled = ${req.body.appShadowsEnabled ? 1 : 0}, themeFont = ${req.body.themeFont === null ? null : escapeString(req.body.themeFont)} WHERE id = ${req.body.id};`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const deleteTheme = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `DELETE FROM trigger_database.themes WHERE id = ${req.body.id}`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const getThemePlaylists = async (_req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = "SELECT * FROM trigger_database.themePlaylists;";
|
||||
conn.query(query).then((rows) => {
|
||||
res.status(200).send(rows);
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const addThemePlaylist = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `INSERT INTO trigger_database.themePlaylists (name, themeList) VALUES (${escapeString(req.body.name)},${escapeString(req.body.themeList)});`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const updateThemePlaylist = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `UPDATE trigger_database.themePlaylists SET name = ${escapeString(req.body.name)}, themeList = ${escapeString(req.body.themeList)} WHERE id = ${req.body.id};`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const deleteThemePlaylist = async (req, res) => {
|
||||
const conn = await pool.getConnection();
|
||||
const query = `DELETE FROM trigger_database.themePlaylists WHERE id = ${req.body.id}`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const getBackgrounds = async (_req, res) => {
|
||||
fs.readdir(
|
||||
"../TriggerFrontend/backgrounds",
|
||||
{ withFileTypes: true },
|
||||
(_err, files) => {
|
||||
res
|
||||
.status(200)
|
||||
.send(files.filter((file) => file.isFile()).map((file) => file.name));
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
const uploadBackground = async (req, res) => {
|
||||
const image = req.file.path;
|
||||
const thumbnail = `${req.file.destination}thumbs/${req.file.filename}`;
|
||||
exec(`convert ${image} -resize 'x1080' -define jpeg:extent=300kb ${image}`);
|
||||
exec(`convert ${image} -resize 'x360' -define jpeg:extent=75kb ${thumbnail}`);
|
||||
res.status(200).send({ status: "success", statusText: image });
|
||||
};
|
||||
|
||||
const updateBackground = async (req, res) => {
|
||||
fs.rename(
|
||||
`../TriggerFrontend/backgrounds/${req.body.fileName}`,
|
||||
`../TriggerFrontend/backgrounds/${req.body.newFileName}`,
|
||||
(err) => {
|
||||
if (err) throw err;
|
||||
},
|
||||
);
|
||||
fs.rename(
|
||||
`../TriggerFrontend/backgrounds/thumbs/${req.body.fileName}`,
|
||||
`../TriggerFrontend/backgrounds/thumbs/${req.body.newFileName}`,
|
||||
(err) => {
|
||||
if (err) throw err;
|
||||
},
|
||||
);
|
||||
const conn = await pool.getConnection();
|
||||
const query = `UPDATE trigger_database.themes SET backgroundImage = ${escapeString(req.body.newFileName)} WHERE backgroundImage = ${escapeString(req.body.fileName)};`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
const deleteBackground = async (req, res) => {
|
||||
fs.rm(`../TriggerFrontend/backgrounds/${req.body.fileName}`, (_err) => {
|
||||
if (_err) throw _err;
|
||||
});
|
||||
fs.rm(
|
||||
`../TriggerFrontend/backgrounds/thumbs/${req.body.fileName}`,
|
||||
(_err) => {
|
||||
if (_err) throw _err;
|
||||
},
|
||||
);
|
||||
const conn = await pool.getConnection();
|
||||
const query = `UPDATE trigger_database.themes SET backgroundImage = 'none' WHERE backgroundImage = ${escapeString(req.body.fileName)};`;
|
||||
conn
|
||||
.query(query)
|
||||
.then((_rows) => {
|
||||
res.status(200).send({ status: "success" });
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(500).send({ error: err });
|
||||
});
|
||||
conn.end();
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getThemes,
|
||||
addTheme,
|
||||
updateTheme,
|
||||
deleteTheme,
|
||||
getThemePlaylists,
|
||||
addThemePlaylist,
|
||||
updateThemePlaylist,
|
||||
deleteThemePlaylist,
|
||||
getBackgrounds,
|
||||
uploadBackground,
|
||||
updateBackground,
|
||||
deleteBackground,
|
||||
};
|
||||
Reference in New Issue
Block a user