cryptpad/www/file/inner.js

307 lines
12 KiB
JavaScript
Raw Normal View History

define([
'jquery',
2017-09-12 16:40:11 +00:00
'/bower_components/chainpad-crypto/crypto.js',
2020-05-07 09:58:58 +00:00
'/common/toolbar.js',
2017-09-12 16:40:11 +00:00
'/bower_components/nthen/index.js',
'/common/sframe-common.js',
'/common/common-realtime.js',
'/common/common-util.js',
2017-11-13 15:32:40 +00:00
'/common/common-hash.js',
'/common/common-interface.js',
2020-11-19 16:58:40 +00:00
'/common/hyperscript.js',
2017-11-13 15:32:40 +00:00
'/customize/messages.js',
2017-09-12 16:40:11 +00:00
'/file/file-crypto.js',
'/common/media-tag.js',
'/bower_components/file-saver/FileSaver.min.js',
'css!/bower_components/bootstrap/dist/css/bootstrap.min.css',
2018-03-21 17:31:53 +00:00
'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
2018-07-14 13:15:23 +00:00
'less!/file/app-file.less',
2017-09-12 16:40:11 +00:00
], function (
$,
Crypto,
Toolbar,
nThen,
SFCommon,
CommonRealtime,
Util,
2017-11-13 15:32:40 +00:00
Hash,
UI,
2020-11-19 16:58:40 +00:00
h,
2017-11-13 15:32:40 +00:00
Messages,
2017-09-12 16:40:11 +00:00
FileCrypto,
MediaTag)
{
var saveAs = window.saveAs;
var Nacl = window.nacl;
2017-11-13 15:32:40 +00:00
var APP = window.APP = {};
2018-06-07 13:06:20 +00:00
MediaTag.setDefaultConfig('download', {
text: Messages.download_mt_button
});
2017-09-12 16:40:11 +00:00
var andThen = function (common) {
var $appContainer = $('#cp-app-file-content');
var $form = $('#cp-app-file-upload-form');
var $dlform = $('#cp-app-file-download-form');
var $dlview = $('#cp-app-file-download-view');
var $label = $form.find('label');
var $bar = $('.cp-toolbar-container');
var $body = $('body');
$body.on('dragover', function (e) { e.preventDefault(); });
$body.on('drop', function (e) { e.preventDefault(); });
var uploadMode = false;
var secret;
var metadataMgr = common.getMetadataMgr();
var priv = metadataMgr.getPrivateData();
2019-08-26 16:17:39 +00:00
var fileHost = priv.fileHost || priv.origin || '';
2017-09-12 16:40:11 +00:00
if (!priv.filehash) {
uploadMode = true;
} else {
2018-05-25 16:00:10 +00:00
secret = Hash.getSecrets('file', priv.filehash, priv.password);
2017-09-12 16:40:11 +00:00
if (!secret.keys) { throw new Error("You need a hash"); }
}
var Title = common.createTitle({});
var displayed = ['useradmin', 'newpad', 'limit', 'upgrade', 'notifications'];
2017-09-12 16:40:11 +00:00
if (!uploadMode) {
displayed.push('fileshare');
2020-05-07 13:26:12 +00:00
displayed.push('access');
2017-09-12 16:40:11 +00:00
}
var configTb = {
displayed: displayed,
//hideDisplayName: true,
$container: $bar,
metadataMgr: metadataMgr,
sfCommon: common,
};
if (uploadMode) {
2017-10-09 09:52:34 +00:00
displayed.push('pageTitle');
2017-09-12 16:40:11 +00:00
configTb.pageTitle = Messages.upload_title;
}
var toolbar = APP.toolbar = Toolbar.create(configTb);
if (!uploadMode) {
2020-11-19 16:58:40 +00:00
(function () {
Messages.download = "Download"; // XXX
Messages.decrypt = "Decrypt"; // XXX
var progress = h('div.cp-app-file-progress');
var progressTxt = h('span.cp-app-file-progress-txt');
var $progress = $(progress);
var $progressTxt = $(progressTxt);
var downloadEl = h('span.cp-app-file-progress-dl', Messages.download);
var decryptEl = h('span.cp-app-file-progress-dc', Messages.decrypt);
var progressContainer = h('div.cp-app-file-progress-container', [
downloadEl,
decryptEl,
progress
]);
var hexFileName = secret.channel;
var src = fileHost + Hash.getBlobPathFromHex(hexFileName);
var key = secret.keys && secret.keys.cryptKey;
var cryptKey = Nacl.util.encodeBase64(key);
FileCrypto.fetchDecryptedMetadata(src, key, function (e, metadata) {
if (e) {
if (e === 'XHR_ERROR') {
return void UI.errorLoadingScreen(Messages.download_resourceNotAvailable, false, function () {
common.gotoURL('/file/');
});
}
return void console.error(e);
}
2018-05-30 12:36:29 +00:00
2020-11-19 16:58:40 +00:00
// Add pad attributes when the file is saved in the drive
Title.onTitleChange(function () {
var owners = metadata.owners;
if (owners) { common.setPadAttribute('owners', owners); }
common.setPadAttribute('fileType', metadata.type);
});
$(document).on('cpPadStored', function () {
var owners = metadata.owners;
if (owners) { common.setPadAttribute('owners', owners); }
common.setPadAttribute('fileType', metadata.type);
});
// Save to the drive or update the acces time
var title = document.title = metadata.name;
Title.updateTitle(title || Title.defaultTitle);
var owners = metadata.owners;
2020-11-19 16:58:40 +00:00
if (owners) {
common.setPadAttribute('owners', owners);
}
if (metadata.type) {
common.setPadAttribute('fileType', metadata.type);
}
2018-05-30 12:36:29 +00:00
2020-11-19 16:58:40 +00:00
toolbar.addElement(['pageTitle'], {
pageTitle: title,
title: Title.getTitleConfig(),
});
toolbar.$drawer.append(common.createButton('forget', true));
toolbar.$drawer.append(common.createButton('properties', true));
if (common.isLoggedIn()) {
toolbar.$drawer.append(common.createButton('hashtag', true));
}
toolbar.$file.show();
2017-09-12 16:40:11 +00:00
2020-11-19 16:58:40 +00:00
var displayFile = function (ev, sizeMb, CB) {
var called_back;
var cb = function (e) {
if (called_back) { return; }
called_back = true;
if (CB) { CB(e); }
};
2017-09-12 16:40:11 +00:00
2020-11-19 16:58:40 +00:00
var $mt = $dlview.find('media-tag');
$mt.attr('src', src);
$mt.attr('data-crypto-key', 'cryptpad:'+cryptKey);
2017-09-12 16:40:11 +00:00
2020-11-19 16:58:40 +00:00
var rightsideDisplayed = false;
2017-09-12 16:40:11 +00:00
2020-11-24 15:38:31 +00:00
MediaTag($mt[0], {
force: true // Download starts automatically
}).on('complete', function (decrypted) {
2020-11-19 16:58:40 +00:00
$dlview.show();
$dlform.hide();
var $dlButton = $dlview.find('media-tag button');
if (ev) { $dlButton.click(); }
2017-09-12 16:40:11 +00:00
2020-11-19 16:58:40 +00:00
if (!rightsideDisplayed) {
toolbar.$drawer
.append(common.createButton('export', true, {}, function () {
saveAs(decrypted.content, decrypted.metadata.name);
}));
rightsideDisplayed = true;
}
2017-09-12 16:40:11 +00:00
2020-11-19 16:58:40 +00:00
// make pdfs big
var toolbarHeight = $('#cp-toolbar').height();
var $another_iframe = $('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 {
2017-09-12 16:40:11 +00:00
cb();
2020-11-19 16:58:40 +00:00
}
}).on('progress', function (data) {
if (data.progress > 75) { return; }
var p = data.progress +'%';
$progress.width(p);
$progressTxt.text(Math.floor(data.progress) + '%');
}).on('error', function (err) {
console.error(err);
});
};
// XXX Update "download_button" key to use "download" first and "decrypt" second
var todoBigFile = function (sizeMb) {
$dlform.show();
UI.removeLoadingScreen();
var button = h('button.btn.btn-primary', {
title: Messages.download_button
}, Messages.download_button);
$dlform.append([
h('h2', Util.fixHTML(metadata.name)),
h('div.cp-button-container', [
button,
progressTxt
]),
]);
// don't display the size if you don't know it.
if (typeof(sizeMb) === 'number') {
$dlform.find('h2').append(' - ' +
Messages._getKey('formattedMB', [sizeMb]));
}
var decrypting = false;
var onClick = function (ev) {
if (decrypting) { return; }
decrypting = true;
$(button).prop('disabled', 'disabled');
$dlform.append(progressContainer);
displayFile(ev, sizeMb, function (err) {
$appContainer.css('background-color',
common.getAppConfig().appBackgroundColor);
if (err) { UI.alert(err); }
2017-09-12 16:40:11 +00:00
});
2020-11-19 16:58:40 +00:00
};
if (typeof(sizeMb) === 'number' && sizeMb < 5) { return void onClick(); }
$(button).click(onClick);
};
common.getFileSize(hexFileName, function (e, data) {
if (e) {
return void UI.errorLoadingScreen(e);
2017-09-12 16:40:11 +00:00
}
2020-11-19 16:58:40 +00:00
var size = Util.bytesToMegabytes(data);
return void todoBigFile(size);
2017-09-12 16:40:11 +00:00
});
});
2020-11-19 16:58:40 +00:00
})();
2017-09-12 16:40:11 +00:00
return;
}
2017-09-13 14:19:26 +00:00
// we're in upload mode
2017-09-12 16:40:11 +00:00
if (!common.isLoggedIn()) {
UI.removeLoadingScreen();
return UI.alert(Messages.upload_mustLogin, function () {
common.setLoginRedirect('login');
2017-09-12 16:40:11 +00:00
});
}
$form.css({
display: 'block',
});
var fmConfig = {
dropArea: $form,
hoverArea: $label,
body: $body,
2018-05-25 16:00:10 +00:00
keepTable: true // Don't fadeOut the table with the uploaded files
2017-09-12 16:40:11 +00:00
};
var FM = common.createFileManager(fmConfig);
$form.find("#cp-app-file-upfile").on('change', function (e) {
var file = e.target.files[0];
FM.handleFile(file);
});
UI.removeLoadingScreen();
2017-09-12 16:40:11 +00:00
};
var main = function () {
var common;
nThen(function (waitFor) {
$(waitFor(function () {
UI.addLoadingScreen();
2017-09-12 16:40:11 +00:00
}));
SFCommon.create(waitFor(function (c) { APP.common = common = c; }));
}).nThen(function (/*waitFor*/) {
common.getSframeChannel().onReady(function () {
andThen(common);
});
});
};
main();
});