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

Commit 949557b

Browse files
authored
Fall back from server generated thumbnail to original image (#10853)
* Add test for falling back from thumbnail to original * Fall back from server generated thumbnail to original image
1 parent 82e3203 commit 949557b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/components/views/messages/MImageBody.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
165165
};
166166

167167
private onImageError = (): void => {
168+
// If the thumbnail failed to load then try again using the contentUrl
169+
if (this.state.thumbUrl) {
170+
this.setState({
171+
thumbUrl: null,
172+
});
173+
return;
174+
}
175+
168176
this.clearBlurhashTimeout();
169177
this.setState({
170178
imgError: true,

test/components/views/messages/MImageBody-test.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import React from "react";
1818
import { fireEvent, render, screen } from "@testing-library/react";
19-
import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
19+
import { EventType, getHttpUriForMxc, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
2020
import fetchMock from "fetch-mock-jest";
2121
import encrypt from "matrix-encrypt-attachment";
2222
import { mocked } from "jest-mock";
@@ -171,4 +171,39 @@ describe("<MImageBody/>", () => {
171171
expect(screen.getByRole("progressbar")).toBeInTheDocument();
172172
});
173173
});
174+
175+
it("should fall back to /download/ if /thumbnail/ fails", async () => {
176+
const thumbUrl = "https://server/_matrix/media/r0/thumbnail/server/image?width=800&height=600&method=scale";
177+
const downloadUrl = "https://server/_matrix/media/r0/download/server/image";
178+
// eslint-disable-next-line no-restricted-properties
179+
cli.mxcUrlToHttp.mockImplementation(
180+
(mxcUrl: string, width?: number, height?: number, resizeMethod?: string, allowDirectLinks?: boolean) => {
181+
return getHttpUriForMxc("https://server", mxcUrl, width, height, resizeMethod, allowDirectLinks);
182+
},
183+
);
184+
185+
const event = new MatrixEvent({
186+
room_id: "!room:server",
187+
sender: userId,
188+
type: EventType.RoomMessage,
189+
content: {
190+
body: "alt for a test image",
191+
info: {
192+
w: 40,
193+
h: 50,
194+
},
195+
url: "mxc://server/image",
196+
},
197+
});
198+
199+
const { container } = render(
200+
<MImageBody {...props} mxEvent={event} mediaEventHelper={new MediaEventHelper(event)} />,
201+
);
202+
203+
const img = container.querySelector(".mx_MImageBody_thumbnail")!;
204+
expect(img).toHaveProperty("src", thumbUrl);
205+
206+
fireEvent.error(img);
207+
expect(img).toHaveProperty("src", downloadUrl);
208+
});
174209
});

0 commit comments

Comments
 (0)