initial commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
export class HttpError extends Error {
|
||||
constructor(status, message, details = undefined) {
|
||||
super(message);
|
||||
this.name = 'HttpError';
|
||||
this.status = status;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
export function notFoundHandler(req, res, next) {
|
||||
next(new HttpError(404, 'Ressource nicht gefunden.'));
|
||||
}
|
||||
|
||||
export function errorHandler(error, req, res, next) {
|
||||
if (res.headersSent) return next(error);
|
||||
let status = Number.isInteger(error.status) ? error.status : 500;
|
||||
if (error.code === 'LIMIT_FILE_SIZE') status = 413;
|
||||
if (error.code === 'LIMIT_UNEXPECTED_FILE') status = 400;
|
||||
if (status >= 500) console.error(error);
|
||||
res.status(status).json({
|
||||
error: error.name || 'Error',
|
||||
message: status >= 500 ? 'Interner Serverfehler.' : error.message,
|
||||
...(error.details ? { details: error.details } : {})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import path from 'node:path';
|
||||
import crypto from 'node:crypto';
|
||||
import multer from 'multer';
|
||||
import { config } from '../config.js';
|
||||
import { HttpError } from './errors.js';
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: path.join(config.storageDir, 'uploads'),
|
||||
filename(req, file, callback) {
|
||||
const safe = file.originalname.replace(/[^a-zA-Z0-9._-]/g, '_');
|
||||
callback(null, `${Date.now()}-${crypto.randomUUID()}-${safe}`);
|
||||
}
|
||||
});
|
||||
|
||||
function fileFilter(req, file, callback) {
|
||||
const allowed = new Set([
|
||||
'application/gpx+xml', 'application/xml', 'text/xml',
|
||||
'image/jpeg', 'image/png', 'image/webp',
|
||||
'audio/mpeg', 'audio/mp4', 'audio/x-m4a', 'audio/aac', 'audio/ogg', 'audio/wav', 'audio/webm'
|
||||
]);
|
||||
const extension = path.extname(file.originalname).toLowerCase();
|
||||
if (allowed.has(file.mimetype) || extension === '.gpx') return callback(null, true);
|
||||
callback(new HttpError(415, `Dateityp nicht unterstützt: ${file.originalname}`));
|
||||
}
|
||||
|
||||
export const upload = multer({
|
||||
storage,
|
||||
limits: { fileSize: config.maxUploadBytes, files: 25 },
|
||||
fileFilter
|
||||
});
|
||||
Reference in New Issue
Block a user