ShyyTAUI/src/routes/api/docs/[slug]/+server.ts
2025-05-12 11:53:27 +02:00

40 lines
No EOL
1.2 KiB
TypeScript

import { error } from '@sveltejs/kit';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the directory path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Path to markdown files
const DOCS_PATH = path.resolve(__dirname, '../../../../lib/taDocs');
export async function GET({ params }) {
const { slug } = params;
// Validate filename to prevent directory traversal attacks
if (!slug || slug.includes('..') || slug.includes('/') || !slug.endsWith('.md')) {
throw error(400, 'Invalid documentation file requested');
}
const filePath = path.join(DOCS_PATH, slug);
try {
if (!fs.existsSync(filePath)) {
throw error(404, 'Documentation file not found');
}
const content = fs.readFileSync(filePath, 'utf-8');
return new Response(content, {
headers: {
'Content-Type': 'text/markdown',
'Cache-Control': 'max-age=600' // Cache for 10 minutes
}
});
} catch (err) {
console.error(`Error reading documentation file: ${filePath}`, err);
throw error(500, 'Error loading documentation');
}
}