diff --git a/src/customisations/models/IMediaEventContent.ts b/src/customisations/models/IMediaEventContent.ts index 62dfe4ee19..a3e0231ebc 100644 --- a/src/customisations/models/IMediaEventContent.ts +++ b/src/customisations/models/IMediaEventContent.ts @@ -31,18 +31,26 @@ export interface IEncryptedFile { v: string; } -export interface IMediaEventContent { - body?: string; - url?: string; // required on unencrypted media - file?: IEncryptedFile; // required for *encrypted* media - info?: { - thumbnail_url?: string; // eslint-disable-line camelcase - thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase +export interface IMediaEventInfo { + thumbnail_url?: string; // eslint-disable-line camelcase + thumbnail_file?: IEncryptedFile; // eslint-disable-line camelcase + thumbnail_info?: { // eslint-disable-line camelcase mimetype: string; w?: number; h?: number; size?: number; }; + mimetype: string; + w?: number; + h?: number; + size?: number; +} + +export interface IMediaEventContent { + body?: string; + url?: string; // required on unencrypted media + file?: IEncryptedFile; // required for *encrypted* media + info?: IMediaEventInfo; } export interface IPreparedMedia extends IMediaObject { diff --git a/src/utils/DecryptFile.ts b/src/utils/DecryptFile.ts index e66db4ffb2..891439ffe1 100644 --- a/src/utils/DecryptFile.ts +++ b/src/utils/DecryptFile.ts @@ -17,18 +17,22 @@ limitations under the License. // Pull in the encryption lib so that we can decrypt attachments. import encrypt from 'browser-encrypt-attachment'; import { mediaFromContent } from "../customisations/Media"; -import { IEncryptedFile } from "../customisations/models/IMediaEventContent"; +import { IEncryptedFile, IMediaEventInfo } from "../customisations/models/IMediaEventContent"; import { getBlobSafeMimeType } from "./blobs"; /** * Decrypt a file attached to a matrix event. - * @param {IEncryptedFile} file The json taken from the matrix event. + * @param {IEncryptedFile} file The encrypted file information taken from the matrix event. * This passed to [link]{@link https://github.com/matrix-org/browser-encrypt-attachments} * as the encryption info object, so will also have the those keys in addition to * the keys below. + * @param {IMediaEventInfo} info The info parameter taken from the matrix event. * @returns {Promise} Resolves to a Blob of the file. */ -export function decryptFile(file: IEncryptedFile): Promise { +export function decryptFile( + file: IEncryptedFile, + info?: IMediaEventInfo, +): Promise { const media = mediaFromContent({ file }); // Download the encrypted file as an array buffer. return media.downloadSource().then((response) => { @@ -44,7 +48,7 @@ export function decryptFile(file: IEncryptedFile): Promise { // they introduce XSS attacks if the Blob URI is viewed directly in the // browser (e.g. by copying the URI into a new tab or window.) // See warning at top of file. - let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : ''; + let mimetype = info?.mimetype ? info.mimetype.split(";")[0].trim() : ''; mimetype = getBlobSafeMimeType(mimetype); return new Blob([dataArray], { type: mimetype }); diff --git a/src/utils/MediaEventHelper.ts b/src/utils/MediaEventHelper.ts index 8b8edcc62a..f68da309bc 100644 --- a/src/utils/MediaEventHelper.ts +++ b/src/utils/MediaEventHelper.ts @@ -76,7 +76,8 @@ export class MediaEventHelper implements IDestroyable { private fetchSource = () => { if (this.media.isEncrypted) { - return decryptFile(this.event.getContent().file); + const content = this.event.getContent(); + return decryptFile(content.file, content.info); } return this.media.downloadSource().then(r => r.blob()); }; @@ -87,7 +88,7 @@ export class MediaEventHelper implements IDestroyable { if (this.media.isEncrypted) { const content = this.event.getContent(); if (content.info?.thumbnail_file) { - return decryptFile(content.info.thumbnail_file); + return decryptFile(content.info.thumbnail_file, content.info.thumbnail_info); } else { // "Should never happen" console.warn("Media claims to have thumbnail and is encrypted, but no thumbnail_file found");