Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
use middleware for parsing to avoid duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
tycrek committed Jun 15, 2021
1 parent 0227ce1 commit 4a69115
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions ass.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ function startup() {
// Don't process favicon requests
app.use((req, res, next) => req.url.includes('favicon.ico') ? res.sendStatus(204) : next());

// Middleware for parsing the resource ID and handling 404
app.use('/:resourceId', (req, res, next) => {
// Parse the resource ID
req.ass = { resourceId: req.params.resourceId.split('.')[0] };

// If the ID is invalid, return 404. Otherwise, continue normally
(!req.ass.resourceId || !data[req.ass.resourceId]) ? res.sendStatus(404) : next();
});

// Upload file
app.post('/', upload.single('file'), (req, res) => {
// Prevent uploads from unauthorized clients
Expand Down Expand Up @@ -161,11 +170,7 @@ function startup() {

// View file
app.get('/:resourceId', (req, res) => {
// Parse the resource ID
let resourceId = req.params.resourceId.split('.')[0];

// If the ID is invalid, return 404
if (!resourceId || !data[resourceId]) return res.sendStatus(404);
let resourceId = req.ass.resourceId;

// If the client is Discord, send an Open Graph embed
if (req.useragent.isBot) return res.type('html').send(new OpenGraph(getTrueHttp(), getTrueDomain(), resourceId, data[resourceId]).build());
Expand All @@ -183,11 +188,7 @@ function startup() {
// https://oembed.com/
// https://old.reddit.com/r/discordapp/comments/82p8i6/a_basic_tutorial_on_how_to_get_the_most_out_of/
app.get('/:resourceId/oembed.json', (req, res) => {
// Parse the resource ID
let resourceId = req.params.resourceId.split('.')[0];

// If the ID is invalid, return 404
if (!resourceId || !data[resourceId]) return res.sendStatus(404);
let resourceId = req.ass.resourceId;

// Build the oEmbed object & send the response
let { opengraph, mimetype } = data[resourceId];
Expand Down

0 comments on commit 4a69115

Please sign in to comment.