feat: ability to use / for URLS_ROUTE

This commit is contained in:
diced 2022-12-03 14:31:13 -08:00
parent ace474eb2c
commit cfdcf05135
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1

View file

@ -99,8 +99,10 @@ async function start() {
favicon.pipe(res); favicon.pipe(res);
}); });
router.on('GET', `${config.urls.route}/:id`, async (req, res, params) => { const urlsRoute = async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer); if (params.id === '') return notFound(req, res, nextServer);
else if (params.id === 'dashboard' && !config.features.headless)
return nextServer.render(req, res as ServerResponse, '/dashboard');
const url = await prisma.url.findFirst({ const url = await prisma.url.findFirst({
where: { where: {
@ -131,35 +133,53 @@ async function start() {
} }
return redirect(res, url.destination); return redirect(res, url.destination);
}); };
router.on( const uploadsRoute = async (req, res, params) => {
'GET', if (params.id === '') return notFound(req, res, nextServer);
config.uploader.route === '/' ? '/:id' : `${config.uploader.route}/:id`, else if (params.id === 'dashboard' && !config.features.headless)
async (req, res, params) => { return nextServer.render(req, res as ServerResponse, '/dashboard');
const image = await prisma.image.findFirst({
where: {
OR: [{ file: params.id }, { invisible: { invis: decodeURI(params.id) } }],
},
});
if (!image) return rawFile(req, res, nextServer, params.id);
else {
const failed = await preFile(image, prisma);
if (failed) return notFound(req, res, nextServer);
if (image.password || image.embed || image.mimetype.startsWith('text/'))
redirect(res, `/view/${image.file}`);
else fileDb(req, res, nextServer, handle, image);
postFile(image, prisma);
}
};
// makes sure to handle both in one route as you cant have two handlers with the same route
if (config.urls.route === '/' && config.uploader.route === '/') {
router.on('GET', '/:id', async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer); if (params.id === '') return notFound(req, res, nextServer);
else if (params.id === 'dashboard' && !config.features.headless) else if (params.id === 'dashboard' && !config.features.headless)
return nextServer.render(req, res as ServerResponse, '/dashboard'); return nextServer.render(req, res as ServerResponse, '/dashboard');
const image = await prisma.image.findFirst({ const url = await prisma.url.findFirst({
where: { where: {
OR: [{ file: params.id }, { invisible: { invis: decodeURI(params.id) } }], OR: [{ id: params.id }, { vanity: params.id }, { invisible: { invis: decodeURI(params.id) } }],
}, },
}); });
if (!image) return rawFile(req, res, nextServer, params.id); if (url) return urlsRoute(req, res, params);
else { else return uploadsRoute(req, res, params);
const failed = await preFile(image, prisma); });
if (failed) return notFound(req, res, nextServer); } else {
router.on('GET', config.urls.route === '/' ? '/:id' : `${config.urls.route}/:id`, urlsRoute);
if (image.password || image.embed || image.mimetype.startsWith('text/')) router.on('GET', config.uploader.route === '/' ? '/:id' : `${config.uploader.route}/:id`, uploadsRoute);
redirect(res, `/view/${image.file}`); }
else fileDb(req, res, nextServer, handle, image);
postFile(image, prisma);
}
}
);
router.on('GET', '/r/:id', async (req, res, params) => { router.on('GET', '/r/:id', async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer); if (params.id === '') return notFound(req, res, nextServer);