define([ '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', '/bower_components/tweetnacl/nacl-fast.min.js', '/bower_components/file-saver/FileSaver.min.js', ], function ($, Crypto, realtimeInput, Toolbar, Cryptpad, Visible, Notify, FileCrypto) { var Messages = Cryptpad.Messages; var saveAs = window.saveAs; var Nacl = window.nacl; var APP = {}; $(function () { var ifrw = $('#pad-iframe')[0].contentWindow; var $iframe = $('#pad-iframe').contents(); var $form = $iframe.find('#upload-form'); //var $progress = $form.find('#progress'); var $label = $form.find('label'); var $table = $iframe.find('#status'); Cryptpad.addLoadingScreen(); var Title; var myFile; var myDataType; var queue = { queue: [], inProgress: false }; var uid = function () { return 'file-' + String(Math.random()).substring(2); }; var upload = function (blob, metadata, id) { console.log(metadata); if (queue.inProgress) { return; } queue.inProgress = true; var $cancelCell = $table.find('tr[id="'+id+'"]').find('.upCancel'); $cancelCell.html('-'); var u8 = new Uint8Array(blob); var key = Nacl.randomBytes(32); var next = FileCrypto.encrypt(u8, metadata, key); var estimate = FileCrypto.computeEncryptedSize(blob.byteLength, metadata); var chunks = []; var sendChunk = function (box, cb) { var enc = Nacl.util.encodeBase64(box); chunks.push(box); Cryptpad.rpc.send('UPLOAD', enc, function (e, msg) { console.log(box); cb(e, msg); }); }; var actual = 0; var again = function (err, box) { if (err) { throw new Error(err); } if (box) { actual += box.length; var progressValue = (actual / estimate * 100); var progress = progressValue + '%'; return void sendChunk(box, function (e) { if (e) { return console.error(e); } /*$progress.css({ width: progress, });*/ var $pv = $table.find('tr[id="'+id+'"]').find('.progressValue'); $pv.text(Math.round(progressValue*100)/100 + '%'); var $pb = $table.find('tr[id="'+id+'"]').find('.progressContainer'); $pb.css({ width: (progressValue/100)*188+'px' }); next(again); }); } if (actual !== estimate) { console.error('Estimated size does not match actual size'); } // if not box then done Cryptpad.uploadComplete(function (e, id) { if (e) { return void console.error(e); } var uri = ['', 'blob', id.slice(0,2), id].join('/'); console.log("encrypted blob is now available as %s", uri); var b64Key = Nacl.util.encodeBase64(key); Cryptpad.replaceHash(Cryptpad.getFileHashFromKeys(id, b64Key)); //$form.hide(); APP.toolbar.addElement(['fileshare'], {}); var title = document.title = metadata.name; myFile = blob; myDataType = metadata.type; var defaultName = Cryptpad.getDefaultName(Cryptpad.parsePadUrl(window.location.href)); Title.updateTitle(title || defaultName); APP.toolbar.title.show(); console.log(title); Cryptpad.alert(Messages._getKey('upload_success', [title])); queue.inProgress = false; queue.next(); }); }; Cryptpad.uploadStatus(estimate, function (e, pending) { if (e) { queue.inProgress = false; queue.next(); if (e === 'TOO_LARGE') { return void Cryptpad.alert(Messages.upload_tooLarge); } if (e === 'NOT_ENOUGH_SPACE') { return void Cryptpad.alert(Messages.upload_notEnoughSpace); } console.error(e); return void Cryptpad.alert(Messages.upload_serverError); } if (pending) { // TODO keep this message in case of pending files in another window? return void Cryptpad.confirm(Messages.upload_uploadPending, function (yes) { if (!yes) { return; } Cryptpad.uploadCancel(function (e, res) { if (e) { return void console.error(e); } console.log(res); next(again); }); }); } next(again); }); }; var prettySize = function (bytes) { var kB = Cryptpad.bytesToKilobytes(bytes); if (kB < 1024) { return kB + Messages.KB; } var mB = Cryptpad.bytesToMegabytes(bytes); return mB + Messages.MB; }; queue.next = function () { if (queue.queue.length === 0) { return; } if (queue.inProgress) { return; } var file = queue.queue.shift(); upload(file.blob, file.metadata, file.id); }; queue.push = function (obj) { var id = uid(); obj.id = id; queue.queue.push(obj); $table.show(); var estimate = FileCrypto.computeEncryptedSize(obj.blob.byteLength, obj.metadata); var $progressBar = $('