From 78ce2c90c55b82e96e6bd83cd4330fb4840ffe1f Mon Sep 17 00:00:00 2001 From: Ianyourgod Date: Sun, 3 Mar 2024 15:59:39 -0600 Subject: [PATCH] oopsies --- api/core.js | 34 ++++++++++++++++++----- api/db/UserManager.js | 10 +++++++ api/v1/routes/projects/projectRedirect.js | 9 ++++-- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/api/core.js b/api/core.js index f9dd16b..a82a090 100644 --- a/api/core.js +++ b/api/core.js @@ -31,12 +31,32 @@ app.use(rateLimit({ })); const UserManager = new um(); +UserManager.init(); -endpointLoader(app, 'v1/routes', { - UserManager: UserManager, - homeDir: path.join(__dirname, "../") -}); +function escapeXML(unsafe) { + unsafe = String(unsafe); + return unsafe.replace(/[<>&'"\n]/g, c => { + switch (c) { + case '<': return '<'; + case '>': return '>'; + case '&': return '&'; + case '\'': return '''; + case '"': return '"'; + case '\n': return ' ' + } + }); +}; -app.listen(PORT, () => { - console.log(`API is listening on port ${PORT}`); -}); \ No newline at end of file +(async () => { + await UserManager.init(); + + endpointLoader(app, 'v1/routes', { + UserManager: UserManager, + homeDir: path.join(__dirname, "../"), + escapeXML: escapeXML + }); + + app.listen(PORT, () => { + console.log(`API is listening on port ${PORT}`); + }); +})(); \ No newline at end of file diff --git a/api/db/UserManager.js b/api/db/UserManager.js index a199ac3..3d758e7 100644 --- a/api/db/UserManager.js +++ b/api/db/UserManager.js @@ -850,6 +850,16 @@ class UserManager { async deleteMessage(id) { await this.messages.deleteOne({id: id}); } + + /** + * @param {string} id - ID of the project + * @returns {Promise} - True if the project exists, false if not + */ + async projectExists(id) { + const result = await this.projects.findOne({id: id}); + + return result !== undefined; + } } module.exports = UserManager; \ No newline at end of file diff --git a/api/v1/routes/projects/projectRedirect.js b/api/v1/routes/projects/projectRedirect.js index 8ca3b45..98a2a03 100644 --- a/api/v1/routes/projects/projectRedirect.js +++ b/api/v1/routes/projects/projectRedirect.js @@ -4,14 +4,17 @@ const path = require('path'); module.exports = (app, options) => { const projectTemplate = fs.readFileSync(path.join(options.homeDir, 'project.html')).toString(); app.get('/:id', async function (req, res) { - const json = options.UserManager.getProjectData(String(req.params.id)); + const json = await options.UserManager.getProjectData(String(req.params.id)); + if (!json) { - res.sendFile(path.join(options.homeDir, '404-noproject.html')); + res.status(404); + res.send('Not found'); return; } + let html = projectTemplate; for (const prop in json) { - html = html.replaceAll(`{project.${prop}}`, escapeXML(json[prop])); + html = html.replaceAll(`{project.${prop}}`, options.escapeXML(json[prop])); } res.status(200); res.send(html);