Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 4.x gif thumb url base64 type should be corrected #44129

Merged
merged 5 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ exports[`MonthPicker and WeekPicker render WeekPicker 1`] = `
</thead>
<tbody>
<tr
class="ant-picker-week-panel-row ant-picker-week-panel-row-selected"
class="ant-picker-week-panel-row"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥这个会变?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react-component/picker#664 貌似是这个,修复了 4.x 的

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zombieJ 看看是否是预期行为。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,预期的。rc-picker patch 也是从 v5 里 pick 的

>
<td
class="ant-picker-cell ant-picker-cell-week"
Expand Down
28 changes: 28 additions & 0 deletions components/upload/__tests__/uploadlist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ describe('Upload List', () => {
// jsdom not support `createObjectURL` yet. Let's handle this.
const originCreateObjectURL = window.URL.createObjectURL;
window.URL.createObjectURL = jest.fn(() => '');
const originRevokeObjectURL = window.URL.revokeObjectURL;
window.URL.revokeObjectURL = jest.fn(() => '');

// Mock dom
let size = { width: 0, height: 0 };
Expand Down Expand Up @@ -88,6 +90,7 @@ describe('Upload List', () => {

afterAll(() => {
window.URL.createObjectURL = originCreateObjectURL;
window.URL.revokeObjectURL = originRevokeObjectURL;
mockWidthGet.mockRestore();
mockHeightGet.mockRestore();
mockSrcSet.mockRestore();
Expand Down Expand Up @@ -921,6 +924,31 @@ describe('Upload List', () => {
unmount();
});

it('upload gif file should be converted to the image/gif base64', async () => {
const mockFile = new File([''], 'foo.gif', {
type: 'image/gif',
});

const previewFunc = jest.fn(previewImage);

const { unmount } = render(
<Upload
fileList={[{ originFileObj: mockFile }] as UploadProps['fileList']}
previewFile={previewFunc}
locale={{ uploading: 'uploading' }}
listType="picture-card"
/>,
);

await waitFor(() => {
expect(previewFunc).toHaveBeenCalled();
});
await previewFunc(mockFile).then(dataUrl => {
expect(dataUrl).toEqual('data:image/gif;base64,');
});
unmount();
});

it("upload non image file shouldn't be converted to the base64", async () => {
const mockFile = new File([''], 'foo.7z', {
type: 'application/x-7z-compressed',
Expand Down
12 changes: 9 additions & 3 deletions components/upload/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ export function previewImage(file: File | Blob): Promise<string> {
ctx!.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
const dataURL = canvas.toDataURL();
document.body.removeChild(canvas);

window.URL.revokeObjectURL(img.src);
resolve(dataURL);
};
img.crossOrigin = 'anonymous';
if (file.type.startsWith('image/svg+xml')) {
const reader = new FileReader();
reader.addEventListener('load', () => {
reader.onload = () => {
if (reader.result) img.src = reader.result as string;
});
};
reader.readAsDataURL(file);
} else if (file.type.startsWith('image/gif')) {
const reader = new FileReader();
reader.onload = () => {
if (reader.result) resolve(reader.result as string);
};
reader.readAsDataURL(file);
} else {
img.src = window.URL.createObjectURL(file);
Expand Down