fix: upload 识别图片类型逻辑错误 (#21473)

* fix: upload 识别图片类型逻辑错误

当 uploadFile 有类型时,直接对 类型 进行判断即可

* test: 增加识别文件类型的测试用例
This commit is contained in:
程成 2020-02-19 20:12:44 +08:00 committed by GitHub
parent b38ab0653c
commit c968184348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 4 deletions

View File

@ -498,4 +498,12 @@ describe('Upload', () => {
);
errorSpy.mockRestore();
});
it('it should be treated as file but not an image', () => {
const file = { status: 'done', uid: '-1', type: 'video/mp4', url: 'https://zos.alipayobjects.com/rmsportal/IQKRngzUuFzJzGzRJXUs.png' };
const wrapper = mount(
<Upload listType="picture-card" fileList={[file]} />,
);
expect(wrapper.find('img').length).toBe(0);
});
});

View File

@ -42,11 +42,11 @@ const extname = (url: string = '') => {
return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
};
const isImageFileType = (type: string): boolean => !!type && type.indexOf('image/') === 0;
const isImageFileType = (type: string): boolean => type.indexOf('image/') === 0;
export const isImageUrl = (file: UploadFile): boolean => {
if (isImageFileType(file.type)) {
return true;
if (file.type) {
return isImageFileType(file.type);
}
const url: string = (file.thumbUrl || file.url) as string;
const extension = extname(url);
@ -70,7 +70,7 @@ export const isImageUrl = (file: UploadFile): boolean => {
const MEASURE_SIZE = 200;
export function previewImage(file: File | Blob): Promise<string> {
return new Promise(resolve => {
if (!isImageFileType(file.type)) {
if (!file.type || !isImageFileType(file.type)) {
resolve('');
return;
}