This repository was archived by the owner on Sep 11, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +53
-1
lines changed
Expand file tree Collapse file tree 4 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change @@ -73,6 +73,7 @@ import { navigateToPermalink } from "../../utils/permalinks/navigator";
7373import { SdkContextClass } from "../../contexts/SDKContext" ;
7474import { ModuleRunner } from "../../modules/ModuleRunner" ;
7575import 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}
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ limitations under the License.
1515*/
1616
1717import { mocked , MockedObject } from "jest-mock" ;
18+ import fetchMockJest from "fetch-mock-jest" ;
1819import {
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} ) ;
Original file line number Diff line number Diff 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+
70417049md5@^2.3.0 :
70427050 version "2.3.0"
70437051 resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
You can’t perform that action at this time.
0 commit comments