import fs from 'fs/promises' import path from 'path' import { getUserFromToken } from '../../utils/auth.js' export default defineEventHandler(async (event) => { try { const token = getCookie(event, 'auth_token') const currentUser = token ? await getUserFromToken(token) : null if (!currentUser) { throw createError({ statusCode: 401, statusMessage: 'Nicht authentifiziert' }) } if (currentUser.role !== 'admin' && currentUser.role !== 'vorstand') { throw createError({ statusCode: 403, statusMessage: 'Keine Berechtigung' }) } const { filename, content } = await readBody(event) if (!filename || !content) { throw createError({ statusCode: 400, statusMessage: 'Filename und Content sind erforderlich' }) } // Sicherheitsprüfung: Nur bestimmte Dateien erlauben const allowedFiles = [ 'vereinsmeisterschaften.csv', 'mannschaften.csv', 'termine.csv', 'spielplan.csv' ] if (!allowedFiles.includes(filename)) { throw createError({ statusCode: 403, statusMessage: 'Datei nicht erlaubt' }) } // Handle both dev and production paths const cwd = process.cwd() let filePath // In production (.output/server), working dir is .output if (cwd.endsWith('.output')) { filePath = path.join(cwd, '../public/data', filename) } else { // In development, working dir is project root filePath = path.join(cwd, 'public/data', filename) } // Debugging: Zeige Pfad-Informationen const absoluteFilePath = path.resolve(filePath) console.log('=== CSV SAVE DEBUG ===') console.log('filename:', filename) console.log('process.cwd():', cwd) console.log('calculated filePath:', filePath) console.log('absolute filePath:', absoluteFilePath) console.log('content length:', content.length) console.log('content preview (first 200 chars):', content.substring(0, 200)) const dataDir = path.dirname(filePath) // Sicherstellen, dass das Verzeichnis existiert await fs.mkdir(dataDir, { recursive: true }) console.log('dataDir exists/created:', dataDir) // Prüfe ob Datei existiert vor dem Schreiben try { const statsBefore = await fs.stat(absoluteFilePath) console.log('Datei existiert vor Schreiben:', true) console.log('Dateigröße vor Schreiben:', statsBefore.size, 'bytes') console.log('Letzte Änderung vor Schreiben:', statsBefore.mtime) } catch (error) { console.log('Datei existiert nicht vor Schreiben:', error.code) } // Datei schreiben await fs.writeFile(filePath, content, 'utf8') console.log('Datei geschrieben:', absoluteFilePath) // Prüfe ob Datei existiert nach dem Schreiben try { const statsAfter = await fs.stat(absoluteFilePath) console.log('Datei existiert nach Schreiben:', true) console.log('Dateigröße nach Schreiben:', statsAfter.size, 'bytes') console.log('Letzte Änderung nach Schreiben:', statsAfter.mtime) // Lese die Datei zurück, um zu bestätigen, dass sie geschrieben wurde const writtenContent = await fs.readFile(absoluteFilePath, 'utf8') console.log('Geschriebener Inhalt (first 200 chars):', writtenContent.substring(0, 200)) console.log('Geschriebener Inhalt length:', writtenContent.length) console.log('Inhalte stimmen überein:', writtenContent === content ? 'JA' : 'NEIN') } catch (error) { console.error('FEHLER: Datei existiert nicht nach Schreiben:', error) } console.log('=== END CSV SAVE DEBUG ===') return { success: true, message: 'Datei erfolgreich gespeichert', debug: { filePath: absoluteFilePath, cwd: cwd, contentLength: content.length } } } catch (error) { console.error('Fehler beim Speichern der CSV-Datei:', error) if (error.statusCode) { throw error } throw createError({ statusCode: 500, statusMessage: 'Fehler beim Speichern der Datei' }) } })