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

Commit 19f8b44

Browse files
Implement download_file in widget driver (#12931)
* Implement download_file in widget driver Signed-off-by: Michael Weimann <[email protected]> * Fix test URIs Signed-off-by: Michael Weimann <[email protected]> * Use download-file branch as widget-api source Signed-off-by: Michael Weimann <[email protected]> * bump matrix-widget-api to 1.9.0 Signed-off-by: Kim Brose <[email protected]> * prettier Signed-off-by: Kim Brose <[email protected]> --------- Signed-off-by: Michael Weimann <[email protected]> Signed-off-by: Kim Brose <[email protected]> Co-authored-by: Kim Brose <[email protected]>
1 parent 2a450c0 commit 19f8b44

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"matrix-encrypt-attachment": "^1.0.3",
116116
"matrix-events-sdk": "0.0.1",
117117
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop",
118-
"matrix-widget-api": "^1.8.2",
118+
"matrix-widget-api": "^1.9.0",
119119
"memoize-one": "^6.0.0",
120120
"minimist": "^1.2.5",
121121
"oidc-client-ts": "^3.0.1",

src/stores/widgets/StopGapWidgetDriver.ts

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import { navigateToPermalink } from "../../utils/permalinks/navigator";
7373
import { SdkContextClass } from "../../contexts/SDKContext";
7474
import { ModuleRunner } from "../../modules/ModuleRunner";
7575
import SettingsStore from "../../settings/SettingsStore";
76+
import { Media } from "../../customisations/Media";
7677

7778
// TODO: Purge this from the universe
7879

@@ -679,4 +680,18 @@ export class StopGapWidgetDriver extends WidgetDriver {
679680

680681
return { contentUri: uploadResult.content_uri };
681682
}
683+
684+
/**
685+
* Download a file from the media repository on the homeserver.
686+
*
687+
* @param contentUri - the MXC URI of the file to download
688+
* @returns an object with: file - response contents as Blob
689+
*/
690+
public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> {
691+
const client = MatrixClientPeg.safeGet();
692+
const media = new Media({ mxc: contentUri }, client);
693+
const response = await media.downloadSource();
694+
const blob = await response.blob();
695+
return { file: blob };
696+
}
682697
}

test/stores/widgets/StopGapWidgetDriver-test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import { mocked, MockedObject } from "jest-mock";
18+
import fetchMockJest from "fetch-mock-jest";
1819
import {
1920
MatrixClient,
2021
ClientEvent,
@@ -616,4 +617,32 @@ describe("StopGapWidgetDriver", () => {
616617
expect(client.uploadContent).toHaveBeenCalledWith("data");
617618
});
618619
});
620+
621+
describe("downloadFile", () => {
622+
let driver: WidgetDriver;
623+
624+
beforeEach(() => {
625+
driver = mkDefaultDriver();
626+
});
627+
628+
it("should download a file and return the blob", async () => {
629+
// eslint-disable-next-line no-restricted-properties
630+
client.mxcUrlToHttp.mockImplementation((mxcUrl) => {
631+
if (mxcUrl === "mxc://example.com/test_file") {
632+
return "https://example.com/_matrix/media/v3/download/example.com/test_file";
633+
}
634+
635+
return null;
636+
});
637+
638+
fetchMockJest.get("https://example.com/_matrix/media/v3/download/example.com/test_file", "test contents");
639+
640+
const result = await driver.downloadFile("mxc://example.com/test_file");
641+
// A type test is impossible here because of
642+
// https://github.com/jefflau/jest-fetch-mock/issues/209
643+
// Tell TypeScript that file is a blob.
644+
const file = result.file as Blob;
645+
await expect(file.text()).resolves.toEqual("test contents");
646+
});
647+
});
619648
});

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -7038,6 +7038,14 @@ matrix-widget-api@^1.8.2:
70387038
"@types/events" "^3.0.0"
70397039
events "^3.2.0"
70407040

7041+
matrix-widget-api@^1.9.0:
7042+
version "1.9.0"
7043+
resolved "https://registry.yarnpkg.com/matrix-widget-api/-/matrix-widget-api-1.9.0.tgz#884136b405bd3c56e4ea285095c9e01ec52b6b1f"
7044+
integrity sha512-au8mqralNDqrEvaVAkU37bXOb8I9SCe+ACdPk11QWw58FKstVq31q2wRz+qWA6J+42KJ6s1DggWbG/S3fEs3jw==
7045+
dependencies:
7046+
"@types/events" "^3.0.0"
7047+
events "^3.2.0"
7048+
70417049
md5@^2.3.0:
70427050
version "2.3.0"
70437051
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"

0 commit comments

Comments
 (0)