Skip to content

Commit

Permalink
Merge pull request #252 from majkus522/routes
Browse files Browse the repository at this point in the history
Fix routes
  • Loading branch information
man90es authored May 15, 2024
2 parents 7ad1e97 + 5854563 commit 2bda670
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 43 deletions.
9 changes: 9 additions & 0 deletions src/modules/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import chalk from 'chalk';

const cache = new keyv();

export async function containsFolders(path: string):Promise<boolean>
{
try {
return (await fs.readdir(`assets/data/${path}`, { withFileTypes: true }))[0].isDirectory();
} catch (e) {
return false;
}
}

export async function getTypes(): Promise<string[]> {
const found = await cache.get('types');
if (found) return found;
Expand Down
91 changes: 48 additions & 43 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getEntity,
getImage,
getTypes,
containsFolders,
} from '../modules/filesystem';

const router = new Router();
Expand All @@ -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;
Expand Down Expand Up @@ -77,43 +63,62 @@ 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} exist`);
ctx.body = await getAvailableImages(type, id);
} catch (e) {
ctx.status = 404;
ctx.body = { error: e.message };
}
});

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 };
}
}
Expand Down

0 comments on commit 2bda670

Please sign in to comment.