31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
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
|
|
});
|