Migrate the file to a sandboxed iframe
This commit is contained in:
298
www/file/main.js
298
www/file/main.js
@@ -1,269 +1,47 @@
|
||||
// Load #1, load as little as possible because we are in a race to get the loading screen up.
|
||||
define([
|
||||
'/bower_components/nthen/index.js',
|
||||
'/api/config',
|
||||
'jquery',
|
||||
'/bower_components/chainpad-crypto/crypto.js',
|
||||
'/bower_components/chainpad-netflux/chainpad-netflux.js',
|
||||
'/common/toolbar2.js',
|
||||
'/common/cryptpad-common.js',
|
||||
'/common/visible.js',
|
||||
'/common/notify.js',
|
||||
'/file/file-crypto.js',
|
||||
'/common/requireconfig.js',
|
||||
'/common/sframe-common-outer.js'
|
||||
], function (nThen, ApiConfig, $, RequireConfig, SFCommonO) {
|
||||
var requireConfig = RequireConfig();
|
||||
|
||||
'/common/media-tag.js',
|
||||
|
||||
'/bower_components/file-saver/FileSaver.min.js',
|
||||
|
||||
'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
|
||||
'less!/customize/src/less/cryptpad.less',
|
||||
], function ($, Crypto, realtimeInput, Toolbar, Cryptpad, Visible, Notify, FileCrypto, MediaTag) {
|
||||
var Messages = Cryptpad.Messages;
|
||||
var saveAs = window.saveAs;
|
||||
var Nacl = window.nacl;
|
||||
|
||||
var APP = window.APP = {};
|
||||
|
||||
$(function () {
|
||||
|
||||
var andThen = function () {
|
||||
var ifrw = $('#pad-iframe')[0].contentWindow;
|
||||
var $iframe = $('#pad-iframe').contents();
|
||||
var $appContainer = $iframe.find('#app');
|
||||
var $form = $iframe.find('#upload-form');
|
||||
var $dlform = $iframe.find('#download-form');
|
||||
var $dlview = $iframe.find('#download-view');
|
||||
var $label = $form.find('label');
|
||||
var $dllabel = $dlform.find('label span');
|
||||
var $progress = $iframe.find('#progress');
|
||||
var $body = $iframe.find('body');
|
||||
|
||||
$body.on('dragover', function (e) { e.preventDefault(); });
|
||||
$body.on('drop', function (e) { e.preventDefault(); });
|
||||
|
||||
Cryptpad.addLoadingScreen();
|
||||
|
||||
var Title;
|
||||
|
||||
var uploadMode = false;
|
||||
|
||||
var $bar = $iframe.find('.toolbar-container');
|
||||
|
||||
var secret;
|
||||
var hexFileName;
|
||||
if (window.location.hash) {
|
||||
secret = Cryptpad.getSecrets();
|
||||
if (!secret.keys) { throw new Error("You need a hash"); } // TODO
|
||||
hexFileName = Cryptpad.base64ToHex(secret.channel);
|
||||
} else {
|
||||
uploadMode = true;
|
||||
}
|
||||
|
||||
Title = Cryptpad.createTitle({}, function(){}, Cryptpad);
|
||||
|
||||
var displayed = ['useradmin', 'newpad', 'limit', 'upgrade'];
|
||||
if (secret && hexFileName) {
|
||||
displayed.push('fileshare');
|
||||
}
|
||||
|
||||
var configTb = {
|
||||
displayed: displayed,
|
||||
ifrw: ifrw,
|
||||
common: Cryptpad,
|
||||
//hideDisplayName: true,
|
||||
$container: $bar,
|
||||
// Loaded in load #2
|
||||
nThen(function (waitFor) {
|
||||
$(waitFor());
|
||||
}).nThen(function (waitFor) {
|
||||
var req = {
|
||||
cfg: requireConfig,
|
||||
req: [ '/common/loading.js' ],
|
||||
pfx: window.location.origin
|
||||
};
|
||||
window.rc = requireConfig;
|
||||
window.apiconf = ApiConfig;
|
||||
$('#sbox-iframe').attr('src',
|
||||
ApiConfig.httpSafeOrigin + '/file/inner.html?' + requireConfig.urlArgs +
|
||||
'#' + encodeURIComponent(JSON.stringify(req)));
|
||||
|
||||
if (uploadMode) {
|
||||
displayed.push('pageTitle');
|
||||
configTb.pageTitle = Messages.upload_title;
|
||||
}
|
||||
|
||||
var toolbar = APP.toolbar = Toolbar.create(configTb);
|
||||
toolbar.$rightside.html(''); // Remove the drawer if we don't use it to hide the toolbar
|
||||
|
||||
|
||||
if (!uploadMode) {
|
||||
var src = Cryptpad.getBlobPathFromHex(hexFileName);
|
||||
var cryptKey = secret.keys && secret.keys.fileKeyStr;
|
||||
var key = Nacl.util.decodeBase64(cryptKey);
|
||||
|
||||
FileCrypto.fetchDecryptedMetadata(src, key, function (e, metadata) {
|
||||
if (e) { return void console.error(e); }
|
||||
var title = document.title = metadata.name;
|
||||
Title.updateTitle(title || Title.defaultTitle);
|
||||
toolbar.addElement(['pageTitle'], {pageTitle: title});
|
||||
|
||||
console.error(metadata);
|
||||
|
||||
var displayFile = function (ev, sizeMb, CB) {
|
||||
var called_back;
|
||||
var cb = function (e) {
|
||||
if (called_back) { return; }
|
||||
called_back = true;
|
||||
if (CB) { CB(e); }
|
||||
};
|
||||
|
||||
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);
|
||||
$mt.attr('data-crypto-key', 'cryptpad:'+cryptKey);
|
||||
|
||||
var rightsideDisplayed = false;
|
||||
|
||||
$(window.document).on('decryption', function (e) {
|
||||
var decrypted = e.originalEvent;
|
||||
if (decrypted.callback) {
|
||||
decrypted.callback();
|
||||
}
|
||||
|
||||
console.log(decrypted);
|
||||
$dlview.show();
|
||||
$dlform.hide();
|
||||
var $dlButton = $dlview.find('media-tag button');
|
||||
if (ev) { $dlButton.click(); }
|
||||
if (!$dlButton.length) {
|
||||
$appContainer.css('background', 'white');
|
||||
}
|
||||
$dlButton.addClass('btn btn-success');
|
||||
var text = Messages.download_mt_button + '<br>';
|
||||
text += '<b>' + Cryptpad.fixHTML(title) + '</b><br>';
|
||||
text += '<em>' + Messages._getKey('formattedMB', [sizeMb]) + '</em>';
|
||||
$dlButton.html(text);
|
||||
|
||||
if (!rightsideDisplayed) {
|
||||
toolbar.$rightside.append(Cryptpad.createButton('export', true, {}, function () {
|
||||
saveAs(decrypted.blob, decrypted.metadata.name);
|
||||
}))
|
||||
.append(Cryptpad.createButton('forget', true, {}, function () {
|
||||
// not sure what to do here
|
||||
}));
|
||||
rightsideDisplayed = true;
|
||||
}
|
||||
|
||||
// make pdfs big
|
||||
var toolbarHeight = $iframe.find('#toolbar').height();
|
||||
var $another_iframe = $iframe.find('media-tag iframe').css({
|
||||
'height': 'calc(100vh - ' + toolbarHeight + 'px)',
|
||||
'width': '100vw',
|
||||
'position': 'absolute',
|
||||
'bottom': 0,
|
||||
'left': 0,
|
||||
'border': 0
|
||||
});
|
||||
|
||||
if ($another_iframe.length) {
|
||||
$another_iframe.load(function () {
|
||||
cb();
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
})
|
||||
.on('decryptionError', function (e) {
|
||||
var error = e.originalEvent;
|
||||
//Cryptpad.alert(error.message);
|
||||
cb(error.message);
|
||||
})
|
||||
.on('decryptionProgress', function (e) {
|
||||
var progress = e.originalEvent;
|
||||
var p = progress.percent +'%';
|
||||
$progress.width(p);
|
||||
console.log(progress.percent);
|
||||
});
|
||||
|
||||
/**
|
||||
* 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();
|
||||
$dllabel.append($('<br>'));
|
||||
$dllabel.append(Cryptpad.fixHTML(metadata.name));
|
||||
|
||||
// don't display the size if you don't know it.
|
||||
if (typeof(sizeM) === 'number') {
|
||||
$dllabel.append($('<br>'));
|
||||
$dllabel.append(Messages._getKey('formattedMB', [sizeMb]));
|
||||
}
|
||||
var decrypting = false;
|
||||
var onClick = function (ev) {
|
||||
if (decrypting) { return; }
|
||||
decrypting = true;
|
||||
displayFile(ev, sizeMb, function (err) {
|
||||
if (err) { Cryptpad.alert(err); }
|
||||
});
|
||||
};
|
||||
if (typeof(sizeMb) === 'number' && sizeMb < 5) { return void onClick(); }
|
||||
$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;
|
||||
}
|
||||
|
||||
if (!Cryptpad.isLoggedIn()) {
|
||||
return Cryptpad.alert(Messages.upload_mustLogin, function () {
|
||||
if (sessionStorage) {
|
||||
sessionStorage.redirectTo = window.location.href;
|
||||
}
|
||||
window.location.href = '/login/';
|
||||
});
|
||||
}
|
||||
|
||||
$form.css({
|
||||
display: 'block',
|
||||
});
|
||||
|
||||
var fmConfig = {
|
||||
dropArea: $form,
|
||||
hoverArea: $label,
|
||||
body: $body,
|
||||
keepTable: true // Don't fadeOut the tbale with the uploaded files
|
||||
// This is a cheap trick to avoid loading sframe-channel in parallel with the
|
||||
// loading screen setup.
|
||||
var done = waitFor();
|
||||
var onMsg = function (msg) {
|
||||
var data = JSON.parse(msg.data);
|
||||
if (data.q !== 'READY') { return; }
|
||||
window.removeEventListener('message', onMsg);
|
||||
var _done = done;
|
||||
done = function () { };
|
||||
_done();
|
||||
};
|
||||
|
||||
var FM = Cryptpad.createFileManager(fmConfig);
|
||||
|
||||
$form.find("#file").on('change', function (e) {
|
||||
var file = e.target.files[0];
|
||||
FM.handleFile(file);
|
||||
window.addEventListener('message', onMsg);
|
||||
}).nThen(function (/*waitFor*/) {
|
||||
var addData = function (meta) {
|
||||
meta.filehash = window.location.hash;
|
||||
};
|
||||
SFCommonO.start({
|
||||
noRealtime: true,
|
||||
addData: addData
|
||||
});
|
||||
|
||||
// we're in upload mode
|
||||
Cryptpad.removeLoadingScreen();
|
||||
};
|
||||
|
||||
Cryptpad.ready(function () {
|
||||
andThen();
|
||||
Cryptpad.reportAppUsage();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user