From 2068a8b85ba446bbbd602eb1bf0d9f20dc1a3042 Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 15 Jun 2017 14:58:18 +0200 Subject: [PATCH 1/2] Embed the mediatag library in the file app --- www/common/cryptpad-common.js | 2 +- www/file/inner.html | 11 +--- www/file/main.js | 108 +++++++++++++++++++++++----------- 3 files changed, 78 insertions(+), 43 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index df2f17379..55e2b1cc7 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -962,7 +962,7 @@ define([ data.FM.handleFile(file, ev); if (callback) { callback(); } }); - button.click(function () { $input.click(); }) + button.click(function () { $input.click(); }); break; case 'template': if (!AppConfig.enableTemplates) { return; } diff --git a/www/file/inner.html b/www/file/inner.html index 0005241a5..c4ccbbd7b 100644 --- a/www/file/inner.html +++ b/www/file/inner.html @@ -21,14 +21,9 @@ data-localization="download_button"> - + diff --git a/www/file/main.js b/www/file/main.js index dcd2139d1..32175f0f3 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -23,6 +23,7 @@ define([ var $iframe = $('#pad-iframe').contents(); var $form = $iframe.find('#upload-form'); var $dlform = $iframe.find('#download-form'); + var $dlview = $iframe.find('#download-view'); var $label = $form.find('label'); var $progress = $iframe.find('#progress'); var $body = $iframe.find('body'); @@ -94,7 +95,6 @@ define([ Title.updateTitle(Cryptpad.initialName || getTitle() || Title.defaultTitle); if (!uploadMode) { - $dlform.show(); var src = Cryptpad.getBlobPathFromHex(hexFileName); var cryptKey = secret.keys && secret.keys.fileKeyStr; var key = Nacl.util.decodeBase64(cryptKey); @@ -104,42 +104,82 @@ define([ var title = document.title = metadata.name; Title.updateTitle(title || Title.defaultTitle); - Cryptpad.removeLoadingScreen(); - var decrypting = false; - $dlform.find('#dl, #progress').click(function () { - if (decrypting) { return; } - if (myFile) { return void exportFile(); } - decrypting = true; - - return Cryptpad.fetch(src, function (e, u8) { - if (e) { - decrypting = false; - return void Cryptpad.alert(e); - } - - // now decrypt the u8 - if (!u8 || !u8.length) { - return void Cryptpad.errorLoadingScreen(e); + var displayFile = function (ev) { + console.log(e); + $mt = $dlview.find('media-tag'); + var cryptKey = secret.keys && secret.keys.fileKeyStr; + var hexFileName = Cryptpad.base64ToHex(secret.channel); + $mt.attr('src', '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName); + $mt.attr('data-crypto-key', 'cryptpad:'+cryptKey); + + $(window.document).on('decryption', function (e) { + var decrypted = e.originalEvent; + if (decrypted.callback) { decrypted.callback(); } + $dlview.show(); + $dlform.hide(); + if (ev) { + var $dlButton = $dlview.find('media-tag button'); + $dlButton.click(); } + Cryptpad.removeLoadingScreen(); + }) + .on('decryptionError', function (e) { + var error = e.originalEvent; + Cryptpad.alert(error.message); + }) + .on('decryptionProgress', function (e) { + var progress = e.originalEvent; + var p = progress.percent +'%'; + $progress.width(p); + console.log(progress.percent); + }); - FileCrypto.decrypt(u8, key, function (e, data) { - if (e) { - decrypting = false; - return console.error(e); - } - console.log(data); - var title = document.title = data.metadata.name; - myFile = data.content; - myDataType = data.metadata.type; - Title.updateTitle(title || Title.defaultTitle); - exportFile(); - decrypting = false; - }, function (progress) { - var p = progress * 100 +'%'; - $progress.width(p); - console.error(progress); - }); + require(['/common/media-tag.js'], function (MediaTag) { + /** + * Allowed mime types that have to be set for a rendering after a decryption. + * + * @type {Array} + */ + var allowedMediaTypes = [ + 'image/png', + 'image/jpeg', + 'image/jpg', + 'image/gif', + 'audio/mp3', + 'audio/ogg', + 'audio/wav', + 'audio/webm', + 'video/mp4', + 'video/ogg', + 'video/webm', + 'application/pdf', + 'application/dash+xml', + 'download' + ]; + MediaTag.CryptoFilter.setAllowedMediaTypes(allowedMediaTypes); + + MediaTag($mt[0]); }); + }; + + var todoBigFile = function (sizeMb) { + $dlform.show(); + Cryptpad.removeLoadingScreen(); + var decrypting = false; + var onClick = function () { + if (decrypting) { return; } + if (myFile) { return void exportFile(); } + decrypting = true; + displayFile(); + }; + if (sizeMb < 5) { return void onClick(); } + Cryptpad.removeLoadingScreen(); + $dlform.find('#dl, #progress').click(onClick); + }; + Cryptpad.getFileSize(window.location.href, function (e, data) { + if (e) { return void Cryptpad.errorLoadingScreen(e); } + var size = Cryptpad.bytesToMegabytes(data); + return void todoBigFile(size); }); }); return; From c2c04bd5d8f0251eee8c021ce258a9fa34da3cae Mon Sep 17 00:00:00 2001 From: yflory Date: Thu, 15 Jun 2017 15:02:31 +0200 Subject: [PATCH 2/2] Lint --- www/file/main.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/www/file/main.js b/www/file/main.js index 32175f0f3..20d166c5f 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -105,8 +105,7 @@ define([ Title.updateTitle(title || Title.defaultTitle); var displayFile = function (ev) { - console.log(e); - $mt = $dlview.find('media-tag'); + var $mt = $dlview.find('media-tag'); var cryptKey = secret.keys && secret.keys.fileKeyStr; var hexFileName = Cryptpad.base64ToHex(secret.channel); $mt.attr('src', '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName);