115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
const filename = getRouterParam(event, 'filename')
|
|
|
|
if (!filename) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Dateiname fehlt'
|
|
})
|
|
}
|
|
|
|
// Erlaubte Dateinamen für Sicherheit
|
|
const allowedFiles = [
|
|
'spielplan_gesamt.pdf',
|
|
'spielplan_erwachsene.pdf',
|
|
'spielplan_nachwuchs.pdf',
|
|
'spielplan_erwachsene_1.pdf',
|
|
'spielplan_erwachsene_2.pdf',
|
|
'spielplan_erwachsene_3.pdf',
|
|
'spielplan_erwachsene_4.pdf',
|
|
'spielplan_erwachsene_5.pdf',
|
|
'spielplan_jugendmannschaft.pdf'
|
|
]
|
|
|
|
// Prüfe ob es eine dynamische Mannschafts-PDF ist
|
|
const isDynamicMannschaft = filename.startsWith('spielplan_') && filename.endsWith('.pdf') &&
|
|
!allowedFiles.includes(filename)
|
|
|
|
if (!allowedFiles.includes(filename) && !isDynamicMannschaft) {
|
|
throw createError({
|
|
statusCode: 403,
|
|
statusMessage: 'Datei nicht erlaubt'
|
|
})
|
|
}
|
|
|
|
let filePath
|
|
|
|
if (isDynamicMannschaft) {
|
|
// Für dynamische Mannschafts-PDFs: Verwende Gesamt-Spielplan als Fallback
|
|
// Hier könnte später ein PDF-Generator implementiert werden
|
|
filePath = path.join(process.cwd(), 'public', 'documents', 'spielplaene', 'spielplan_gesamt.pdf')
|
|
} else {
|
|
// Für vordefinierte PDFs
|
|
filePath = path.join(process.cwd(), 'public', 'documents', 'spielplaene', filename)
|
|
}
|
|
|
|
// Prüfe ob Datei existiert
|
|
try {
|
|
await fs.access(filePath)
|
|
} catch (error) {
|
|
// Fallback: Erstelle eine informative HTML-Seite
|
|
const htmlContent = `
|
|
<!DOCTYPE html>
|
|
<html lang="de">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PDF nicht verfügbar</title>
|
|
<style>
|
|
body { font-family: Arial, sans-serif; margin: 40px; background-color: #f5f5f5; }
|
|
.container { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
|
|
.error { color: #d32f2f; font-size: 24px; margin-bottom: 20px; }
|
|
.message { color: #333; font-size: 16px; line-height: 1.6; }
|
|
.action { margin-top: 20px; padding: 15px; background: #e3f2fd; border-radius: 4px; }
|
|
.link { color: #1976d2; text-decoration: none; font-weight: bold; }
|
|
.link:hover { text-decoration: underline; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="error">📄 PDF-Datei nicht verfügbar</div>
|
|
<div class="message">
|
|
Die angeforderte PDF-Datei <strong>"${filename}"</strong> wurde noch nicht hochgeladen.
|
|
</div>
|
|
<div class="action">
|
|
<strong>Was können Sie tun?</strong><br>
|
|
• Laden Sie die PDF-Datei über das <a href="/cms/spielplaene" class="link">CMS</a> hoch<br>
|
|
• Kontaktieren Sie den Administrator<br>
|
|
• Versuchen Sie es später erneut
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>`
|
|
|
|
setHeader(event, 'Content-Type', 'text/html; charset=utf-8')
|
|
return htmlContent
|
|
}
|
|
|
|
// Datei lesen
|
|
const fileBuffer = await fs.readFile(filePath)
|
|
|
|
// Content-Type setzen
|
|
setHeader(event, 'Content-Type', 'application/pdf')
|
|
setHeader(event, 'Content-Disposition', `attachment; filename="${filename}"`)
|
|
setHeader(event, 'Content-Length', fileBuffer.length.toString())
|
|
|
|
return fileBuffer
|
|
|
|
} catch (error) {
|
|
console.error('Fehler beim Laden der PDF-Datei:', error)
|
|
|
|
if (error.statusCode) {
|
|
throw error
|
|
}
|
|
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Fehler beim Laden der PDF-Datei'
|
|
})
|
|
}
|
|
})
|