2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2011-07-19 19:48:11 +01:00
|
|
|
/**
|
|
|
|
* Handles the export requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2014-12-30 00:13:01 +01:00
|
|
|
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
|
2011-07-19 19:48:11 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const exporthtml = require('../utils/ExportHtml');
|
|
|
|
const exporttxt = require('../utils/ExportTxt');
|
|
|
|
const exportEtherpad = require('../utils/ExportEtherpad');
|
2024-02-23 19:48:55 +01:00
|
|
|
import fs from 'fs';
|
2020-11-23 13:24:19 -05:00
|
|
|
const settings = require('../utils/Settings');
|
2024-02-23 19:48:55 +01:00
|
|
|
import os from 'os';
|
2021-01-21 21:06:52 +00:00
|
|
|
const hooks = require('../../static/js/pluginfw/hooks');
|
2024-02-23 19:48:55 +01:00
|
|
|
import util from 'util';
|
2023-06-26 18:17:06 +01:00
|
|
|
const { checkValidRev } = require('../utils/checkValidRev');
|
2011-07-19 19:48:11 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
const fsp_writeFile = util.promisify(fs.writeFile);
|
|
|
|
const fsp_unlink = util.promisify(fs.unlink);
|
|
|
|
|
2019-02-15 22:30:17 +01:00
|
|
|
const tempDirectory = os.tmpdir();
|
2015-05-18 16:24:41 +01:00
|
|
|
|
2011-07-19 19:48:11 +01:00
|
|
|
/**
|
|
|
|
* do a requested export
|
2023-10-17 12:49:56 +02:00
|
|
|
* @param {Object} req the request object
|
|
|
|
* @param {Object} res the response object
|
|
|
|
* @param {String} padId the pad id to export
|
|
|
|
* @param {String} readOnlyId the read only id of the pad to export
|
|
|
|
* @param {String} type the type to export
|
2015-05-18 16:24:41 +01:00
|
|
|
*/
|
2024-02-23 19:48:55 +01:00
|
|
|
exports.doExport = async (req: any, res: any, padId: string, readOnlyId: string, type:string) => {
|
2020-09-16 14:57:27 -03:00
|
|
|
// avoid naming the read-only file as the original pad's id
|
2020-11-23 13:24:19 -05:00
|
|
|
let fileName = readOnlyId ? readOnlyId : padId;
|
2014-05-07 16:27:41 +01:00
|
|
|
|
2014-05-06 21:13:22 +01:00
|
|
|
// allow fileName to be overwritten by a hook, the type type is kept static for security reasons
|
2020-11-23 13:24:19 -05:00
|
|
|
const hookFileName = await hooks.aCallFirst('exportFileName', padId);
|
2019-01-31 08:55:36 +00:00
|
|
|
|
|
|
|
// if fileName is set then set it to the padId, note that fileName is returned as an array.
|
|
|
|
if (hookFileName.length) {
|
|
|
|
fileName = hookFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell the browser that this is a downloadable file
|
2020-11-23 13:24:19 -05:00
|
|
|
res.attachment(`${fileName}.${type}`);
|
2019-01-31 08:55:36 +00:00
|
|
|
|
2023-06-26 18:17:06 +01:00
|
|
|
if (req.params.rev !== undefined) {
|
|
|
|
// ensure revision is a number
|
|
|
|
// modify req, as we use it in a later call to exportConvert
|
|
|
|
req.params.rev = checkValidRev(req.params.rev);
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// if this is a plain text export, we can do this directly
|
|
|
|
// We have to over engineer this because tabs are stored as attributes and not plain text
|
2020-11-23 13:24:19 -05:00
|
|
|
if (type === 'etherpad') {
|
2021-06-29 19:13:10 +02:00
|
|
|
const pad = await exportEtherpad.getPadRaw(padId, readOnlyId);
|
2019-01-31 08:55:36 +00:00
|
|
|
res.send(pad);
|
2020-11-23 13:24:19 -05:00
|
|
|
} else if (type === 'txt') {
|
|
|
|
const txt = await exporttxt.getPadTXTDocument(padId, req.params.rev);
|
2019-01-31 08:55:36 +00:00
|
|
|
res.send(txt);
|
|
|
|
} else {
|
|
|
|
// render the html document
|
2021-06-29 19:13:10 +02:00
|
|
|
let html = await exporthtml.getPadHTMLDocument(padId, req.params.rev, readOnlyId);
|
2019-01-31 08:55:36 +00:00
|
|
|
|
|
|
|
// decide what to do with the html export
|
|
|
|
|
|
|
|
// if this is a html export, we can send this from here directly
|
2020-11-23 13:24:19 -05:00
|
|
|
if (type === 'html') {
|
2019-01-31 08:55:36 +00:00
|
|
|
// do any final changes the plugin might want to make
|
2020-11-23 13:24:19 -05:00
|
|
|
const newHTML = await hooks.aCallFirst('exportHTMLSend', html);
|
2019-01-31 08:55:36 +00:00
|
|
|
if (newHTML.length) html = newHTML;
|
|
|
|
res.send(html);
|
2021-03-17 18:35:16 -04:00
|
|
|
return;
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// else write the html export to a file
|
2020-11-23 13:24:19 -05:00
|
|
|
const randNum = Math.floor(Math.random() * 0xFFFFFFFF);
|
|
|
|
const srcFile = `${tempDirectory}/etherpad_export_${randNum}.html`;
|
2019-01-31 08:55:36 +00:00
|
|
|
await fsp_writeFile(srcFile, html);
|
|
|
|
|
|
|
|
// ensure html can be collected by the garbage collector
|
|
|
|
html = null;
|
|
|
|
|
2021-03-17 18:53:07 -04:00
|
|
|
// send the convert job to the converter (abiword, libreoffice, ..)
|
2020-11-23 13:24:19 -05:00
|
|
|
const destFile = `${tempDirectory}/etherpad_export_${randNum}.${type}`;
|
2019-01-31 08:55:36 +00:00
|
|
|
|
|
|
|
// Allow plugins to overwrite the convert in export process
|
2020-11-23 13:24:19 -05:00
|
|
|
const result = await hooks.aCallAll('exportConvert', {srcFile, destFile, req, res});
|
2019-01-31 08:55:36 +00:00
|
|
|
if (result.length > 0) {
|
|
|
|
// console.log("export handled by plugin", destFile);
|
|
|
|
} else {
|
2021-03-17 18:54:57 -04:00
|
|
|
const converter =
|
|
|
|
settings.soffice != null ? require('../utils/LibreOffice')
|
|
|
|
: settings.abiword != null ? require('../utils/Abiword')
|
|
|
|
: null;
|
2021-03-18 01:01:47 -04:00
|
|
|
await converter.convertFile(srcFile, destFile, type);
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send the file
|
|
|
|
await res.sendFile(destFile, null);
|
|
|
|
|
|
|
|
// clean up temporary files
|
|
|
|
await fsp_unlink(srcFile);
|
|
|
|
|
|
|
|
// 100ms delay to accommodate for slow windows fs
|
2020-11-23 13:24:19 -05:00
|
|
|
if (os.type().indexOf('Windows') > -1) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await fsp_unlink(destFile);
|
|
|
|
}
|
2021-01-21 21:06:52 +00:00
|
|
|
};
|