Files
harheimertc/server/api/mannschaften.get.js
Torsten Schulz (local) c409fa6d4b
Some checks failed
Code Analysis and Production Deploy / analyze (push) Has been skipped
Code Analysis and Production Deploy / deploy-production (push) Has been skipped
Code Analysis and Production Deploy / deploy-test (push) Has been cancelled
Update candidate paths for CSV file retrieval in mannschaften.get.js
- Adjusted the logic to prioritize the new CMS write target for public data.
- Updated comments to clarify the order of candidate paths for file retrieval.
2026-05-05 15:13:22 +02:00

65 lines
1.6 KiB
JavaScript

import { promises as fs } from 'fs'
import path from 'path'
async function exists(p) {
try {
await fs.access(p)
return true
} catch {
return false
}
}
export default defineEventHandler(async (event) => {
try {
const cwd = process.cwd()
const filename = 'mannschaften.csv'
// Prefer CMS write target first (server/data/public-data),
// then legacy locations.
const candidates = [
path.join(cwd, 'server/data/public-data', filename),
path.join(cwd, '../server/data/public-data', filename),
path.join(cwd, '.output/server/data', filename),
path.join(cwd, 'server/data', filename),
path.join(cwd, '.output/public/data', filename),
path.join(cwd, 'public/data', filename),
path.join(cwd, '../.output/public/data', filename),
path.join(cwd, '../public/data', filename)
]
let csvPath = null
for (const p of candidates) {
if (await exists(p)) {
csvPath = p
break
}
}
if (!csvPath) {
throw createError({
statusCode: 404,
statusMessage: 'Mannschaften-Datei nicht gefunden'
})
}
const csv = await fs.readFile(csvPath, 'utf-8')
setHeader(event, 'Content-Type', 'text/csv; charset=utf-8')
setHeader(event, 'Cache-Control', 'no-cache, no-store, must-revalidate')
setHeader(event, 'Pragma', 'no-cache')
setHeader(event, 'Expires', '0')
return csv
} catch (error) {
if (error?.statusCode) throw error
console.error('Fehler beim Laden der Mannschaften:', error)
throw createError({
statusCode: 500,
statusMessage: 'Fehler beim Laden der Mannschaften'
})
}
})