2021-07-14 20:51:20 -06:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-07-14 20:51:20 -06:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-07-14 20:51:20 -06:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type MatrixEvent, EventType, MsgType } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
import { type FileContent, type ImageContent, type MediaEventContent } from "matrix-js-sdk/src/types";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
2021-07-14 20:51:20 -06:00
|
|
|
import { LazyValue } from "./LazyValue";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type Media, mediaFromContent } from "../customisations/Media";
|
2021-07-14 20:51:20 -06:00
|
|
|
import { decryptFile } from "./DecryptFile";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type IDestroyable } from "./IDestroyable";
|
2021-10-15 16:31:29 +02:00
|
|
|
|
2021-07-14 20:51:20 -06:00
|
|
|
// TODO: We should consider caching the blobs. https://github.com/vector-im/element-web/issues/17192
|
|
|
|
|
|
|
|
|
|
export class MediaEventHelper implements IDestroyable {
|
2021-07-20 08:55:07 -06:00
|
|
|
// Either an HTTP or Object URL (when encrypted) to the media.
|
2023-02-16 17:21:44 +00:00
|
|
|
public readonly sourceUrl: LazyValue<string | null>;
|
|
|
|
|
public readonly thumbnailUrl: LazyValue<string | null>;
|
2021-07-20 08:55:07 -06:00
|
|
|
|
|
|
|
|
// Either the raw or decrypted (when encrypted) contents of the file.
|
2021-07-14 20:51:20 -06:00
|
|
|
public readonly sourceBlob: LazyValue<Blob>;
|
2023-02-16 17:21:44 +00:00
|
|
|
public readonly thumbnailBlob: LazyValue<Blob | null>;
|
2021-07-20 08:55:07 -06:00
|
|
|
|
2021-07-14 20:51:20 -06:00
|
|
|
public readonly media: Media;
|
|
|
|
|
|
|
|
|
|
public constructor(private event: MatrixEvent) {
|
|
|
|
|
this.sourceUrl = new LazyValue(this.prepareSourceUrl);
|
|
|
|
|
this.thumbnailUrl = new LazyValue(this.prepareThumbnailUrl);
|
|
|
|
|
this.sourceBlob = new LazyValue(this.fetchSource);
|
|
|
|
|
this.thumbnailBlob = new LazyValue(this.fetchThumbnail);
|
|
|
|
|
|
|
|
|
|
this.media = mediaFromContent(this.event.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 15:55:07 -06:00
|
|
|
public get fileName(): string {
|
2022-06-30 16:22:52 +01:00
|
|
|
return (
|
2023-07-17 13:07:58 +01:00
|
|
|
this.event.getContent<FileContent>().filename ||
|
2024-03-11 09:30:00 +00:00
|
|
|
this.event.getContent<MediaEventContent>().body ||
|
2022-06-30 16:22:52 +01:00
|
|
|
"download"
|
|
|
|
|
);
|
2021-07-16 15:55:07 -06:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public destroy(): void {
|
2021-07-14 20:51:20 -06:00
|
|
|
if (this.media.isEncrypted) {
|
2023-02-16 17:21:44 +00:00
|
|
|
if (this.sourceUrl.cachedValue) URL.revokeObjectURL(this.sourceUrl.cachedValue);
|
|
|
|
|
if (this.thumbnailUrl.cachedValue) URL.revokeObjectURL(this.thumbnailUrl.cachedValue);
|
2021-07-14 20:51:20 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
private prepareSourceUrl = async (): Promise<string | null> => {
|
2021-07-14 20:51:20 -06:00
|
|
|
if (this.media.isEncrypted) {
|
|
|
|
|
const blob = await this.sourceBlob.value;
|
|
|
|
|
return URL.createObjectURL(blob);
|
|
|
|
|
} else {
|
|
|
|
|
return this.media.srcHttp;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
private prepareThumbnailUrl = async (): Promise<string | null> => {
|
2021-07-14 20:51:20 -06:00
|
|
|
if (this.media.isEncrypted) {
|
|
|
|
|
const blob = await this.thumbnailBlob.value;
|
2021-07-22 22:49:30 +01:00
|
|
|
if (blob === null) return null;
|
2021-07-14 20:51:20 -06:00
|
|
|
return URL.createObjectURL(blob);
|
|
|
|
|
} else {
|
|
|
|
|
return this.media.thumbnailHttp;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private fetchSource = (): Promise<Blob> => {
|
2021-07-14 20:51:20 -06:00
|
|
|
if (this.media.isEncrypted) {
|
2024-03-11 09:30:00 +00:00
|
|
|
const content = this.event.getContent<MediaEventContent>();
|
2023-02-16 17:21:44 +00:00
|
|
|
return decryptFile(content.file!, content.info);
|
2021-07-14 20:51:20 -06:00
|
|
|
}
|
|
|
|
|
return this.media.downloadSource().then((r) => r.blob());
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-16 17:21:44 +00:00
|
|
|
private fetchThumbnail = (): Promise<Blob | null> => {
|
2021-07-14 20:51:20 -06:00
|
|
|
if (!this.media.hasThumbnail) return Promise.resolve(null);
|
|
|
|
|
|
|
|
|
|
if (this.media.isEncrypted) {
|
2023-07-17 13:07:58 +01:00
|
|
|
const content = this.event.getContent<ImageContent>();
|
2021-07-14 20:51:20 -06:00
|
|
|
if (content.info?.thumbnail_file) {
|
2021-08-10 19:25:56 -04:00
|
|
|
return decryptFile(content.info.thumbnail_file, content.info.thumbnail_info);
|
2021-07-14 20:51:20 -06:00
|
|
|
} else {
|
|
|
|
|
// "Should never happen"
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn("Media claims to have thumbnail and is encrypted, but no thumbnail_file found");
|
2021-07-14 20:51:20 -06:00
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 20:26:55 +12:00
|
|
|
const thumbnailHttp = this.media.thumbnailHttp;
|
|
|
|
|
if (!thumbnailHttp) return Promise.resolve(null);
|
|
|
|
|
|
|
|
|
|
return fetch(thumbnailHttp).then((r) => r.blob());
|
2021-07-14 20:51:20 -06:00
|
|
|
};
|
2021-07-15 14:19:07 -06:00
|
|
|
|
|
|
|
|
public static isEligible(event: MatrixEvent): boolean {
|
|
|
|
|
if (!event) return false;
|
|
|
|
|
if (event.isRedacted()) return false;
|
|
|
|
|
if (event.getType() === EventType.Sticker) return true;
|
|
|
|
|
if (event.getType() !== EventType.RoomMessage) return false;
|
|
|
|
|
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
const mediaMsgTypes: string[] = [MsgType.Video, MsgType.Audio, MsgType.Image, MsgType.File];
|
2023-02-16 17:21:44 +00:00
|
|
|
if (mediaMsgTypes.includes(content.msgtype!)) return true;
|
2021-07-15 14:19:07 -06:00
|
|
|
if (typeof content.url === "string") return true;
|
|
|
|
|
|
|
|
|
|
// Finally, it's probably not media
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-03-24 14:38:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the media event in question supports being hidden in the timeline.
|
|
|
|
|
* @param event Any matrix event.
|
|
|
|
|
* @returns `true` if the media can be hidden, otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
public static canHide(event: MatrixEvent): boolean {
|
|
|
|
|
if (!event) return false;
|
|
|
|
|
if (event.isRedacted()) return false;
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
const hideTypes: string[] = [MsgType.Video, MsgType.Image];
|
|
|
|
|
if (hideTypes.includes(content.msgtype!)) return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-14 20:51:20 -06:00
|
|
|
}
|