Skip to content

Commit

Permalink
oopsies
Browse files Browse the repository at this point in the history
  • Loading branch information
Ianyourgod committed Mar 3, 2024
1 parent 5e5504d commit 78ce2c9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
34 changes: 27 additions & 7 deletions api/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '&lt;';
case '>': return '&gt;';
case '&': return '&amp;';
case '\'': return '&apos;';
case '"': return '&quot;';
case '\n': return '&#10;'
}
});
};

app.listen(PORT, () => {
console.log(`API is listening on port ${PORT}`);
});
(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}`);
});
})();
10 changes: 10 additions & 0 deletions api/db/UserManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,16 @@ class UserManager {
async deleteMessage(id) {
await this.messages.deleteOne({id: id});
}

/**
* @param {string} id - ID of the project
* @returns {Promise<boolean>} - 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;
9 changes: 6 additions & 3 deletions api/v1/routes/projects/projectRedirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78ce2c9

Please sign in to comment.