> @julian said:
>
> @AMAARETS I don't think it's browser specific since the checks live on the backend.
>
> I'll try with a RAR file.
Since I didn't trust myself to identify the problem with the function for sure, I preferred to actually check it by printing the variable data to the terminal, I changed the function to this:
async function validateUploadedFileMime(uploadedFile) {
console.log('===== MIME Validation Start =====');
console.log('1. uploadedFile object metadata:');
console.log(' - Name:', uploadedFile.name);
console.log(' - Path:', uploadedFile.path);
console.log(' - Type (from request/browser):', uploadedFile.type);
console.log(' - Size:', uploadedFile.size);
const detected = await fileTypeFromFile(uploadedFile.path);
console.log('2. file-type library detection:', detected || 'null (could not detect by content)');
const mimeFromExt = mime.getType(uploadedFile.name);
console.log('3. mime library detection (by extension):', mimeFromExt || 'null (could not detect by extension)');
const detectedMimeType = detected ? detected.mime : mimeFromExt;
console.log('4. Final detectedMimeType to compare:', detectedMimeType);
const hasDetected = !!detectedMimeType;
const hasUploadedType = !!uploadedFile.type;
const isMismatch = detectedMimeType !== uploadedFile.type;
console.log('5. Conditions check:');
console.log(' - Has detected MIME?:', hasDetected);
console.log(' - Has uploaded type from request?:', hasUploadedType);
console.log(' - Is there a mismatch?:', isMismatch);
if (detectedMimeType && uploadedFile.type && detectedMimeType !== uploadedFile.type) {
console.error('❌ VALIDATION FAILED: MIME mismatch!');
console.error(` Expected (from file content/ext): "${detectedMimeType}"`);
console.error(` Received (from request/browser): "${uploadedFile.type}"`);
console.log('==================================');
throw new Error('[[error:invalid-mime-type]]');
}
console.log('✅ VALIDATION PASSED');
console.log('==================================');
}
And this is the output
===== MIME Validation Start =====
1. uploadedFile object metadata:
- Name: בדיקה-מחשב-מק.zip
- Path: /tmp/91c31e55fb2334122e3c21aace4755f3
- Type (from request/browser): application/x-zip-compressed
- Size: 204
2. file-type library detection:
{ ext: 'zip', mime: 'application/zip' }
3. mime library detection (by extension):
application/zip
4. Final detectedMimeType to compare:
application/zip
5. Conditions check:
- Has detected MIME?: true
- Has uploaded type from request?: true
- Is there a mismatch?: true
❌ VALIDATION FAILED: MIME mismatch!
Expected (from file content/ext): "application/zip"
Received (from request/browser): "application/x-zip-compressed"
==================================
As you can clearly see there is a mismatch of expectations between the browser and the server