mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-16 20:56:35 +01:00
🐍 refactor: Normalize Non-Standard Browser MIME Type Aliases in inferMimeType (#12240)
* 🐛 fix: Normalize non-standard browser MIME types in inferMimeType macOS Chrome/Firefox report .py files as text/x-python-script instead of text/x-python, causing client-side validation to reject Python file uploads. inferMimeType now normalizes known MIME type aliases before returning, so non-standard variants match the accepted regex patterns. * 🧪 test: Add tests for MIME type alias normalization in inferMimeType * 🐛 fix: Restore JSDoc params and make mimeTypeAliases immutable * 🧪 test: Add checkType integration tests, remove redundant DragDropModal tests
This commit is contained in:
parent
8318446704
commit
7c39a45944
2 changed files with 50 additions and 7 deletions
|
|
@ -1,15 +1,52 @@
|
|||
import type { FileConfig } from './types/files';
|
||||
import {
|
||||
fileConfig as baseFileConfig,
|
||||
documentParserMimeTypes,
|
||||
getEndpointFileConfig,
|
||||
mergeFileConfig,
|
||||
applicationMimeTypes,
|
||||
defaultOCRMimeTypes,
|
||||
documentParserMimeTypes,
|
||||
supportedMimeTypes,
|
||||
mergeFileConfig,
|
||||
inferMimeType,
|
||||
textMimeTypes,
|
||||
} from './file-config';
|
||||
import { EModelEndpoint } from './schemas';
|
||||
|
||||
describe('inferMimeType', () => {
|
||||
it('should normalize text/x-python-script to text/x-python', () => {
|
||||
expect(inferMimeType('test.py', 'text/x-python-script')).toBe('text/x-python');
|
||||
});
|
||||
|
||||
it('should return a type that matches textMimeTypes after normalization', () => {
|
||||
const normalized = inferMimeType('test.py', 'text/x-python-script');
|
||||
expect(textMimeTypes.test(normalized)).toBe(true);
|
||||
});
|
||||
|
||||
it('should pass through standard browser types unchanged', () => {
|
||||
expect(inferMimeType('test.py', 'text/x-python')).toBe('text/x-python');
|
||||
expect(inferMimeType('doc.pdf', 'application/pdf')).toBe('application/pdf');
|
||||
});
|
||||
|
||||
it('should infer from extension when browser type is empty', () => {
|
||||
expect(inferMimeType('test.py', '')).toBe('text/x-python');
|
||||
expect(inferMimeType('code.js', '')).toBe('text/javascript');
|
||||
expect(inferMimeType('photo.heic', '')).toBe('image/heic');
|
||||
});
|
||||
|
||||
it('should return empty string for unknown extension with no browser type', () => {
|
||||
expect(inferMimeType('file.xyz', '')).toBe('');
|
||||
});
|
||||
|
||||
it('should produce a type accepted by checkType after normalizing text/x-python-script', () => {
|
||||
const normalized = inferMimeType('test.py', 'text/x-python-script');
|
||||
expect(baseFileConfig.checkType(normalized)).toBe(true);
|
||||
});
|
||||
|
||||
it('should reject raw text/x-python-script without normalization', () => {
|
||||
expect(baseFileConfig.checkType('text/x-python-script')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applicationMimeTypes', () => {
|
||||
const odfTypes = [
|
||||
'application/vnd.oasis.opendocument.text',
|
||||
|
|
|
|||
|
|
@ -357,15 +357,21 @@ export const imageTypeMapping: { [key: string]: string } = {
|
|||
heif: 'image/heif',
|
||||
};
|
||||
|
||||
/** Normalizes non-standard MIME types that browsers may report to their canonical forms */
|
||||
export const mimeTypeAliases: Readonly<Record<string, string>> = {
|
||||
'text/x-python-script': 'text/x-python',
|
||||
};
|
||||
|
||||
/**
|
||||
* Infers the MIME type from a file's extension when the browser doesn't recognize it
|
||||
* @param fileName - The name of the file including extension
|
||||
* @param currentType - The current MIME type reported by the browser (may be empty)
|
||||
* @returns The inferred MIME type if browser didn't provide one, otherwise the original type
|
||||
* Infers the MIME type from a file's extension when the browser doesn't recognize it,
|
||||
* and normalizes known non-standard MIME type aliases to their canonical forms.
|
||||
* @param fileName - The file name including its extension
|
||||
* @param currentType - The MIME type reported by the browser (may be empty string)
|
||||
* @returns The normalized or inferred MIME type; empty string if unresolvable
|
||||
*/
|
||||
export function inferMimeType(fileName: string, currentType: string): string {
|
||||
if (currentType) {
|
||||
return currentType;
|
||||
return mimeTypeAliases[currentType] ?? currentType;
|
||||
}
|
||||
|
||||
const extension = fileName.split('.').pop()?.toLowerCase() ?? '';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue