multer optional path and res fix

This commit is contained in:
Pau 2025-05-14 20:17:08 +02:00
parent 69b111d995
commit c0b64962f3
1 changed files with 8 additions and 7 deletions

View File

@ -11,22 +11,23 @@ if (!fs.existsSync(uploadDir)) {
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: (req, file, cb) => { destination: (req, file, cb) => {
let userPath = req.body.path || ''; const userPath = req.body?.path || ''; //optional path from request body
userPath = path.normalize(userPath).replace(/^(\.\.[\/\\])+/, ''); const normalizedUserPath = path.normalize(userPath).replace(/^(\.\.[\/\\])+/, '');
const targetPath = path.join(uploadDir, userPath); const targetPath = path.join(uploadDir, normalizedUserPath);
if (!targetPath.startsWith(uploadDir)) { if (!targetPath.startsWith(uploadDir)) {
return cb(new Error('Invalid path.')); return cb(new Error('Invalid path.'));
} }
// Crea el directorio si no existe
if (!fs.existsSync(targetPath)) { if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath, { recursive: true }); fs.mkdirSync(targetPath, { recursive: true });
} }
cb(null, targetPath); cb(null, targetPath);
}, },
filename: (req, file, cb) => { filename: (req, file, cb) => {
const randomName = crypto.randomBytes(16).toString('hex'); const randomName = crypto.randomBytes(16).toString('hex');
@ -60,13 +61,13 @@ const uploadMiddleware = (fieldName, multiple = false) => (req, res, next) => {
uploader(req, res, (err) => { uploader(req, res, (err) => {
if (err instanceof multer.MulterError) { if (err instanceof multer.MulterError) {
return res.status(400).json({ error: err.message }); res.status(400).json({ error: err.message });
} else if (err) { } else if (err) {
return res.status(500).json({ error: 'Internal server error during file upload.' }); res.status(500).json({ error: 'Internal server error during file upload.' });
} }
if (!multiple && !req.file) { if (!multiple && !req.file) {
return res.status(400).json({ error: `No file uploaded under field "${fieldName}"` }); res.status(400).json({ error: `No file uploaded under field "${fieldName}"` });
} }
const result = req.file const result = req.file