27 lines
900 B
TypeScript
27 lines
900 B
TypeScript
import { Router } from 'express';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const router = Router();
|
|
const LOCALES_DIR = path.join(__dirname, '..', '..', 'locales');
|
|
|
|
router.get('/:locale/*', (req, res) => {
|
|
const locale = req.params.locale;
|
|
const filePath = (req.params as Record<string, string>)[0];
|
|
const abs = path.resolve(path.join(LOCALES_DIR, locale, filePath));
|
|
if (!abs.startsWith(path.resolve(LOCALES_DIR) + path.sep)) {
|
|
res.status(403).json({ error: 'Forbidden' });
|
|
return;
|
|
}
|
|
if (!fs.existsSync(abs)) {
|
|
res.status(404).json({ error: 'Not found' });
|
|
return;
|
|
}
|
|
const ext = path.extname(abs).slice(1);
|
|
if (ext === 'html') res.type('text/html').send(fs.readFileSync(abs, 'utf8'));
|
|
else if (ext === 'json') res.json(JSON.parse(fs.readFileSync(abs, 'utf8')));
|
|
else res.type('text/plain').send(fs.readFileSync(abs, 'utf8'));
|
|
});
|
|
|
|
export default router;
|