Skip to content

Commit c3e5367

Browse files
RiotRobotdbkr
andauthored
Fix downloaded attachments not being decrypted (#30433) (#30434)
* Fix downloaded attachments not being decrypted Fixes #30339 * Import order (cherry picked from commit 1e15a32) Co-authored-by: David Baker <[email protected]>
1 parent 5a4b541 commit c3e5367

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/hooks/useDownloadMedia.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ export function useDownloadMedia(url: string, fileName?: string, mxEvent?: Matri
5959
return downloadBlob(blobRef.current);
6060
}
6161

62-
const res = await fetch(url);
63-
if (!res.ok) {
64-
throw parseErrorResponse(res, await res.text());
62+
// We must download via the mediaEventHelper if given as the file may need decryption.
63+
if (mediaEventHelper) {
64+
blobRef.current = await mediaEventHelper.sourceBlob.value;
65+
} else {
66+
const res = await fetch(url);
67+
if (!res.ok) {
68+
throw parseErrorResponse(res, await res.text());
69+
}
70+
71+
blobRef.current = await res.blob();
6572
}
6673

67-
const blob = await res.blob();
68-
blobRef.current = blob;
69-
70-
await downloadBlob(blob);
74+
await downloadBlob(blobRef.current);
7175
} catch (e) {
7276
showError(e);
7377
}

test/unit-tests/components/views/elements/ImageView-test.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import React from "react";
1010
import { mocked } from "jest-mock";
1111
import { render, fireEvent, waitFor } from "jest-matrix-react";
1212
import fetchMock from "fetch-mock-jest";
13+
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
1314

1415
import ImageView from "../../../../../src/components/views/elements/ImageView";
1516
import { FileDownloader } from "../../../../../src/utils/FileDownloader";
1617
import Modal from "../../../../../src/Modal";
1718
import ErrorDialog from "../../../../../src/components/views/dialogs/ErrorDialog";
19+
import { stubClient } from "../../../../test-utils";
1820

1921
jest.mock("../../../../../src/utils/FileDownloader");
2022

@@ -44,6 +46,39 @@ describe("<ImageView />", () => {
4446
expect(fetchMock).toHaveFetched("https://example.com/image.png");
4547
});
4648

49+
it("should use event as download source if given", async () => {
50+
stubClient();
51+
52+
const event = new MatrixEvent({
53+
event_id: "$eventId",
54+
type: "m.image",
55+
content: {
56+
body: "fromEvent.png",
57+
url: "mxc://test.dummy/fromEvent.png",
58+
file_name: "filename.png",
59+
},
60+
origin_server_ts: new Date(2000, 0, 1, 0, 0, 0, 0).getTime(),
61+
});
62+
63+
fetchMock.get("http://this.is.a.url/test.dummy/fromEvent.png", "TESTFILE");
64+
const { getByRole } = render(
65+
<ImageView
66+
src="https://test.dummy/fromSrc.png"
67+
name="fromName.png"
68+
onFinished={jest.fn()}
69+
mxEvent={event}
70+
/>,
71+
);
72+
fireEvent.click(getByRole("button", { name: "Download" }));
73+
await waitFor(() =>
74+
expect(mocked(FileDownloader).mock.instances[0].download).toHaveBeenCalledWith({
75+
blob: expect.anything(),
76+
name: "fromEvent.png",
77+
}),
78+
);
79+
expect(fetchMock).toHaveFetched("http://this.is.a.url/test.dummy/fromEvent.png");
80+
});
81+
4782
it("should start download on Ctrl+S", async () => {
4883
fetchMock.get("https://example.com/image.png", "TESTFILE");
4984

0 commit comments

Comments
 (0)