import multer from 'multer'; import path from 'path'; import fs from 'fs'; const storage = multer.diskStorage({ destination: (req, file, cb) => { const rezeptnummer = req.body.rezeptnummer || req.params.rezeptnummer; const rNummer = 'R' + String(rezeptnummer).padStart(3, '0'); const uploadPath = path.join('uploads', rNummer); if (!fs.existsSync(uploadPath)) { fs.mkdirSync(uploadPath, { recursive: true }); } cb(null, uploadPath); }, filename: (req, file, cb) => { const rezeptnummer = req.body.rezeptnummer || req.params.rezeptnummer; const rNummer = 'R' + String(rezeptnummer).padStart(3, '0'); const uploadPath = path.join('uploads', rNummer); const existingFiles = fs.readdirSync(uploadPath).filter(f => f.startsWith(rNummer)); const nextIndex = existingFiles.length; const filename = `${rNummer}_${nextIndex}.jpg`; cb(null, filename); } }); const fileFilter = (req, file, cb) => { const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png']; if (allowedTypes.includes(file.mimetype)) { cb(null, true); } else { cb(new Error('Nur JPG, JPEG und PNG Dateien erlaubt'), false); } }; export const upload = multer({ storage, fileFilter, limits: { fileSize: 10 * 1024 * 1024 } // 10MB });