new features
This commit is contained in:
52
backend/src/middleware/upload.ts
Normal file
52
backend/src/middleware/upload.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
const UPLOAD_DIR = path.resolve(__dirname, '../../../uploads/bestellungen');
|
||||
const THUMBNAIL_DIR = path.resolve(__dirname, '../../../uploads/bestellungen/thumbnails');
|
||||
|
||||
// Ensure directories exist
|
||||
[UPLOAD_DIR, THUMBNAIL_DIR].forEach(dir => {
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
logger.info(`Created upload directory: ${dir}`);
|
||||
}
|
||||
});
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination(_req: any, _file: any, cb: any) {
|
||||
cb(null, UPLOAD_DIR);
|
||||
},
|
||||
filename(_req: any, file: any, cb: any) {
|
||||
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
|
||||
const ext = path.extname(file.originalname);
|
||||
cb(null, `${uniqueSuffix}${ext}`);
|
||||
},
|
||||
});
|
||||
|
||||
const ALLOWED_TYPES = [
|
||||
'image/jpeg', 'image/png', 'image/gif', 'image/webp',
|
||||
'application/pdf',
|
||||
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'text/plain', 'text/csv',
|
||||
];
|
||||
|
||||
const multerOptions: any = {
|
||||
storage,
|
||||
fileFilter(_req: any, file: any, cb: any) {
|
||||
if (ALLOWED_TYPES.includes(file.mimetype)) {
|
||||
cb(null, true);
|
||||
} else {
|
||||
cb(new Error(`Dateityp ${file.mimetype} ist nicht erlaubt.`));
|
||||
}
|
||||
},
|
||||
limits: { fileSize: 20 * 1024 * 1024 }, // 20 MB
|
||||
};
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export const uploadBestellung: any = multer(multerOptions);
|
||||
|
||||
export { UPLOAD_DIR, THUMBNAIL_DIR };
|
||||
Reference in New Issue
Block a user