cryptpad/www/file/inner.js

272 lines
9.6 KiB
JavaScript
Raw Normal View History

define([
'jquery',
2017-09-12 16:40:11 +00:00
'/bower_components/chainpad-crypto/crypto.js',
'/common/toolbar3.js',
'/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',
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,
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 $dllabel = $dlform.find('label span');
var $progress = $('#cp-app-file-dlprogress');
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();
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'];
if (!uploadMode) {
displayed.push('fileshare');
}
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);
toolbar.$rightside.html('');
if (!uploadMode) {
2018-05-25 16:00:10 +00:00
var hexFileName = secret.channel;
2017-11-13 15:32:40 +00:00
var src = Hash.getBlobPathFromHex(hexFileName);
2018-05-25 16:00:10 +00:00
var key = secret.keys && secret.keys.cryptKey;
var cryptKey = Nacl.util.encodeBase64(key);
2017-09-12 16:40:11 +00:00
FileCrypto.fetchDecryptedMetadata(src, key, function (e, metadata) {
if (e) {
if (e === 'XHR_ERROR') {
2018-06-12 13:15:46 +00:00
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
// 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);
});
// Save to the drive or update the acces time
2017-09-12 16:40:11 +00:00
var title = document.title = metadata.name;
Title.updateTitle(title || Title.defaultTitle);
2018-05-30 12:36:29 +00:00
2019-06-13 09:17:43 +00:00
var owners = metadata.owners;
if (owners) {
common.setPadAttribute('owners', owners);
}
if (metadata.type) {
common.setPadAttribute('fileType', metadata.type);
}
2019-07-04 13:51:29 +00:00
toolbar.addElement(['pageTitle'], {
pageTitle: title,
title: Title.getTitleConfig(),
});
toolbar.$rightside.append(common.createButton('forget', true));
toolbar.$rightside.append(common.createButton('properties', true));
if (common.isLoggedIn()) {
toolbar.$rightside.append(common.createButton('hashtag', true));
}
2017-09-12 16:40:11 +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); }
};
var $mt = $dlview.find('media-tag');
2018-05-25 16:00:10 +00:00
$mt.attr('src', src);
2017-09-12 16:40:11 +00:00
$mt.attr('data-crypto-key', 'cryptpad:'+cryptKey);
var rightsideDisplayed = false;
2018-06-07 13:06:20 +00:00
MediaTag($mt[0]).on('complete', function (decrypted) {
2017-09-12 16:40:11 +00:00
$dlview.show();
$dlform.hide();
var $dlButton = $dlview.find('media-tag button');
if (ev) { $dlButton.click(); }
if (!rightsideDisplayed) {
toolbar.$rightside
.append(common.createButton('export', true, {}, function () {
2018-06-07 13:06:20 +00:00
saveAs(decrypted.content, decrypted.metadata.name);
}));
2017-09-12 16:40:11 +00:00
rightsideDisplayed = true;
}
// 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 {
cb();
}
2018-06-07 13:06:20 +00:00
}).on('progress', function (data) {
var p = data.progress +'%';
2017-09-12 16:40:11 +00:00
$progress.width(p);
2018-06-07 13:06:20 +00:00
}).on('error', function (err) {
console.error(err);
2017-09-12 16:40:11 +00:00
});
};
var todoBigFile = function (sizeMb) {
$dlform.show();
UI.removeLoadingScreen();
2017-09-12 16:40:11 +00:00
$dllabel.append($('<br>'));
$dllabel.append(Util.fixHTML(metadata.name));
2017-09-12 16:40:11 +00:00
// 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) {
$appContainer.css('background-color',
common.getAppConfig().appBackgroundColor);
if (err) { UI.alert(err); }
2017-09-12 16:40:11 +00:00
});
};
if (typeof(sizeMb) === 'number' && sizeMb < 5) { return void onClick(); }
$dlform.find('#cp-app-file-dlfile, #cp-app-file-dlprogress').click(onClick);
};
2018-04-27 15:23:23 +00:00
common.getFileSize(hexFileName, function (e, data) {
2017-09-12 16:40:11 +00:00
if (e) {
return void UI.errorLoadingScreen(e);
2017-09-12 16:40:11 +00:00
}
2017-11-13 15:32:40 +00:00
var size = Util.bytesToMegabytes(data);
2017-09-12 16:40:11 +00:00
return void todoBigFile(size);
});
});
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 () {
2017-09-13 14:19:26 +00:00
common.setLoginRedirect(function () {
common.gotoURL('/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();
});