Commit a6885b33 by DadaVinqi Committed by GitHub

fix: normalize file extension to lowercase before parsing (#6996) (#7105)

Files uploaded with an uppercase or mixed-case extension (e.g. `123.PDF`,
`report.Docx`) failed to be parsed because the readFile worker matches the
extension against a lowercase switch (`'pdf'`, `'docx'`, ...), so an
uppercase suffix fell through to the default branch and threw
"... is not supported".

Normalize the extension to lowercase once at the lowest common entry point
`readFileContentByBuffer`, instead of at each business-layer call site. This
keeps business-layer behavior unchanged and covers all callers
(chat upload, external url, dataset, local file) in a single place.

Adds regression tests covering upper/mixed-case extensions.

Fixes #6996

Co-authored-by: DadaVinqi <DadaVinqi@users.noreply.github.com>
parent decb6d2f
......@@ -49,7 +49,7 @@ export const readFileContentByBuffer = async ({
teamId,
tmbId,
extension,
extension: rawExtension,
buffer,
encoding,
customPdfParse = false,
......@@ -74,6 +74,9 @@ export const readFileContentByBuffer = async ({
}): Promise<{
rawText: string;
}> => {
// 归一化扩展名为小写,避免大写/混合大小写后缀(如 .PDF)无法匹配解析器(#6996)
const extension = rawExtension.toLowerCase();
const parseMarkdownImages = (rawText: string) =>
parseMarkdownBase64Images(rawText, {
parseBase64: true,
......
......@@ -581,4 +581,41 @@ describe('readFileContentByBuffer', () => {
expect(result.rawText).toBe('text with');
expect(result.rawText).not.toContain('data:image/png;base64');
});
it('应将大写扩展名归一化为小写后再传给解析器(#6996)', async () => {
const buffer = Buffer.from('pdf content');
const result = await readFileContentByBuffer({
teamId,
tmbId,
extension: 'PDF',
buffer,
encoding: 'utf-8'
});
// 解析器应收到小写扩展名,从而命中对应分支而非报 "not supported"
expect(mockReadRawContentFromBuffer).toHaveBeenLastCalledWith(
expect.objectContaining({
extension: 'pdf'
})
);
expect(result.rawText).toBe('parsed-pdf-content');
});
it('应将混合大小写扩展名归一化为小写', async () => {
const buffer = Buffer.from('docx content');
await readFileContentByBuffer({
teamId,
tmbId,
extension: 'Docx',
buffer,
encoding: 'utf-8'
});
expect(mockReadRawContentFromBuffer).toHaveBeenLastCalledWith(
expect.objectContaining({
extension: 'docx'
})
);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment