From 36bacfec6838a27e36ef056c8af6a13e2e24537d Mon Sep 17 00:00:00 2001 From: majkus522 Date: Sun, 31 Dec 2023 16:08:56 +0100 Subject: [PATCH 1/3] Fix routes Fix access to deeper routes, e.g. weekly-boss --- src/modules/filesystem.ts | 9 ++++ src/routes/index.ts | 91 +++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/modules/filesystem.ts b/src/modules/filesystem.ts index 0a7b26ba..e589ed1a 100644 --- a/src/modules/filesystem.ts +++ b/src/modules/filesystem.ts @@ -12,6 +12,15 @@ import chalk from 'chalk'; const cache = new keyv(); +export async function containsFolders(path: string):Promise +{ + try { + return (await fs.readdir(`assets/data/${path}`, { withFileTypes: true }))[0].isDirectory(); + } catch (e) { + return false; + } +} + export async function getTypes(): Promise { const found = await cache.get('types'); if (found) return found; diff --git a/src/routes/index.ts b/src/routes/index.ts index bf2d34a1..e43c117c 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -9,6 +9,7 @@ import { getEntity, getImage, getTypes, + containsFolders, } from '../modules/filesystem'; const router = new Router(); @@ -20,22 +21,7 @@ router.get('/', async (ctx) => { }; }); -router.get('/:type', async (ctx) => { - const { type } = ctx.params; - try { - const entityNames = await getAvailableEntities(type); - ctx.body = entityNames; - } catch (e) { - ctx.status = 404; - const availableTypes = await getTypes(); - ctx.body = { - error: e.message, - availableTypes, - }; - } -}); - -router.get('/:type/all', async (ctx) => { +router.get('/:type*/all', async (ctx) => { try { const { lang, ...params } = ctx.query; const { type } = ctx.params; @@ -77,22 +63,13 @@ router.get('/:type/all', async (ctx) => { } }); -router.get('/:type/:id', async (ctx) => { - try { - const { lang } = ctx.query; - const { type, id } = ctx.params; - - ctx.body = await getEntity(type, id, lang as string); - } catch (e) { - ctx.status = 404; - ctx.body = { error: e.message }; - } -}); - -router.get('/:type/:id/list', async (ctx) => { - const { type, id } = ctx.params; - +router.get('/:type*/list', async (ctx) => { + let { type } = ctx.params; + const id = type.substring(type.lastIndexOf("/") + 1); + type = type.substring(0, type.lastIndexOf("/")); try { + if(await containsFolders(ctx.params.type)) + throw new Error(`No images for ${ctx.params.type}/lista exist`); ctx.body = await getAvailableImages(type, id); } catch (e) { ctx.status = 404; @@ -100,20 +77,48 @@ router.get('/:type/:id/list', async (ctx) => { } }); -router.get('/:type/:id/:imageType', async (ctx) => { - const { type, id, imageType } = ctx.params; - - try { - const image = await getImage(type, id, imageType); - - ctx.body = image.image; - ctx.type = image.type; - } catch (e) { - ctx.status = 404; +router.get('/:type*', async (ctx) => { + let { type } = ctx.params; + if(!(await containsFolders(type.substring(0, type.lastIndexOf("/"))))) { + const imageType = type.substring(type.lastIndexOf("/") + 1); + type = type.substring(0, type.lastIndexOf("/")); + const id = type.substring(type.lastIndexOf("/") + 1); + type = type.substring(0, type.lastIndexOf("/")); + try { + const image = await getImage(type, id, imageType); + + ctx.body = image.image; + ctx.type = image.type; + } catch (e) { + ctx.status = 404; + try { + const av = await getAvailableImages(type, id); + ctx.body = { error: e.message, availableImages: av }; + } catch (e) { + ctx.body = { error: e.message }; + } + } + } else if(await containsFolders(type)) { + try { + const entityNames = await getAvailableEntities(type); + ctx.body = entityNames; + } catch (e) { + ctx.status = 404; + const availableTypes = await getTypes(); + ctx.body = { + error: e.message, + availableTypes, + }; + } + } else { try { - const av = await getAvailableImages(type, id); - ctx.body = { error: e.message, availableImages: av }; + const { lang } = ctx.query; + const id = type.substring(type.lastIndexOf("/") + 1); + type = type.substring(0, type.lastIndexOf("/")); + + ctx.body = await getEntity(type, id, lang as string); } catch (e) { + ctx.status = 404; ctx.body = { error: e.message }; } } From 8ad3d52a42a7a9764a95b6c9e9ef1fddc5b8bceb Mon Sep 17 00:00:00 2001 From: majkus522 Date: Tue, 14 May 2024 20:26:11 +0200 Subject: [PATCH 2/3] Fix --- src/routes/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/routes/index.ts b/src/routes/index.ts index e43c117c..cd2c2a0e 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -68,8 +68,6 @@ router.get('/:type*/list', async (ctx) => { const id = type.substring(type.lastIndexOf("/") + 1); type = type.substring(0, type.lastIndexOf("/")); try { - if(await containsFolders(ctx.params.type)) - throw new Error(`No images for ${ctx.params.type}/lista exist`); ctx.body = await getAvailableImages(type, id); } catch (e) { ctx.status = 404; From 58545630eacb4e8275f3031351f590dcc1e936d0 Mon Sep 17 00:00:00 2001 From: majkus522 Date: Tue, 14 May 2024 20:49:05 +0200 Subject: [PATCH 3/3] Typo --- src/routes/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/routes/index.ts b/src/routes/index.ts index cd2c2a0e..e071caf7 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -68,6 +68,8 @@ router.get('/:type*/list', async (ctx) => { const id = type.substring(type.lastIndexOf("/") + 1); type = type.substring(0, type.lastIndexOf("/")); try { + if(await containsFolders(ctx.params.type)) + throw new Error(`No images for ${ctx.params.type} exist`); ctx.body = await getAvailableImages(type, id); } catch (e) { ctx.status = 404;