101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
import multer from 'multer'
|
|
import pdf from 'pdf-parse'
|
|
import fs from 'fs/promises'
|
|
import path from 'path'
|
|
|
|
// Multer-Konfiguration für PDF-Uploads
|
|
const storage = multer.diskStorage({
|
|
destination: (req, file, cb) => {
|
|
cb(null, 'public/documents/')
|
|
},
|
|
filename: (req, file, cb) => {
|
|
cb(null, 'satzung.pdf')
|
|
}
|
|
})
|
|
|
|
const upload = multer({
|
|
storage,
|
|
fileFilter: (req, file, cb) => {
|
|
if (file.mimetype === 'application/pdf') {
|
|
cb(null, true)
|
|
} else {
|
|
cb(new Error('Nur PDF-Dateien sind erlaubt'), false)
|
|
}
|
|
},
|
|
limits: {
|
|
fileSize: 10 * 1024 * 1024 // 10MB Limit
|
|
}
|
|
})
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
if (event.method !== 'POST') {
|
|
throw createError({
|
|
statusCode: 405,
|
|
statusMessage: 'Method Not Allowed'
|
|
})
|
|
}
|
|
|
|
try {
|
|
// Multer-Middleware für File-Upload
|
|
await new Promise((resolve, reject) => {
|
|
upload.single('pdf')(event.node.req, event.node.res, (err) => {
|
|
if (err) reject(err)
|
|
else resolve()
|
|
})
|
|
})
|
|
|
|
const file = event.node.req.file
|
|
if (!file) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Keine PDF-Datei hochgeladen'
|
|
})
|
|
}
|
|
|
|
// PDF-Text extrahieren
|
|
const pdfBuffer = await fs.readFile(file.path)
|
|
const pdfData = await pdf(pdfBuffer)
|
|
|
|
// Text in HTML-Format konvertieren (einfache Formatierung)
|
|
const htmlContent = convertTextToHtml(pdfData.text)
|
|
|
|
// Config aktualisieren
|
|
const configPath = 'server/data/config.json'
|
|
const configData = JSON.parse(await fs.readFile(configPath, 'utf-8'))
|
|
|
|
configData.seiten.satzung = {
|
|
pdfUrl: '/documents/satzung.pdf',
|
|
content: htmlContent
|
|
}
|
|
|
|
await fs.writeFile(configPath, JSON.stringify(configData, null, 2))
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Satzung erfolgreich hochgeladen und verarbeitet',
|
|
pdfUrl: '/documents/satzung.pdf'
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('PDF Upload Error:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: error.message || 'Fehler beim Verarbeiten der PDF-Datei'
|
|
})
|
|
}
|
|
})
|
|
|
|
function convertTextToHtml(text) {
|
|
// Einfache Text-zu-HTML-Konvertierung
|
|
let html = text
|
|
.replace(/\n\n+/g, '</p><p>') // Absätze
|
|
.replace(/\n/g, '<br>') // Zeilenumbrüche
|
|
.replace(/^(.+)$/gm, '<p>$1</p>') // Alle Zeilen in Paragraphen
|
|
|
|
// Überschriften erkennen (einfache Heuristik)
|
|
html = html.replace(/<p>(§\s*\d+.*?)<\/p>/g, '<h3>$1</h3>')
|
|
html = html.replace(/<p>(\d+\.\s+.*?)<\/p>/g, '<h4>$1</h4>')
|
|
|
|
return `<h2>Satzung</h2>${html}`
|
|
}
|