46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from 'next'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
const DOCS_DIR = path.join(process.cwd(), 'docs')
|
|
|
|
export default function handler(req: NextApiRequest, res: NextApiResponse): void {
|
|
const { file } = req.query
|
|
const locale = (req.query.locale as string) || 'fr' // Default to French
|
|
|
|
if (!file || typeof file !== 'string') {
|
|
return res.status(400).json({ error: 'File name is required' })
|
|
}
|
|
|
|
// Validate locale
|
|
const validLocale = locale === 'en' || locale === 'fr' ? locale : 'fr'
|
|
|
|
// Security: prevent directory traversal
|
|
const safeFile = path.basename(file)
|
|
|
|
// Try locale-specific file first (docs/en/file.md or docs/fr/file.md)
|
|
let filePath = path.join(DOCS_DIR, validLocale, safeFile)
|
|
|
|
// If locale-specific file doesn't exist, fallback to root docs directory
|
|
if (!fs.existsSync(filePath)) {
|
|
filePath = path.join(DOCS_DIR, safeFile)
|
|
}
|
|
|
|
// Check if file exists and is in docs directory
|
|
if (!filePath.startsWith(DOCS_DIR)) {
|
|
return res.status(403).json({ error: 'Access denied' })
|
|
}
|
|
|
|
try {
|
|
if (!fs.existsSync(filePath)) {
|
|
return res.status(404).json({ error: 'File not found' })
|
|
}
|
|
|
|
const content = fs.readFileSync(filePath, 'utf-8')
|
|
res.status(200).send(content)
|
|
} catch (error) {
|
|
console.error('Error reading doc file:', error)
|
|
res.status(500).json({ error: 'Failed to read file' })
|
|
}
|
|
}
|