Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
This commit is contained in:
33
backend/middleware/uploadMiddleware.js
Normal file
33
backend/middleware/uploadMiddleware.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user