', {
+ 'class': 'cp-splitter'
+ }).appendTo($preview);
+
+ $preview.on('scroll', function() {
+ splitter.css('top', $preview.scrollTop() + 'px');
+ });
+
+ var $target = $iframe.find('.CodeMirror');
+
+ splitter.on('mousedown', function (e) {
+ e.preventDefault();
+ var x = e.pageX;
+ var w = $target.width();
+
+ $iframe.on('mouseup mousemove', function handler(evt) {
+ if (evt.type === 'mouseup') {
+ $iframe.off('mouseup mousemove', handler);
+ return;
+ }
+ $target.css('width', (w - x + evt.pageX) + 'px');
+ });
+ });
+ }
+*/
+
+ Cryptpad.removeLoadingScreen();
+ setEditable(!readOnly);
+ initializing = false;
+
+ onLocal(); // push local state to avoid parse errors later.
+
+ if (readOnly) {
+ config.onRemote();
+ return;
+ }
+
+ if (isNew) {
+ common.openTemplatePicker();
+ }
+
+ var fmConfig = {
+ dropArea: $('.CodeMirror'),
+ body: $('body'),
+ onUploaded: function (ev, data) {
+ //var cursor = editor.getCursor();
+ //var cleanName = data.name.replace(/[\[\]]/g, '');
+ //var text = '!['+cleanName+']('+data.url+')';
+ var parsed = Cryptpad.parsePadUrl(data.url);
+ var hexFileName = Cryptpad.base64ToHex(parsed.hashData.channel);
+ var src = '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName;
+ var mt = '
';
+ editor.replaceSelection(mt);
+ }
+ };
+ APP.FM = common.createFileManager(fmConfig);
+ };
+
+ config.onRemote = function () {
+ if (initializing) { return; }
+ if (isHistoryMode) { return; }
+
+ var oldDoc = canonicalize(CodeMirror.$textarea.val());
+ var shjson = APP.realtime.getUserDoc();
+
+ // Update the user list (metadata) from the hyperjson
+ //Metadata.update(shjson);
+
+ var hjson = JSON.parse(shjson);
+ var remoteDoc = hjson.content;
+
+ if (hjson.metadata) {
+ metadataMgr.updateMetadata(hjson.metadata);
+ }
+
+ var highlightMode = hjson.highlightMode;
+ if (highlightMode && highlightMode !== APP.highlightMode) {
+ CodeMirror.setMode(highlightMode, onModeChanged);
+ }
+
+ CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
+ drawPreview();
+
+ if (!readOnly) {
+ var textValue = canonicalize(CodeMirror.$textarea.val());
+ var shjson2 = stringifyInner(textValue);
+ if (shjson2 !== shjson) {
+ console.error("shjson2 !== shjson");
+ TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
+ APP.patchText(shjson2);
+ }
+ }
+ if (oldDoc !== remoteDoc) { Cryptpad.notify(); }
+ };
+
+ config.onAbort = function () {
+ // inform of network disconnect
+ setEditable(false);
+ toolbar.failed();
+ Cryptpad.alert(Messages.common_connectionLost, undefined, true);
+ };
+
+ config.onConnectionChange = function (info) {
+ setEditable(info.state);
+ //toolbar.failed();
+ if (info.state) {
+ initializing = true;
+ //toolbar.reconnecting(info.myId);
+ Cryptpad.findOKButton().click();
+ } else {
+ Cryptpad.alert(Messages.common_connectionLost, undefined, true);
+ }
+ };
+
+ config.onError = onConnectError;
+
+ cpNfInner = common.startRealtime(config);
+ metadataMgr = cpNfInner.metadataMgr;
+
+ cpNfInner.onInfiniteSpinner(function () {
+ setEditable(false);
+ Cryptpad.confirm(Messages.realtime_unrecoverableError, function (yes) {
+ if (!yes) { return; }
+ common.gotoURL();
+ });
+ });
+
+ editor.on('change', onLocal);
+
+ Cryptpad.onLogout(function () { setEditable(false); });
+ };
+
+ var main = function () {
+ var common;
+
+ nThen(function (waitFor) {
+ $(waitFor(function () {
+ Cryptpad.addLoadingScreen();
+ }));
+ SFCommon.create(waitFor(function (c) { APP.common = common = c; }));
+ }).nThen(function (/*waitFor*/) {
+ common.getSframeChannel().onReady(function () {
+ andThen(common);
+ });
+ });
+ };
+ main();
});
diff --git a/www/file/main.js b/www/file/main.js
index 136702c47..0af6558ef 100644
--- a/www/file/main.js
+++ b/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/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,
+ '/common/requireconfig.js',
+ '/common/sframe-common-outer.js'
+], function (nThen, ApiConfig, $, RequireConfig, SFCommonO) {
+ var requireConfig = RequireConfig();
+
+ // Loaded in load #2
+ nThen(function (waitFor) {
+ $(waitFor());
+ }).nThen(function (waitFor) {
+ var req = {
+ cfg: requireConfig,
+ req: [ '/common/loading.js' ],
+ pfx: window.location.origin
};
-
- 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 + '
';
- text += '
' + Cryptpad.fixHTML(title) + '';
- text += '
' + Messages._getKey('formattedMB', [sizeMb]) + '';
- $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($('
'));
- $dllabel.append(Cryptpad.fixHTML(metadata.name));
-
- // don't display the size if you don't know it.
- if (typeof(sizeM) === 'number') {
- $dllabel.append($('
'));
- $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
+ window.rc = requireConfig;
+ window.apiconf = ApiConfig;
+ $('#sbox-iframe').attr('src',
+ ApiConfig.httpSafeOrigin + '/file/inner.html?' + requireConfig.urlArgs +
+ '#' + encodeURIComponent(JSON.stringify(req)));
+
+ // 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();
- });
-
});
});
diff --git a/www/file/assets/image.png-encrypted b/www/oldfile/assets/image.png-encrypted
similarity index 100%
rename from www/file/assets/image.png-encrypted
rename to www/oldfile/assets/image.png-encrypted
diff --git a/www/oldfile/file-crypto.js b/www/oldfile/file-crypto.js
new file mode 100644
index 000000000..44952fc87
--- /dev/null
+++ b/www/oldfile/file-crypto.js
@@ -0,0 +1,271 @@
+define([
+ '/bower_components/tweetnacl/nacl-fast.min.js',
+], function () {
+ var Nacl = window.nacl;
+ var PARANOIA = true;
+
+ var plainChunkLength = 128 * 1024;
+ var cypherChunkLength = 131088;
+
+ var computeEncryptedSize = function (bytes, meta) {
+ var metasize = Nacl.util.decodeUTF8(JSON.stringify(meta)).length;
+ var chunks = Math.ceil(bytes / plainChunkLength);
+ return metasize + 18 + (chunks * 16) + bytes;
+ };
+
+ var encodePrefix = function (p) {
+ return [
+ 65280, // 255 << 8
+ 255,
+ ].map(function (n, i) {
+ return (p & n) >> ((1 - i) * 8);
+ });
+ };
+ var decodePrefix = function (A) {
+ return (A[0] << 8) | A[1];
+ };
+
+ var slice = function (A) {
+ return Array.prototype.slice.call(A);
+ };
+
+ var createNonce = function () {
+ return new Uint8Array(new Array(24).fill(0));
+ };
+
+ var increment = function (N) {
+ var l = N.length;
+ while (l-- > 1) {
+ if (PARANOIA) {
+ if (typeof(N[l]) !== 'number') {
+ throw new Error('E_UNSAFE_TYPE');
+ }
+ if (N[l] > 255) {
+ throw new Error('E_OUT_OF_BOUNDS');
+ }
+ }
+ /* jshint probably suspects this is unsafe because we lack types
+ but as long as this is only used on nonces, it should be safe */
+ if (N[l] !== 255) { return void N[l]++; } // jshint ignore:line
+ N[l] = 0;
+
+ // you don't need to worry about this running out.
+ // you'd need a REAAAALLY big file
+ if (l === 0) {
+ throw new Error('E_NONCE_TOO_LARGE');
+ }
+ }
+ };
+
+ var joinChunks = function (chunks) {
+ return new Blob(chunks);
+ };
+
+ var concatBuffer = function (a, b) { // TODO make this not so ugly
+ return new Uint8Array(slice(a).concat(slice(b)));
+ };
+
+ var fetchMetadata = function (src, cb) {
+ var done = false;
+ var CB = function (err, res) {
+ if (done) { return; }
+ done = true;
+ cb(err, res);
+ };
+
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", src, true);
+ xhr.setRequestHeader('Range', 'bytes=0-1');
+ xhr.responseType = 'arraybuffer';
+
+ xhr.onload = function () {
+ if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); }
+ var res = new Uint8Array(xhr.response);
+ var size = decodePrefix(res);
+ var xhr2 = new XMLHttpRequest();
+
+ xhr2.open("GET", src, true);
+ xhr2.setRequestHeader('Range', 'bytes=2-' + (size + 2));
+ xhr2.responseType = 'arraybuffer';
+ xhr2.onload = function () {
+ if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); }
+ var res2 = new Uint8Array(xhr2.response);
+ var all = concatBuffer(res, res2);
+ CB(void 0, all);
+ };
+ xhr2.send(null);
+ };
+ xhr.send(null);
+ };
+
+ var decryptMetadata = function (u8, key) {
+ var prefix = u8.subarray(0, 2);
+ var metadataLength = decodePrefix(prefix);
+
+ var metaBox = new Uint8Array(u8.subarray(2, 2 + metadataLength));
+ var metaChunk = Nacl.secretbox.open(metaBox, createNonce(), key);
+
+ try {
+ return JSON.parse(Nacl.util.encodeUTF8(metaChunk));
+ }
+ catch (e) { return null; }
+ };
+
+ var fetchDecryptedMetadata = function (src, key, cb) {
+ if (typeof(src) !== 'string') {
+ return window.setTimeout(function () {
+ cb('NO_SOURCE');
+ });
+ }
+ fetchMetadata(src, function (e, buffer) {
+ if (e) { return cb(e); }
+ cb(void 0, decryptMetadata(buffer, key));
+ });
+ };
+
+ var decrypt = function (u8, key, done, progress) {
+ var MAX = u8.length;
+ var _progress = function (offset) {
+ if (typeof(progress) !== 'function') { return; }
+ progress(Math.min(1, offset / MAX));
+ };
+
+ var nonce = createNonce();
+ var i = 0;
+
+ var prefix = u8.subarray(0, 2);
+ var metadataLength = decodePrefix(prefix);
+
+ var res = {
+ metadata: undefined,
+ };
+
+ var metaBox = new Uint8Array(u8.subarray(2, 2 + metadataLength));
+
+ var metaChunk = Nacl.secretbox.open(metaBox, nonce, key);
+ increment(nonce);
+
+ try {
+ res.metadata = JSON.parse(Nacl.util.encodeUTF8(metaChunk));
+ } catch (e) {
+ return window.setTimeout(function () {
+ done('E_METADATA_DECRYPTION');
+ });
+ }
+
+ if (!res.metadata) {
+ return void setTimeout(function () {
+ done('NO_METADATA');
+ });
+ }
+
+ var takeChunk = function (cb) {
+ var start = i * cypherChunkLength + 2 + metadataLength;
+ var end = start + cypherChunkLength;
+ i++;
+ var box = new Uint8Array(u8.subarray(start, end));
+
+ // decrypt the chunk
+ var plaintext = Nacl.secretbox.open(box, nonce, key);
+ increment(nonce);
+
+ if (!plaintext) { return cb('DECRYPTION_ERROR'); }
+
+ _progress(end);
+ cb(void 0, plaintext);
+ };
+
+ var chunks = [];
+
+ var again = function () {
+ takeChunk(function (e, plaintext) {
+ if (e) {
+ return setTimeout(function () {
+ done(e);
+ });
+ }
+ if (plaintext) {
+ if ((2 + metadataLength + i * cypherChunkLength) < u8.length) { // not done
+ chunks.push(plaintext);
+ return setTimeout(again);
+ }
+ chunks.push(plaintext);
+ res.content = joinChunks(chunks);
+ return done(void 0, res);
+ }
+ done('UNEXPECTED_ENDING');
+ });
+ };
+
+ again();
+ };
+
+ // metadata
+ /* { filename: 'raccoon.jpg', type: 'image/jpeg' } */
+ var encrypt = function (u8, metadata, key) {
+ var nonce = createNonce();
+
+ // encode metadata
+ var plaintext = Nacl.util.decodeUTF8(JSON.stringify(metadata));
+
+ // if metadata is too large, drop the thumbnail.
+ if (plaintext.length > 65535) {
+ var temp = JSON.parse(JSON.stringify(metadata));
+ delete metadata.thumbnail;
+ plaintext = Nacl.util.decodeUTF8(JSON.stringify(temp));
+ }
+
+ var i = 0;
+
+ var state = 0;
+ var next = function (cb) {
+ if (state === 2) { return void cb(); }
+
+ var start;
+ var end;
+ var part;
+ var box;
+
+ if (state === 0) { // metadata...
+ part = new Uint8Array(plaintext);
+ box = Nacl.secretbox(part, nonce, key);
+ increment(nonce);
+
+ if (box.length > 65535) {
+ return void cb('METADATA_TOO_LARGE');
+ }
+ var prefixed = new Uint8Array(encodePrefix(box.length)
+ .concat(slice(box)));
+ state++;
+
+ return void cb(void 0, prefixed);
+ }
+
+ // encrypt the rest of the file...
+ start = i * plainChunkLength;
+ end = start + plainChunkLength;
+
+ part = u8.subarray(start, end);
+ box = Nacl.secretbox(part, nonce, key);
+ increment(nonce);
+ i++;
+
+ // regular data is done
+ if (i * plainChunkLength >= u8.length) { state = 2; }
+
+ return void cb(void 0, box);
+ };
+
+ return next;
+ };
+
+ return {
+ decrypt: decrypt,
+ encrypt: encrypt,
+ joinChunks: joinChunks,
+ computeEncryptedSize: computeEncryptedSize,
+ decryptMetadata: decryptMetadata,
+ fetchMetadata: fetchMetadata,
+ fetchDecryptedMetadata: fetchDecryptedMetadata,
+ };
+});
diff --git a/www/file/file.less b/www/oldfile/file.less
similarity index 100%
rename from www/file/file.less
rename to www/oldfile/file.less
diff --git a/www/oldfile/index.html b/www/oldfile/index.html
new file mode 100644
index 000000000..a72a3c60b
--- /dev/null
+++ b/www/oldfile/index.html
@@ -0,0 +1,30 @@
+
+
+
+
CryptPad
+
+
+
+
+
+
+
+
diff --git a/www/oldfile/inner.html b/www/oldfile/inner.html
new file mode 100644
index 000000000..0bdfd1c41
--- /dev/null
+++ b/www/oldfile/inner.html
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/www/oldfile/inner.js b/www/oldfile/inner.js
new file mode 100644
index 000000000..4be0b8483
--- /dev/null
+++ b/www/oldfile/inner.js
@@ -0,0 +1,14 @@
+define([
+ 'jquery',
+ 'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
+ 'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
+ 'less!/file/file.less',
+ 'less!/customize/src/less/cryptpad.less',
+ 'less!/customize/src/less/toolbar.less',
+], function ($) {
+ $('.loading-hidden').removeClass('loading-hidden');
+ // dirty hack to get rid the flash of the lock background
+ setTimeout(function () {
+ $('#app').addClass('ready');
+ }, 100);
+});
diff --git a/www/oldfile/main.js b/www/oldfile/main.js
new file mode 100644
index 000000000..136702c47
--- /dev/null
+++ b/www/oldfile/main.js
@@ -0,0 +1,269 @@
+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',
+
+ '/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,
+ };
+
+ 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 + '
';
+ text += '
' + Cryptpad.fixHTML(title) + '';
+ text += '
' + Messages._getKey('formattedMB', [sizeMb]) + '';
+ $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($('
'));
+ $dllabel.append(Cryptpad.fixHTML(metadata.name));
+
+ // don't display the size if you don't know it.
+ if (typeof(sizeM) === 'number') {
+ $dllabel.append($('
'));
+ $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
+ };
+
+ var FM = Cryptpad.createFileManager(fmConfig);
+
+ $form.find("#file").on('change', function (e) {
+ var file = e.target.files[0];
+ FM.handleFile(file);
+ });
+
+ // we're in upload mode
+ Cryptpad.removeLoadingScreen();
+ };
+
+ Cryptpad.ready(function () {
+ andThen();
+ Cryptpad.reportAppUsage();
+ });
+
+ });
+});
diff --git a/www/file/test/index.html b/www/oldfile/test/index.html
similarity index 100%
rename from www/file/test/index.html
rename to www/oldfile/test/index.html
diff --git a/www/file/test/main.js b/www/oldfile/test/main.js
similarity index 100%
rename from www/file/test/main.js
rename to www/oldfile/test/main.js