diff --git a/packages/components/src/utils.test.ts b/packages/components/src/utils.test.ts index ba8238ee049..32529f10fdc 100644 --- a/packages/components/src/utils.test.ts +++ b/packages/components/src/utils.test.ts @@ -1,4 +1,10 @@ -import { removeInvalidImageMarkdown, convertRequireToImport, COMMONJS_REQUIRE_REGEX, IMPORT_EXTRACTION_REGEX } from './utils' +import { + removeInvalidImageMarkdown, + convertRequireToImport, + COMMONJS_REQUIRE_REGEX, + IMPORT_EXTRACTION_REGEX, + mapExtToInputField +} from './utils' describe('removeInvalidImageMarkdown', () => { describe('strips non-http/https image markdown', () => { @@ -229,3 +235,35 @@ describe('Import extraction regex (utils.ts line 1596 pattern)', () => { expect(extractModules('console.log("hello")')).toEqual([]) }) }) + +describe('mapExtToInputField', () => { + it('maps known lowercase extensions to their input field', () => { + expect(mapExtToInputField('.pdf')).toBe('pdfFile') + expect(mapExtToInputField('.txt')).toBe('txtFile') + expect(mapExtToInputField('.json')).toBe('jsonFile') + expect(mapExtToInputField('.csv')).toBe('csvFile') + expect(mapExtToInputField('.docx')).toBe('docxFile') + }) + + it('maps uppercase extensions to the same input field (case-insensitive)', () => { + expect(mapExtToInputField('.PDF')).toBe('pdfFile') + expect(mapExtToInputField('.TXT')).toBe('txtFile') + expect(mapExtToInputField('.JSON')).toBe('jsonFile') + expect(mapExtToInputField('.DOCX')).toBe('docxFile') + }) + + it('maps mixed-case extensions to the same input field', () => { + expect(mapExtToInputField('.Pdf')).toBe('pdfFile') + expect(mapExtToInputField('.Csv')).toBe('csvFile') + }) + + it('falls back to txtFile for unknown extensions', () => { + expect(mapExtToInputField('.unknown')).toBe('txtFile') + }) + + it('falls back to txtFile for empty or nullish extensions', () => { + expect(mapExtToInputField('')).toBe('txtFile') + expect(mapExtToInputField(undefined)).toBe('txtFile') + expect(mapExtToInputField(null as unknown as string)).toBe('txtFile') + }) +}) diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index c89210a59f5..da7a58f9da5 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -1068,11 +1068,11 @@ export const getVersion: () => Promise<{ version: string }> = async () => { /** * Map Ext to InputField - * @param {string} ext + * @param {string} [ext] File extension (case-insensitive), e.g. ".pdf" * @returns {string} */ -export const mapExtToInputField = (ext: string) => { - switch (ext) { +export const mapExtToInputField = (ext?: string) => { + switch (ext?.toLowerCase()) { case '.txt': return 'txtFile' case '.pdf': diff --git a/packages/server/src/utils/createAttachment.ts b/packages/server/src/utils/createAttachment.ts index 2daec1e2e1d..2cc759c2bf2 100644 --- a/packages/server/src/utils/createAttachment.ts +++ b/packages/server/src/utils/createAttachment.ts @@ -177,7 +177,7 @@ export const createFileAttachment = async (req: Request) => { if (fileInputFieldFromExt !== 'txtFile') { fileInputField = fileInputFieldFromExt } else if (fileInputFieldFromMimeType !== 'txtFile') { - fileInputField = fileInputFieldFromExt + fileInputField = fileInputFieldFromMimeType } await removeSpecificFileFromUpload(file.path ?? file.key)