mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-27 23:13:01 -04:00
32 lines
903 B
TypeScript
32 lines
903 B
TypeScript
import { promises as fs } from 'node:fs';
|
|
|
|
import { rootDir } from '../../paths.ts';
|
|
|
|
import { getLocale } from '~~/server/data.ts';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const filename = getRouterParam(event, 'filename');
|
|
if (!filename || filename.includes('..')) {
|
|
throw createError({
|
|
status: 400,
|
|
statusMessage: 'Bad Request',
|
|
});
|
|
}
|
|
|
|
const path = `${rootDir}/locale/${getLocale(event)}/docs/${filename}`;
|
|
|
|
try {
|
|
await fs.access(path);
|
|
if (filename.endsWith('.json')) {
|
|
event.node.res.setHeader('Content-Type', 'application/json');
|
|
}
|
|
event.node.res.setHeader('Cache-Control', 'public, max-age=86400');
|
|
return await fs.readFile(path);
|
|
} catch (err) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Not Found',
|
|
});
|
|
}
|
|
});
|