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

Implement download_file in widget driver #12931

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/stores/widgets/StopGapWidgetDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { navigateToPermalink } from "../../utils/permalinks/navigator";
import { SdkContextClass } from "../../contexts/SDKContext";
import { ModuleRunner } from "../../modules/ModuleRunner";
import SettingsStore from "../../settings/SettingsStore";
import { Media } from "../../customisations/Media";

// TODO: Purge this from the universe

Expand Down Expand Up @@ -679,4 +680,18 @@ export class StopGapWidgetDriver extends WidgetDriver {

return { contentUri: uploadResult.content_uri };
}

/**
* Download a file from the media repository on the homeserver.
*
* @param contentUri - the MXC URI of the file to download
* @returns an object with: file - response contents as Blob
*/
public async downloadFile(contentUri: string): Promise<{ file: XMLHttpRequestBodyInit }> {
const client = MatrixClientPeg.safeGet();
const media = new Media({mxc: contentUri}, client);
const response = await media.downloadSource();
const blob = await response.blob();
return { file: blob };
}
}
29 changes: 29 additions & 0 deletions test/stores/widgets/StopGapWidgetDriver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { mocked, MockedObject } from "jest-mock";
import fetchMockJest from "fetch-mock-jest";
import {
MatrixClient,
ClientEvent,
Expand Down Expand Up @@ -616,4 +617,32 @@
expect(client.uploadContent).toHaveBeenCalledWith("data");
});
});

describe("downloadFile", () => {
let driver: WidgetDriver;

beforeEach(() => {
driver = mkDefaultDriver();
});

it("should download a file and return the blob", async () => {
// eslint-disable-next-line no-restricted-properties
client.mxcUrlToHttp.mockImplementation((mxcUrl) => {
if (mxcUrl === "mxc://example.com/test_file") {
return "https://example.com/_matrix/media/v3/download/test_file";
}

return null;
});

fetchMockJest.get("https://example.com/_matrix/media/v3/download/test_file", "test contents");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this be

Suggested change
return "https://example.com/_matrix/media/v3/download/test_file";
}
return null;
});
fetchMockJest.get("https://example.com/_matrix/media/v3/download/test_file", "test contents");
return "https://example.com/_matrix/media/v3/download/example.com/test_file";
}
return null;
});
fetchMockJest.get("https://example.com/_matrix/media/v3/download/example.com/test_file", "test contents");

it might not be obvious why this calls the deprecated API, but maintainers will know better than me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this be

Done

it might not be obvious why this calls the deprecated API, but maintainers will know better than me.

The implementation relies on Media . Not sure if the test should mock it away or not 🤷


const result = await driver.downloadFile("mxc://example.com/test_file");

Check failure on line 640 in test/stores/widgets/StopGapWidgetDriver-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Property 'downloadFile' does not exist on type 'WidgetDriver'.

Check failure on line 640 in test/stores/widgets/StopGapWidgetDriver-test.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

Property 'downloadFile' does not exist on type 'WidgetDriver'.
// A type test is impossible here because of
// https://github.com/jefflau/jest-fetch-mock/issues/209
// Tell TypeScript that file is a blob.
const file = result.file as Blob;
await expect(file.text()).resolves.toEqual("test contents");
});
});
});
Loading