Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fall back from server generated thumbnail to original image #10853

Merged
merged 3 commits into from
May 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/views/messages/MImageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
};

private onImageError = (): void => {
// If the thumbnail failed to load then try again using the contentUrl
if (this.state.thumbUrl) {
this.setState({
thumbUrl: null,
});
return;
}

this.clearBlurhashTimeout();
this.setState({
imgError: true,
Expand Down
37 changes: 36 additions & 1 deletion test/components/views/messages/MImageBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

import React from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { EventType, getHttpUriForMxc, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import fetchMock from "fetch-mock-jest";
import encrypt from "matrix-encrypt-attachment";
import { mocked } from "jest-mock";
Expand Down Expand Up @@ -171,4 +171,39 @@ describe("<MImageBody/>", () => {
expect(screen.getByRole("progressbar")).toBeInTheDocument();
});
});

it("should fall back to /download/ if /thumbnail/ fails", async () => {
const thumbUrl = "https://server/_matrix/media/r0/thumbnail/server/image?width=800&height=600&method=scale";
const downloadUrl = "https://server/_matrix/media/r0/download/server/image";
// eslint-disable-next-line no-restricted-properties
cli.mxcUrlToHttp.mockImplementation(
(mxcUrl: string, width?: number, height?: number, resizeMethod?: string, allowDirectLinks?: boolean) => {
return getHttpUriForMxc("https://server", mxcUrl, width, height, resizeMethod, allowDirectLinks);
},
);

const event = new MatrixEvent({
room_id: "!room:server",
sender: userId,
type: EventType.RoomMessage,
content: {
body: "alt for a test image",
info: {
w: 40,
h: 50,
},
url: "mxc://server/image",
},
});

const { container } = render(
<MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />,
);

const img = container.querySelector(".mx_MImageBody_thumbnail")!;
expect(img).toHaveProperty("src", thumbUrl);

fireEvent.error(img);
expect(img).toHaveProperty("src", downloadUrl);
});
});