34 lines
1.4 KiB
JavaScript
34 lines
1.4 KiB
JavaScript
import multer from 'multer';
|
|
|
|
const imageMimeTypes = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/gif']);
|
|
const verificationMimeTypes = new Set([...imageMimeTypes, 'application/pdf']);
|
|
const videoMimeTypes = new Set(['video/mp4', 'video/webm', 'video/ogg', 'video/quicktime']);
|
|
|
|
function createUpload({ maxBytes, allowedMimeTypes }) {
|
|
return multer({
|
|
storage: multer.memoryStorage(),
|
|
limits: { fileSize: maxBytes, files: 1 },
|
|
fileFilter: (_req, file, callback) => {
|
|
if (!allowedMimeTypes.has(file.mimetype)) {
|
|
callback(new multer.MulterError('LIMIT_UNEXPECTED_FILE', file.fieldname));
|
|
return;
|
|
}
|
|
callback(null, true);
|
|
},
|
|
});
|
|
}
|
|
|
|
export const imageUpload = createUpload({ maxBytes: 10 * 1024 * 1024, allowedMimeTypes: imageMimeTypes });
|
|
export const adultVerificationUpload = createUpload({ maxBytes: 10 * 1024 * 1024, allowedMimeTypes: verificationMimeTypes });
|
|
export const videoUpload = createUpload({ maxBytes: 100 * 1024 * 1024, allowedMimeTypes: videoMimeTypes });
|
|
|
|
export function uploadErrorHandler(error, _req, res, next) {
|
|
if (error instanceof multer.MulterError) {
|
|
const message = error.code === 'LIMIT_FILE_SIZE'
|
|
? 'Die hochgeladene Datei ist zu groß.'
|
|
: 'Der Dateityp ist nicht erlaubt.';
|
|
return res.status(400).json({ error: message });
|
|
}
|
|
return next(error);
|
|
}
|