use the mimetype from the info property rather than the EncryptedFile

the mimetype in EncryptedFile is undocumented and redundant.
see https://github.com/matrix-org/matrix-doc/pull/2582
This commit is contained in:
Hubert Chathi
2021-08-10 19:27:24 -04:00
parent d7cb855419
commit 3b9810719f
3 changed files with 26 additions and 13 deletions
+8 -4
View File
@@ -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<Blob>} Resolves to a Blob of the file.
*/
export function decryptFile(file: IEncryptedFile): Promise<Blob> {
export function decryptFile(
file: IEncryptedFile,
info: IMediaEventInfo | undefined,
): Promise<Blob> {
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<Blob> {
// 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 });