This repository was archived by the owner on Sep 11, 2024. It is now read-only.
File tree 4 files changed +53
-1
lines changed
4 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 115
115
"matrix-encrypt-attachment" : " ^1.0.3" ,
116
116
"matrix-events-sdk" : " 0.0.1" ,
117
117
"matrix-js-sdk" : " github:matrix-org/matrix-js-sdk#develop" ,
118
- "matrix-widget-api" : " ^1.8.2 " ,
118
+ "matrix-widget-api" : " ^1.9.0 " ,
119
119
"memoize-one" : " ^6.0.0" ,
120
120
"minimist" : " ^1.2.5" ,
121
121
"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";
73
73
import { SdkContextClass } from "../../contexts/SDKContext" ;
74
74
import { ModuleRunner } from "../../modules/ModuleRunner" ;
75
75
import SettingsStore from "../../settings/SettingsStore" ;
76
+ import { Media } from "../../customisations/Media" ;
76
77
77
78
// TODO: Purge this from the universe
78
79
@@ -679,4 +680,18 @@ export class StopGapWidgetDriver extends WidgetDriver {
679
680
680
681
return { contentUri : uploadResult . content_uri } ;
681
682
}
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
+ }
682
697
}
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ limitations under the License.
15
15
*/
16
16
17
17
import { mocked , MockedObject } from "jest-mock" ;
18
+ import fetchMockJest from "fetch-mock-jest" ;
18
19
import {
19
20
MatrixClient ,
20
21
ClientEvent ,
@@ -616,4 +617,32 @@ describe("StopGapWidgetDriver", () => {
616
617
expect ( client . uploadContent ) . toHaveBeenCalledWith ( "data" ) ;
617
618
} ) ;
618
619
} ) ;
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
+ } ) ;
619
648
} ) ;
Original file line number Diff line number Diff line change @@ -7038,6 +7038,14 @@ matrix-widget-api@^1.8.2:
7038
7038
" @types/events" " ^3.0.0"
7039
7039
events "^3.2.0"
7040
7040
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
+
7041
7049
md5@^2.3.0 :
7042
7050
version "2.3.0"
7043
7051
resolved "https://registry.yarnpkg.com/md5/-/md5-2.3.0.tgz#c3da9a6aae3a30b46b7b0c349b87b110dc3bda4f"
You can’t perform that action at this time.
0 commit comments