Fix: Improve PDF MIME type detection for uploads
- Added support for multiple PDF MIME types (application/x-pdf, etc.) - Added fallback to file extension validation (.pdf) - Added detailed logging for debugging upload issues - Resolves 'Only PDF files are allowed' error for valid PDFs
This commit is contained in:
parent
e5c04013bb
commit
9ab091966c
1 changed files with 23 additions and 3 deletions
|
|
@ -32,11 +32,31 @@ const upload = multer({
|
||||||
fileSize: parseInt(process.env['MAX_FILE_SIZE'] || '262144000'), // 250MB default
|
fileSize: parseInt(process.env['MAX_FILE_SIZE'] || '262144000'), // 250MB default
|
||||||
},
|
},
|
||||||
fileFilter: (req, file, cb) => {
|
fileFilter: (req, file, cb) => {
|
||||||
// Only allow PDF files
|
// Allow PDF files with multiple possible MIME types
|
||||||
if (file.mimetype === 'application/pdf') {
|
const allowedMimeTypes = [
|
||||||
|
'application/pdf',
|
||||||
|
'application/x-pdf',
|
||||||
|
'application/acrobat',
|
||||||
|
'applications/vnd.pdf',
|
||||||
|
'text/pdf',
|
||||||
|
'text/x-pdf'
|
||||||
|
];
|
||||||
|
|
||||||
|
// Check MIME type or file extension
|
||||||
|
const isValidPDF = allowedMimeTypes.includes(file.mimetype) ||
|
||||||
|
file.originalname.toLowerCase().endsWith('.pdf');
|
||||||
|
|
||||||
|
// Log for debugging
|
||||||
|
console.log('Upload attempt:', {
|
||||||
|
mimetype: file.mimetype,
|
||||||
|
filename: file.originalname,
|
||||||
|
isValid: isValidPDF
|
||||||
|
});
|
||||||
|
|
||||||
|
if (isValidPDF) {
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
} else {
|
} else {
|
||||||
cb(new Error('Only PDF files are allowed'));
|
cb(new Error(`Only PDF files are allowed. Received: ${file.mimetype} for file: ${file.originalname}`));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue