cryptpad/www/convert/inner.js

396 lines
14 KiB
JavaScript
Raw Normal View History

2023-10-20 22:35:26 +08:00
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-06-22 18:08:03 +08:00
define([
'jquery',
'/api/config',
'/components/chainpad-crypto/crypto.js',
2021-06-22 18:08:03 +08:00
'/common/toolbar.js',
'/components/nthen/index.js',
2021-06-22 18:08:03 +08:00
'/common/sframe-common.js',
'/common/hyperscript.js',
'/customize/messages.js',
'/common/common-interface.js',
2021-10-04 22:06:21 +08:00
'/common/common-util.js',
'/common/outer/worker-channel.js',
2021-11-02 19:42:44 +08:00
'/common/outer/x2t.js',
'/components/file-saver/FileSaver.min.js',
'css!/components/bootstrap/dist/css/bootstrap.min.css',
'css!/components/components-font-awesome/css/font-awesome.min.css',
2021-06-22 18:08:03 +08:00
'less!/convert/app-convert.less',
], function (
$,
ApiConfig,
Crypto,
Toolbar,
nThen,
SFCommon,
h,
Messages,
2021-10-04 22:06:21 +08:00
UI,
Util,
2021-11-02 19:42:44 +08:00
Channel,
X2T
2021-06-22 18:08:03 +08:00
)
{
2021-10-04 22:06:21 +08:00
var APP = window.APP = {};
2021-11-02 19:42:44 +08:00
var CURRENT_VERSION = X2T.CURRENT_VERSION;
2021-06-22 18:08:03 +08:00
var common;
var sFrameChan;
2021-10-04 22:06:21 +08:00
var getFileType = function (type) {
var file = {};
switch(type) {
case 'doc':
file.type = 'docx';
file.title = 'document.docx';
file.doc = 'text';
break;
case 'sheet':
file.type = 'xlsx';
file.title = 'spreadsheet.xlsx';
file.doc = 'spreadsheet';
break;
case 'slide':
file.type = 'pptx';
file.title = 'presentation.pptx';
file.doc = 'presentation';
break;
}
return file;
};
APP.getImageURL = function(name, callback) {
if (name && /^data:image/.test(name)) { return void callback(''); }
if (!Array.isArray(APP.images)) { return void callback(''); }
APP.images.some(function (obj) {
if (obj.name !== name) { return; }
var blob = new Blob([obj.data], {type: "application/bin;charset=utf-8"});
var blobUrl = URL.createObjectURL(blob);
callback(blobUrl);
return true;
});
};
var ooChannel = {};
var makeChannel = function () {
var msgEv = Util.mkEvent();
var iframe = $('#cp-sidebarlayout-rightside > iframe')[0].contentWindow;
window.addEventListener('message', function (msg) {
if (msg.source !== iframe) { return; }
msgEv.fire(msg);
});
var postMsg = function (data) { iframe.postMessage(data, '*'); };
Channel.create(msgEv, postMsg, function (chan) {
var send = ooChannel.send = function (obj) { chan.event('CMD', obj); };
chan.on('CMD', function (obj) {
if (obj.type !== "auth") { return; }
send({
type: "authChanges",
changes: []
});
send({
type: "auth",
result: 1,
sessionId: 'cryptpad',
participants:[],
locks: [],
changes: [],
changesIndex: 0,
indexUser: 0,
buildVersion: "5.2.6",
buildNumber: 2,
licenseType: 3,
});
send({
type: "documentOpen",
data: {"type":"open","status":"ok","data":{"Editor.bin":obj.openCmd.url}}
});
});
});
};
var loadOO = function (blob, type, name, cb) {
var s = h('script', {
type:'text/javascript',
src: '/common/onlyoffice/dist/'+CURRENT_VERSION+'/web-apps/apps/api/documents/api.js'
2021-10-04 22:06:21 +08:00
});
var file = getFileType(type);
APP.$rightside.append(s);
var url = URL.createObjectURL(blob);
var getWindow = function () {
return window.frames && window.frames[0];
};
var getEditor = function () {
var w = getWindow();
if (!w) { return; }
return w.editor || w.editorCell;
};
var getContent = function () {
try {
return getEditor().asc_nativeGetFile();
} catch (e) {
console.error(e);
return;
}
};
var ooconfig = {
"document": {
"fileType": file.type,
"key": "fresh",
"title": file.title,
"url": url,
"permissions": { "print": true, }
},
"documentType": file.doc,
"editorConfig": {
"user": {
"id": "0",
"firstname": Messages.anonymous,
"name": Messages.anonymous,
},
"mode": "view",
"lang": "en"
},
"events": {
"onDocumentReady": function () {
console.error('READY');
var e = getEditor();
var x2tConvertData = function (data, name, typeTarget, cb) {
var fonts = e && e.FontLoader.fontInfos;
var files = e && e.FontLoader.fontFiles.map(function (f) {
return { 'Id': f.Id, };
});
var sframeChan = common.getSframeChannel();
sframeChan.query('Q_OO_CONVERT', {
data: data,
fileName: name,
outputFormat: typeTarget,
images: (e && window.frames[0].AscCommon.g_oDocumentUrls.urls) || {},
fonts: fonts,
fonts_files: files,
}, cb, {
raw: true
});
};
APP.printPdf = function (obj) {
var bin = getContent();
x2tConvertData({
buffer: obj.data,
bin: bin
}, 'output.bin', 'pdf', function (err, obj) {
if (!obj || !obj.data) { return; }
var blob = new Blob([obj.data], {type: "application/pdf"});
cb(blob);
});
};
setTimeout(function () {
if (file.type === "xlsx") {
var d = e.asc_nativePrint(undefined, undefined, 0x101).ImData;
APP.printPdf({
data: d.data
});
return;
}
return void e.asc_Print({});
});
}
}
};
APP.docEditor = new window.DocsAPI.DocEditor("cp-oo-placeholder", ooconfig);
makeChannel();
};
var convertData = function (data, name, typeTarget, cb) {
var sframeChan = common.getSframeChannel();
sframeChan.query('Q_OO_CONVERT', {
data: data,
fileName: name,
outputFormat: typeTarget,
}, cb, {
raw: true
});
};
var x2tConverter = function (typeSrc, typeTarget, type) {
2021-06-22 18:08:03 +08:00
return function (data, name, cb) {
2021-10-04 22:06:21 +08:00
if (typeTarget === 'pdf') {
// Converting to PDF? we need to load OO from a bin
var next = function () {
var blob = new Blob([data], {type: "application/bin;charset=utf-8"});
loadOO(blob, type, name, function (blob) {
cb(blob);
});
};
if (typeSrc === 'bin') { return next(); }
convertData(data, name, 'bin', function (err, obj) {
if (err || !obj || !obj.data) {
UI.warn(Messages.error);
return void cb();
}
name += '.bin';
data = obj.data;
APP.images = obj.images;
next();
});
return;
}
convertData(data, name, typeTarget, function (err, obj) {
if (err || !obj || !obj.data) {
UI.warn(Messages.error);
2021-10-04 22:06:21 +08:00
return void cb();
2021-06-22 18:08:03 +08:00
}
cb(obj.data, obj.images);
2021-06-22 18:08:03 +08:00
});
};
};
var CONVERTERS = {
xlsx: {
2021-10-04 22:06:21 +08:00
pdf: x2tConverter('xlsx', 'pdf', 'sheet'),
ods: x2tConverter('xlsx', 'ods', 'sheet'),
bin: x2tConverter('xlsx', 'bin', 'sheet'),
2021-06-22 18:08:03 +08:00
},
ods: {
2021-10-04 22:06:21 +08:00
pdf: x2tConverter('ods', 'pdf', 'sheet'),
xlsx: x2tConverter('ods', 'xlsx', 'sheet'),
bin: x2tConverter('ods', 'bin', 'sheet'),
2021-06-22 18:08:03 +08:00
},
odt: {
2021-10-04 22:06:21 +08:00
docx: x2tConverter('odt', 'docx', 'doc'),
txt: x2tConverter('odt', 'txt', 'doc'),
bin: x2tConverter('odt', 'bin', 'doc'),
pdf: x2tConverter('odt', 'pdf', 'doc'),
2021-06-22 18:08:03 +08:00
},
docx: {
2021-10-04 22:06:21 +08:00
odt: x2tConverter('docx', 'odt', 'doc'),
txt: x2tConverter('docx', 'txt', 'doc'),
bin: x2tConverter('docx', 'bin', 'doc'),
pdf: x2tConverter('docx', 'pdf', 'doc'),
2021-06-22 18:08:03 +08:00
},
txt: {
2021-10-04 22:06:21 +08:00
odt: x2tConverter('txt', 'odt', 'doc'),
docx: x2tConverter('txt', 'docx', 'doc'),
bin: x2tConverter('txt', 'bin', 'doc'),
pdf: x2tConverter('txt', 'pdf', 'doc'),
2021-06-22 18:08:03 +08:00
},
odp: {
2021-10-04 22:06:21 +08:00
pptx: x2tConverter('odp', 'pptx', 'slide'),
bin: x2tConverter('odp', 'bin', 'slide'),
pdf: x2tConverter('odp', 'pdf', 'slide'),
2021-06-22 18:08:03 +08:00
},
pptx: {
2021-10-04 22:06:21 +08:00
odp: x2tConverter('pptx', 'odp', 'slide'),
bin: x2tConverter('pptx', 'bin', 'slide'),
pdf: x2tConverter('pptx', 'pdf', 'slide'),
2021-06-22 18:08:03 +08:00
},
};
Messages.convertPage = "Convert"; // TODO: hard-coded text since 4.11.0
Messages.convert_hint = "Pick the file you want to convert. The list of output format will be visible afterwards.";
Messages.convert_unsupported = "UNSUPPORTED FILE TYPE :(";
2021-06-22 18:08:03 +08:00
var createToolbar = function () {
var displayed = ['useradmin', 'newpad', 'limit', 'pageTitle', 'notifications'];
var configTb = {
displayed: displayed,
sfCommon: common,
$container: APP.$toolbar,
pageTitle: Messages.convertPage,
metadataMgr: common.getMetadataMgr(),
};
APP.toolbar = Toolbar.create(configTb);
APP.toolbar.$rightside.hide();
};
nThen(function (waitFor) {
$(waitFor(UI.addLoadingScreen));
SFCommon.create(waitFor(function (c) { APP.common = common = c; }));
}).nThen(function (waitFor) {
APP.$container = $('#cp-sidebarlayout-container');
APP.$toolbar = $('#cp-toolbar');
APP.$leftside = $('<div>', {id: 'cp-sidebarlayout-leftside'}).appendTo(APP.$container);
APP.$rightside = $('<div>', {id: 'cp-sidebarlayout-rightside'}).appendTo(APP.$container);
2021-10-04 22:06:21 +08:00
$(h('div#cp-oo-placeholder')).appendTo(APP.$rightside);
2021-06-22 18:08:03 +08:00
sFrameChan = common.getSframeChannel();
sFrameChan.onReady(waitFor());
}).nThen(function (/*waitFor*/) {
createToolbar();
var hint = h('p.cp-convert-hint', Messages.convert_hint);
var picker = h('input', {
type: 'file'
});
APP.$rightside.append([hint, picker]);
$(picker).on('change', function () {
2021-09-21 19:40:00 +08:00
APP.$rightside.find('button, div.notice').remove();
2021-06-22 18:08:03 +08:00
var file = picker.files[0];
var name = file && file.name;
var reader = new FileReader();
var parsed = file && file.name && /.+\.([^.]+)$/.exec(file.name);
var ext = parsed && parsed[1];
reader.onload = function (e) {
if (CONVERTERS[ext]) {
Object.keys(CONVERTERS[ext]).forEach(function (to) {
var button = h('button.btn', to);
$(button).click(function () {
CONVERTERS[ext][to](new Uint8Array(e.target.result), name, function (a) {
var n = name.slice(0, -ext.length) + to;
var blob = new Blob([a], {type: "application/bin;charset=utf-8"});
window.saveAs(blob, n);
});
}).appendTo(APP.$rightside);
});
2021-09-21 19:40:00 +08:00
} else {
var notice = h('div.notice', Messages.convert_unsupported);
APP.$rightside.append(notice);
2021-06-22 18:08:03 +08:00
}
};
if (ext === 'bin') {
var reader2 = new FileReader();
reader2.onload = function (e) {
var str = e.target.result;
var type = str.slice(0,4);
var c = CONVERTERS['bin'] = {};
if (type === "XLSY") {
2021-10-04 22:06:21 +08:00
c.ods = x2tConverter('bin', 'ods', 'sheet');
c.xlsx = x2tConverter('bin', 'xlsx', 'sheet');
c.pdf = x2tConverter('bin', 'pdf', 'sheet');
} else if (type === "PPTY") {
2021-10-04 22:06:21 +08:00
c.odp = x2tConverter('bin', 'odp', 'slide');
c.pptx = x2tConverter('bin', 'pptx', 'slide');
c.pdf = x2tConverter('bin', 'pdf', 'slide');
} else if (type === "DOCY") {
2021-10-04 22:06:21 +08:00
c.odt = x2tConverter('bin', 'odt', 'doc');
c.docx = x2tConverter('bin', 'docx', 'doc');
c.pdf = x2tConverter('bin', 'pdf', 'doc');
} else {
return void console.error('Unsupported');
}
reader.readAsArrayBuffer(file, 'application/octet-stream');
};
return void reader2.readAsText(file);
}
2021-06-22 18:08:03 +08:00
reader.readAsArrayBuffer(file, 'application/octet-stream');
});
UI.removeLoadingScreen();
});
});