Skip to content

Commit bb7b2d1

Browse files
authored
Merge pull request #3818 from element-hq/valere/msc_4039
msc4039 support b64 in addition to blob file download
2 parents 1fce220 + b5870c9 commit bb7b2d1

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Avatar.test.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { type FC, type PropsWithChildren } from "react";
1212
import { type WidgetApi } from "matrix-widget-api";
1313

1414
import { ClientContextProvider } from "./ClientContext";
15-
import { Avatar } from "./Avatar";
15+
import { Avatar, getAvatarFromWidgetAPI } from "./Avatar";
1616
import { mockMatrixRoomMember, mockRtcMembership } from "./utils/test";
1717
import { widget } from "./widget";
1818

@@ -178,3 +178,36 @@ test("should attempt to use widget API if running as a widget", async () => {
178178

179179
expect(widget!.api.downloadFile).toBeCalledWith(expectedMXCUrl);
180180
});
181+
182+
test("Supports download files as base64", async () => {
183+
const expectedMXCUrl = "mxc://example.org/alice-avatar";
184+
const expectedBase64 =
185+
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAADIElEQVR4nAAQA+/8ApxhEfFNuwna" +
186+
"+DO1pFMx5YDg6gb8p1WFkbFSox9H6r5c8jp1gxlHXrDfA/oQFi4A0gTXH9YBNgwRm12xO68QP6lv" +
187+
"ZLKH9qW1VM6kz6zA3T1Ui8J+Xbnh2BZ7oXDe/2gajzoA6j1JGotpz99xO+T2NR634Nhx3zhuera/" +
188+
"UdrpMLdEpwWXLnSqZRasGsrl93FjdTwRBMaqsx6vJksnPOmV9ttbXFIOb0XDGPbVythSC2n7P/bS" +
189+
"Zv0U0QqbBLk/5Wu1werYzAHiz11Bj8bEylQ92Pxvo+PwF6/KbGnIHTvGZkFzDkMnqz3g7Pw3NOSP" +
190+
"oV+qfyJuSI0AeZmrPejFQ8kzBSDWO8D7lr4+6ePRBRmZtKCf+fNjSCOyb5jqwhBnD2cycbJtQQbR" +
191+
"A4qdPG2ONfTPeQgi96+zT7grBI0JwvgFBceJdLJd4BX1VQIyY+j7OYueNWqEpf8iYgMj78I95eRt" +
192+
"nfPLwlxhVns84iL4Yvw8jDrB9vQi8ktpsdJOMiDwKrBGD3q56COD2oIA96CCBgiro4tkvkumZSAc" +
193+
"ZKXRLsziUFGytWJLaPjwnzXv2hicPy6k9AXsF3QkysOZAkB3m9XPpixhq9b0OKqV/zZx3L79o6wZ" +
194+
"Dr40J7sj7f+ARd545CP01r5omHt94tbnjgA46HsM2OhP+qQ882LN+Bhscq2WSHGSHT4J9MQcsWZP" +
195+
"2+N2LdPy61MN4/1++BJHmDcDLQBUEwLvjZp1fRfzxV7yirwIiOA7Vr8z+1yvS/pSkfUzkjswybOd" +
196+
"M5i0I8Q69MTXAKxqtR0/tyGkfCmHfupGASp/SAT9J8f3aQV+gDbpva592v4w8Cv5EMm7CzZPwThF" +
197+
"kgTChNPts7F03ccxpblfIz0EiAON1DKk71rX07BvDlLHY1ItPuqZ7hjy19jrAgl+QqEE1btHVA5R" +
198+
"uAnRXpEWc6rjARlJY5G1wbMk12rrqpr8rhR3YpFgLgOx4BtQ0D/hGe7KANSGBMQojmObId0asCmd" +
199+
"XzmnQI9P8QnwsO9vtqZlgIoU4g+f2/G8Q3/nVMX7dujniwEAAP//KmiQs7P8MeIAAAAASUVORK5C" +
200+
"YII=";
201+
const mockWidgetAPI = {
202+
downloadFile: vi.fn().mockImplementation(async (contentUri) => {
203+
if (contentUri !== expectedMXCUrl) {
204+
return Promise.reject(new Error("Unexpected content URI"));
205+
}
206+
return { file: expectedBase64 };
207+
}),
208+
} as unknown as WidgetApi;
209+
210+
const blob = await getAvatarFromWidgetAPI(mockWidgetAPI, expectedMXCUrl);
211+
212+
expect(blob).toBeInstanceOf(Blob);
213+
});

src/Avatar.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,23 @@ async function getAvatarFromServer(
173173
return blob;
174174
}
175175

176-
async function getAvatarFromWidgetAPI(
176+
// export for testing
177+
export async function getAvatarFromWidgetAPI(
177178
api: WidgetApi,
178179
src: string,
179180
): Promise<Blob> {
180181
const response = await api.downloadFile(src);
181182
const file = response.file;
182183

183184
// element-web sends a Blob, and the MSC4039 is considering changing the spec to strictly Blob, so only handling that
184-
if (!(file instanceof Blob)) {
185-
throw new Error("Downloaded file is not a Blob");
185+
if (file instanceof Blob) {
186+
return file;
187+
} else if (typeof file === "string") {
188+
// it is a base64 string
189+
const bytes = Uint8Array.from(atob(file), (c) => c.charCodeAt(0));
190+
return new Blob([bytes]);
186191
}
187-
188-
return file;
192+
throw new Error(
193+
"Downloaded file format is not supported: " + typeof file + "",
194+
);
189195
}

0 commit comments

Comments
 (0)