2016-11-04 13:46:45 +01:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Pull in the encryption lib so that we can decrypt attachments.
|
2016-11-08 12:42:20 +01:00
|
|
|
import encrypt from 'browser-encrypt-attachment';
|
2016-11-04 13:46:45 +01:00
|
|
|
// Pull in a fetch polyfill so we can download encrypted attachments.
|
2016-11-08 12:42:20 +01:00
|
|
|
import 'isomorphic-fetch';
|
2016-11-04 13:46:45 +01:00
|
|
|
// Grab the client so that we can turn mxc:// URLs into https:// URLS.
|
2016-11-08 12:42:20 +01:00
|
|
|
import MatrixClientPeg from '../MatrixClientPeg';
|
2017-07-12 14:58:14 +02:00
|
|
|
import Promise from 'bluebird';
|
2016-11-04 16:39:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read blob as a data:// URI.
|
|
|
|
* @return {Promise} A promise that resolves with the data:// URI.
|
|
|
|
*/
|
2016-12-02 15:21:07 +01:00
|
|
|
export function readBlobAsDataUri(file) {
|
2017-10-11 18:56:17 +02:00
|
|
|
const deferred = Promise.defer();
|
|
|
|
const reader = new FileReader();
|
2016-11-04 16:39:39 +01:00
|
|
|
reader.onload = function(e) {
|
|
|
|
deferred.resolve(e.target.result);
|
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
deferred.reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
2016-11-04 13:46:45 +01:00
|
|
|
|
|
|
|
|
2016-11-08 12:42:20 +01:00
|
|
|
/**
|
|
|
|
* Decrypt a file attached to a matrix event.
|
|
|
|
* @param file {Object} The json 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 file.url {string} An mxc:// URL for the encrypted file.
|
|
|
|
* @param file.mimetype {string} The MIME-type of the plaintext file.
|
|
|
|
*/
|
2016-11-04 13:46:45 +01:00
|
|
|
export function decryptFile(file) {
|
2016-11-08 12:42:20 +01:00
|
|
|
const url = MatrixClientPeg.get().mxcUrlToHttp(file.url);
|
2016-11-04 13:46:45 +01:00
|
|
|
// Download the encrypted file as an array buffer.
|
2017-07-12 15:02:00 +02:00
|
|
|
return Promise.resolve(fetch(url)).then(function(response) {
|
2016-11-04 13:46:45 +01:00
|
|
|
return response.arrayBuffer();
|
2016-11-08 12:42:20 +01:00
|
|
|
}).then(function(responseData) {
|
2016-11-04 13:46:45 +01:00
|
|
|
// Decrypt the array buffer using the information taken from
|
|
|
|
// the event content.
|
|
|
|
return encrypt.decryptAttachment(responseData, file);
|
|
|
|
}).then(function(dataArray) {
|
|
|
|
// Turn the array into a Blob and give it the correct MIME-type.
|
2017-10-11 18:56:17 +02:00
|
|
|
const blob = new Blob([dataArray], {type: file.mimetype});
|
2016-12-02 15:21:07 +01:00
|
|
|
return blob;
|
2016-11-04 13:46:45 +01:00
|
|
|
});
|
|
|
|
}
|