fix: 统一文档解析文件格式校验

- 统一知识库和工作流支持格式并增加前后端上传拦截

- 拒绝 XLS 与伪装 XLSX,避免空内容解析成功
This commit is contained in:
2026-09-02 19:15:01 +08:00
parent 36acf37976
commit c3673ece46
24 changed files with 658 additions and 101 deletions

View File

@@ -0,0 +1,40 @@
import { mount } from '@vue/test-utils';
import { describe, expect, it, vi } from 'vitest';
import DragFileUpload from './DragFileUpload.vue';
vi.mock('@easyflow/hooks', () => ({
useAppConfig: () => ({ apiURL: '' }),
}));
vi.mock('@easyflow/locales', () => ({
$t: (key: string) => key,
}));
vi.mock('@easyflow/stores', () => ({
useAccessStore: () => ({ accessToken: 'test-token' }),
}));
vi.mock('#/locales', () => ({
$t: (key: string, params?: Record<string, string>) =>
key === 'message.upload.unsupportedFileType'
? `${params?.fileName}”格式不支持,仅支持 ${params?.types} 文件。`
: key,
}));
describe('drag file upload', () => {
it('uses the shared document formats and rejects XLS drops', async () => {
const wrapper = mount(DragFileUpload);
const upload = wrapper.getComponent({ name: 'ElUpload' });
expect(upload.props('accept')).toBe('.txt,.pdf,.docx,.md,.pptx,.xlsx,.csv');
const beforeUpload = upload.props('beforeUpload') as (
file: File,
) => Promise<boolean>;
await expect(beforeUpload(new File(['legacy'], 'test.xls'))).resolves.toBe(
false,
);
});
});

View File

@@ -10,6 +10,7 @@ import { useAccessStore } from '@easyflow/stores';
import { UploadFilled } from '@element-plus/icons-vue';
import { ElIcon, ElMessage, ElUpload } from 'element-plus';
import { DocumentParseFilePolicy } from '#/utils/document-parse-file-policy';
import {
normalizeUploadError,
resolveUploadPath,
@@ -28,7 +29,6 @@ const props = defineProps({
const emit = defineEmits(['success', 'error', 'onChange']);
const MAX_FILE_SIZE_BYTES = 100 * 1024 * 1024;
const ACCEPTED_FILE_TYPES = '.txt,.pdf,.docx,.md,.pptx,.xlsx';
const accessStore = useAccessStore();
const headers = ref({
'easyflow-token': accessStore.accessToken,
@@ -54,12 +54,18 @@ const handleError: UploadProps['onError'] = (error) => {
emit('error', normalizedError);
};
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
if (rawFile.size <= MAX_FILE_SIZE_BYTES) {
return true;
const beforeUpload: UploadProps['beforeUpload'] = async (rawFile) => {
try {
await DocumentParseFilePolicy.validateFiles([rawFile]);
} catch (error: any) {
ElMessage.warning(error?.message || $t('message.notSupported'));
return false;
}
ElMessage.warning($t('message.upload.fileTooLarge'));
return false;
if (rawFile.size > MAX_FILE_SIZE_BYTES) {
ElMessage.warning($t('message.upload.fileTooLarge'));
return false;
}
return true;
};
// 文件状态变化回调
@@ -97,7 +103,7 @@ defineExpose({
drag
:headers="headers"
:action="`${apiURL}${props.action}`"
:accept="ACCEPTED_FILE_TYPES"
:accept="DocumentParseFilePolicy.accept"
:before-upload="beforeUpload"
:on-success="handleSuccess"
:on-error="handleError"
@@ -111,7 +117,9 @@ defineExpose({
<div class="flex flex-col gap-1">
<span class="text-base">{{ $t('message.upload.title') }}</span>
<span class="text-muted-foreground text-sm">{{
$t('message.upload.description')
$t('message.upload.description', {
types: DocumentParseFilePolicy.supportedTypeLabel,
})
}}</span>
</div>
</ElUpload>