mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-17 05:55:00 +08:00
Convert DecryptFile to TS and modernize a bit
This commit is contained in:
parent
93f7f13c44
commit
53935782bc
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
export interface IEncryptedFile {
|
export interface IEncryptedFile {
|
||||||
url: string;
|
url: string;
|
||||||
|
mimetype?: string;
|
||||||
key: {
|
key: {
|
||||||
alg: string;
|
alg: string;
|
||||||
key_ops: string[];
|
key_ops: string[];
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2016 OpenMarket Ltd
|
Copyright 2016, 2018, 2021 The Matrix.org Foundation C.I.C.
|
||||||
Copyright 2018 New Vector Ltd
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@ -17,9 +16,8 @@ limitations under the License.
|
|||||||
|
|
||||||
// Pull in the encryption lib so that we can decrypt attachments.
|
// Pull in the encryption lib so that we can decrypt attachments.
|
||||||
import encrypt from 'browser-encrypt-attachment';
|
import encrypt from 'browser-encrypt-attachment';
|
||||||
// Grab the client so that we can turn mxc:// URLs into https:// URLS.
|
|
||||||
import {MatrixClientPeg} from '../MatrixClientPeg';
|
|
||||||
import {mediaFromContent} from "../customisations/Media";
|
import {mediaFromContent} from "../customisations/Media";
|
||||||
|
import {IEncryptedFile} from "../customisations/models/IMediaEventContent";
|
||||||
|
|
||||||
// WARNING: We have to be very careful about what mime-types we allow into blobs,
|
// WARNING: We have to be very careful about what mime-types we allow into blobs,
|
||||||
// as for performance reasons these are now rendered via URL.createObjectURL()
|
// as for performance reasons these are now rendered via URL.createObjectURL()
|
||||||
@ -55,48 +53,46 @@ import {mediaFromContent} from "../customisations/Media";
|
|||||||
// For the record, mime-types which must NEVER enter this list below include:
|
// For the record, mime-types which must NEVER enter this list below include:
|
||||||
// text/html, text/xhtml, image/svg, image/svg+xml, image/pdf, and similar.
|
// text/html, text/xhtml, image/svg, image/svg+xml, image/pdf, and similar.
|
||||||
|
|
||||||
const ALLOWED_BLOB_MIMETYPES = {
|
const ALLOWED_BLOB_MIMETYPES = [
|
||||||
'image/jpeg': true,
|
'image/jpeg',
|
||||||
'image/gif': true,
|
'image/gif',
|
||||||
'image/png': true,
|
'image/png',
|
||||||
|
|
||||||
'video/mp4': true,
|
'video/mp4',
|
||||||
'video/webm': true,
|
'video/webm',
|
||||||
'video/ogg': true,
|
'video/ogg',
|
||||||
|
|
||||||
'audio/mp4': true,
|
'audio/mp4',
|
||||||
'audio/webm': true,
|
'audio/webm',
|
||||||
'audio/aac': true,
|
'audio/aac',
|
||||||
'audio/mpeg': true,
|
'audio/mpeg',
|
||||||
'audio/ogg': true,
|
'audio/ogg',
|
||||||
'audio/wave': true,
|
'audio/wave',
|
||||||
'audio/wav': true,
|
'audio/wav',
|
||||||
'audio/x-wav': true,
|
'audio/x-wav',
|
||||||
'audio/x-pn-wav': true,
|
'audio/x-pn-wav',
|
||||||
'audio/flac': true,
|
'audio/flac',
|
||||||
'audio/x-flac': true,
|
'audio/x-flac',
|
||||||
};
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a file attached to a matrix event.
|
* Decrypt a file attached to a matrix event.
|
||||||
* @param {Object} file The json taken from the matrix event.
|
* @param {IEncryptedFile} file The json taken from the matrix event.
|
||||||
* This passed to [link]{@link https://github.com/matrix-org/browser-encrypt-attachments}
|
* 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
|
* as the encryption info object, so will also have the those keys in addition to
|
||||||
* the keys below.
|
* the keys below.
|
||||||
* @param {string} file.url An mxc:// URL for the encrypted file.
|
* @returns {Promise<Blob>} Resolves to a Blob of the file.
|
||||||
* @param {string} file.mimetype The MIME-type of the plaintext file.
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
*/
|
||||||
export function decryptFile(file) {
|
export function decryptFile(file: IEncryptedFile): Promise<Blob> {
|
||||||
const media = mediaFromContent({file});
|
const media = mediaFromContent({file});
|
||||||
// Download the encrypted file as an array buffer.
|
// Download the encrypted file as an array buffer.
|
||||||
return media.downloadSource().then(function(response) {
|
return media.downloadSource().then((response) => {
|
||||||
return response.arrayBuffer();
|
return response.arrayBuffer();
|
||||||
}).then(function(responseData) {
|
}).then((responseData) => {
|
||||||
// Decrypt the array buffer using the information taken from
|
// Decrypt the array buffer using the information taken from
|
||||||
// the event content.
|
// the event content.
|
||||||
return encrypt.decryptAttachment(responseData, file);
|
return encrypt.decryptAttachment(responseData, file);
|
||||||
}).then(function(dataArray) {
|
}).then((dataArray) => {
|
||||||
// Turn the array into a Blob and give it the correct MIME-type.
|
// Turn the array into a Blob and give it the correct MIME-type.
|
||||||
|
|
||||||
// IMPORTANT: we must not allow scriptable mime-types into Blobs otherwise
|
// IMPORTANT: we must not allow scriptable mime-types into Blobs otherwise
|
||||||
@ -104,11 +100,10 @@ export function decryptFile(file) {
|
|||||||
// browser (e.g. by copying the URI into a new tab or window.)
|
// browser (e.g. by copying the URI into a new tab or window.)
|
||||||
// See warning at top of file.
|
// See warning at top of file.
|
||||||
let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : '';
|
let mimetype = file.mimetype ? file.mimetype.split(";")[0].trim() : '';
|
||||||
if (!ALLOWED_BLOB_MIMETYPES[mimetype]) {
|
if (!ALLOWED_BLOB_MIMETYPES.includes(mimetype)) {
|
||||||
mimetype = 'application/octet-stream';
|
mimetype = 'application/octet-stream';
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = new Blob([dataArray], {type: mimetype});
|
return new Blob([dataArray], {type: mimetype});
|
||||||
return blob;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user