Refactor environment configuration for local development; update SMTP settings and add JWT secret, encryption key, and debug options. Enhance Nuxt configuration for development server and runtime settings. Introduce new membership application form with validation and PDF generation functionality. Update footer and navigation components to include new membership links. Revise user and session data in JSON files.
This commit is contained in:
100
server/api/membership/download/[id].get.js
Normal file
100
server/api/membership/download/[id].get.js
Normal file
@@ -0,0 +1,100 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import { getUserFromToken } from '../../../utils/auth.js'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
// Datei-ID aus der URL extrahieren
|
||||
const fileId = decodeURIComponent(getRouterParam(event, 'id'))
|
||||
if (!fileId) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Datei-ID fehlt'
|
||||
})
|
||||
}
|
||||
|
||||
// Upload-Verzeichnis finden
|
||||
const uploadDir = path.join(process.cwd(), 'public', 'uploads')
|
||||
console.log('Upload-Verzeichnis:', uploadDir)
|
||||
|
||||
// Alle Dateien im Upload-Verzeichnis durchsuchen
|
||||
const files = await fs.readdir(uploadDir)
|
||||
console.log('Verfügbare Dateien:', files)
|
||||
console.log('Gesuchte Datei-ID:', fileId)
|
||||
|
||||
const matchingFile = files.find(file => file.includes(fileId))
|
||||
console.log('Gefundene Datei:', matchingFile)
|
||||
|
||||
if (!matchingFile) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Datei nicht gefunden'
|
||||
})
|
||||
}
|
||||
|
||||
// Prüfen ob der Benutzer berechtigt ist, diese Datei herunterzuladen
|
||||
const token = getCookie(event, 'auth_token')
|
||||
let isAuthorized = false
|
||||
|
||||
if (token) {
|
||||
// Authentifizierte Benutzer prüfen
|
||||
const user = await getUserFromToken(token)
|
||||
if (user && ['admin', 'vorstand'].includes(user.role)) {
|
||||
// Admin/Vorstand kann alle Dateien herunterladen
|
||||
isAuthorized = true
|
||||
}
|
||||
}
|
||||
|
||||
// Prüfen ob es sich um eine aktuelle Session handelt (innerhalb der letzten 30 Minuten)
|
||||
const sessionKey = `download_${fileId}`
|
||||
const sessionValue = getCookie(event, sessionKey)
|
||||
|
||||
if (sessionValue === 'authorized') {
|
||||
// Session-basierte Berechtigung für Antragsteller
|
||||
isAuthorized = true
|
||||
}
|
||||
|
||||
if (!isAuthorized) {
|
||||
throw createError({
|
||||
statusCode: 403,
|
||||
statusMessage: 'Keine Berechtigung für diesen Download'
|
||||
})
|
||||
}
|
||||
|
||||
const filePath = path.join(uploadDir, matchingFile)
|
||||
|
||||
// Datei lesen
|
||||
const fileBuffer = await fs.readFile(filePath)
|
||||
|
||||
// MIME-Type basierend auf Dateiendung bestimmen
|
||||
const ext = path.extname(matchingFile).toLowerCase()
|
||||
let mimeType = 'application/octet-stream'
|
||||
let filename = matchingFile
|
||||
|
||||
if (ext === '.pdf') {
|
||||
mimeType = 'application/pdf'
|
||||
} else if (ext === '.txt') {
|
||||
mimeType = 'text/plain'
|
||||
filename = matchingFile.replace('.txt', '.pdf') // Für Download als PDF benennen
|
||||
}
|
||||
|
||||
// Datei als Download senden
|
||||
setHeader(event, 'Content-Type', mimeType)
|
||||
setHeader(event, 'Content-Disposition', `attachment; filename="${filename}"`)
|
||||
setHeader(event, 'Content-Length', fileBuffer.length.toString())
|
||||
|
||||
return fileBuffer
|
||||
|
||||
} catch (error) {
|
||||
console.error('Download-Fehler:', error)
|
||||
|
||||
if (error.statusCode) {
|
||||
throw error
|
||||
}
|
||||
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: 'Interner Serverfehler'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user