/
contentRepo.ts
37 lines (35 loc) · 1.56 KB
/
contentRepo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { MatrixClient } from "matrix-bot-sdk";
/**
* Get the HTTP URL for an MXC URI.
* @param {string} baseUrl The base homeserver url which has a content repo.
* @param {string} mxc The mxc:// URI.
* @param {Number} width The desired width of the thumbnail.
* @param {Number} height The desired height of the thumbnail.
* @param resizeMethod The thumbnail resize method to use, either
* "crop" or "scale".
* @param allowDirectLinks If true, return any non-mxc URLs
* directly. Fetching such URLs will leak information about the user to
* anyone they share a room with. If false, will return the emptry string
* for such URLs.
* @return The complete URL to the content. May be empty string if mxc is not a string.
*/
function getHttpUriForMxc(baseUrl: string, mxc: string, width?: number, height?: number,
resizeMethod?: "crop"|"scale", allowDirectLinks?: boolean): string {
console.warn("Deprecated call to ContentRepo.getHttpUriForMxc, prefer to use Intent.matrixClient.mxcToHttp");
if (typeof mxc !== "string" || !mxc) {
return "";
}
if (!mxc.startsWith("mxc://")) {
return allowDirectLinks ? mxc : "";
}
if (width || height || resizeMethod) {
return new MatrixClient(baseUrl, "").mxcToHttpThumbnail(
// Types are possibly not defined here, but this matches the previous implementation
mxc, width as number, height as number, resizeMethod as "crop"|"scale"
);
}
return new MatrixClient(baseUrl, "").mxcToHttp(mxc);
}
export const ContentRepo = {
getHttpUriForMxc: getHttpUriForMxc,
}