From c1d05a00ba7cb95f7415a4b608b9fc3bb909af25 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 23 Oct 2017 10:55:40 +0200 Subject: [PATCH] Improve media-tag export in pad to work with existing CSP --- www/common/common-util.js | 7 +++++++ www/common/cryptpad-common.js | 9 +++++++-- www/pad/inner.js | 8 +++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/www/common/common-util.js b/www/common/common-util.js index 8fda0f8c6..5b2b6e84e 100644 --- a/www/common/common-util.js +++ b/www/common/common-util.js @@ -207,6 +207,13 @@ define([], function () { return Array.prototype.slice.call(A); }; + Util.blobToImage = function (blob, cb) { + var reader = new FileReader(); + reader.onloadend = function() { + cb(reader.result); + }; + reader.readAsDataURL(blob); + }; Util.blobURLToImage = function (url, cb) { var xhr = new XMLHttpRequest(); xhr.onload = function() { diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index cd4320d4f..9789a5ce8 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -1498,10 +1498,15 @@ define([ if (decrypted.callback) { var cb = decrypted.callback; cb(function (mediaObject) { - if (mediaObject.type !== 'download') { return; } - var root = mediaObject.rootElement; + var root = mediaObject.element; if (!root) { return; } + if (mediaObject.type === 'image') { + $(root).data('blob', decrypted.blob); + } + + if (mediaObject.type !== 'download') { return; } + var metadata = decrypted.metadata; var title = ''; diff --git a/www/pad/inner.js b/www/pad/inner.js index 8b2ed6a20..edbb3c25a 100644 --- a/www/pad/inner.js +++ b/www/pad/inner.js @@ -477,9 +477,11 @@ define([ var exportMediaTags = function (inner, cb) { var $clone = $(inner).clone(); nThen(function (waitFor) { - $clone.find('media-tag > img').each(function (i, el) { - Util.blobURLToImage($(el).attr('src'), waitFor(function (imgSrc) { - $(el).attr('src', imgSrc); + $(inner).find('media-tag').each(function (i, el) { + if (!$(el).data('blob')) { return; } + Util.blobToImage($(el).data('blob'), waitFor(function (imgSrc) { + $clone.find('media-tag[src="' + $(el).attr('src') + '"] img') + .attr('src', imgSrc); })); }); }).nThen(function () {