2011-07-21 21:13:58 +02:00
|
|
|
/**
|
|
|
|
* Handles the import requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 16:26:41 +02:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2012-11-27 00:11:45 +01:00
|
|
|
* 2012 Iván Eixarch
|
2014-12-30 00:12:26 +01:00
|
|
|
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
|
2011-07-21 21:13:58 +02: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.
|
|
|
|
*/
|
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
var padManager = require("../db/PadManager")
|
2012-11-23 19:06:18 +01:00
|
|
|
, padMessageHandler = require("./PadMessageHandler")
|
|
|
|
, fs = require("fs")
|
2012-11-23 22:55:25 +01:00
|
|
|
, path = require("path")
|
2012-11-23 19:06:18 +01:00
|
|
|
, settings = require('../utils/Settings')
|
|
|
|
, formidable = require('formidable')
|
|
|
|
, os = require("os")
|
2013-09-27 15:47:34 +02:00
|
|
|
, importHtml = require("../utils/ImportHtml")
|
2014-12-29 20:57:58 +01:00
|
|
|
, importEtherpad = require("../utils/ImportEtherpad")
|
2013-11-27 01:23:11 +01:00
|
|
|
, log4js = require("log4js")
|
2019-01-31 09:55:36 +01:00
|
|
|
, hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks.js")
|
|
|
|
, util = require("util");
|
2012-11-23 18:18:03 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
let fsp_exists = util.promisify(fs.exists);
|
|
|
|
let fsp_rename = util.promisify(fs.rename);
|
|
|
|
let fsp_readFile = util.promisify(fs.readFile);
|
|
|
|
let fsp_unlink = util.promisify(fs.unlink)
|
|
|
|
|
|
|
|
let convertor = null;
|
|
|
|
let exportExtension = "htm";
|
2018-03-08 12:55:00 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// load abiword only if it is enabled and if soffice is disabled
|
|
|
|
if (settings.abiword != null && settings.soffice === null) {
|
2018-03-08 12:55:00 +01:00
|
|
|
convertor = require("../utils/Abiword");
|
2019-02-08 23:20:57 +01:00
|
|
|
}
|
2018-03-08 12:55:00 +01:00
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// load soffice only if it is enabled
|
|
|
|
if (settings.soffice != null) {
|
2018-03-08 12:55:00 +01:00
|
|
|
convertor = require("../utils/LibreOffice");
|
|
|
|
exportExtension = "html";
|
|
|
|
}
|
2012-12-06 11:49:04 +01:00
|
|
|
|
2019-02-15 22:30:17 +01:00
|
|
|
const tmpDirectory = os.tmpdir();
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2011-07-21 21:13:58 +02:00
|
|
|
/**
|
|
|
|
* do a requested import
|
2019-02-08 23:20:57 +01:00
|
|
|
*/
|
2019-01-31 09:55:36 +01:00
|
|
|
async function doImport(req, res, padId)
|
2011-07-21 21:13:58 +02:00
|
|
|
{
|
2013-09-27 15:47:34 +02:00
|
|
|
var apiLogger = log4js.getLogger("ImportHandler");
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
// pipe to a file
|
|
|
|
// convert file to html via abiword or soffice
|
|
|
|
// set html in the pad
|
2013-11-27 01:23:11 +01:00
|
|
|
var randNum = Math.floor(Math.random()*0xFFFFFFFF);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2018-03-08 12:55:00 +01:00
|
|
|
// setting flag for whether to use convertor or not
|
2019-01-31 09:55:36 +01:00
|
|
|
let useConvertor = (convertor != null);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
let form = new formidable.IncomingForm();
|
|
|
|
form.keepExtensions = true;
|
|
|
|
form.uploadDir = tmpDirectory;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// locally wrapped Promise, since form.parse requires a callback
|
|
|
|
let srcFile = await new Promise((resolve, reject) => {
|
|
|
|
form.parse(req, function(err, fields, files) {
|
|
|
|
if (err || files.file === undefined) {
|
|
|
|
// the upload failed, stop at this point
|
|
|
|
if (err) {
|
|
|
|
console.warn("Uploading Error: " + err.stack);
|
2013-11-27 01:23:11 +01:00
|
|
|
}
|
2019-01-31 09:55:36 +01:00
|
|
|
reject("uploadFailed");
|
2018-10-31 23:00:45 +01:00
|
|
|
}
|
2020-04-03 12:30:12 +02:00
|
|
|
if(!files.file){ // might not be a graceful fix but it works
|
|
|
|
reject("uploadFailed");
|
|
|
|
}else{
|
|
|
|
resolve(files.file.path);
|
|
|
|
}
|
2019-01-31 09:55:36 +01:00
|
|
|
});
|
|
|
|
});
|
2018-10-31 23:00:45 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// ensure this is a file ending we know, else we change the file ending to .txt
|
|
|
|
// this allows us to accept source code files like .c or .java
|
|
|
|
let fileEnding = path.extname(srcFile).toLowerCase()
|
|
|
|
, knownFileEndings = [".txt", ".doc", ".docx", ".pdf", ".odt", ".html", ".htm", ".etherpad", ".rtf"]
|
|
|
|
, fileEndingUnknown = (knownFileEndings.indexOf(fileEnding) < 0);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
if (fileEndingUnknown) {
|
|
|
|
// the file ending is not known
|
2018-10-31 23:09:27 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
if (settings.allowUnknownFileEnds === true) {
|
|
|
|
// we need to rename this file with a .txt ending
|
|
|
|
let oldSrcFile = srcFile;
|
2018-10-31 23:09:27 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
srcFile = path.join(path.dirname(srcFile), path.basename(srcFile, fileEnding) + ".txt");
|
2020-03-16 19:56:50 +01:00
|
|
|
await fsp_rename(oldSrcFile, srcFile);
|
2019-01-31 09:55:36 +01:00
|
|
|
} else {
|
|
|
|
console.warn("Not allowing unknown file type to be imported", fileEnding);
|
|
|
|
throw "uploadFailed";
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
let destFile = path.join(tmpDirectory, "etherpad_import_" + randNum + "." + exportExtension);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// Logic for allowing external Import Plugins
|
2020-03-17 14:07:14 +01:00
|
|
|
let result = await hooks.aCallAll("import", { srcFile, destFile, fileEnding });
|
2019-01-31 09:55:36 +01:00
|
|
|
let importHandledByPlugin = (result.length > 0); // This feels hacky and wrong..
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
let fileIsEtherpad = (fileEnding === ".etherpad");
|
|
|
|
let fileIsHTML = (fileEnding === ".html" || fileEnding === ".htm");
|
|
|
|
let fileIsTXT = (fileEnding === ".txt");
|
2018-10-31 23:15:01 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
if (fileIsEtherpad) {
|
|
|
|
// we do this here so we can see if the pad has quite a few edits
|
|
|
|
let _pad = await padManager.getPad(padId);
|
|
|
|
let headCount = _pad.head;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
if (headCount >= 10) {
|
|
|
|
apiLogger.warn("Direct database Import attempt of a pad that already has content, we won't be doing this");
|
|
|
|
throw "padHasData";
|
|
|
|
}
|
2018-10-31 23:20:55 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
const fsp_readFile = util.promisify(fs.readFile);
|
|
|
|
let _text = await fsp_readFile(srcFile, "utf8");
|
2019-02-08 15:46:05 +01:00
|
|
|
req.directDatabaseAccess = true;
|
2019-01-31 09:55:36 +01:00
|
|
|
await importEtherpad.setPadRaw(padId, _text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert file to html if necessary
|
2019-02-08 15:46:05 +01:00
|
|
|
if (!importHandledByPlugin && !req.directDatabaseAccess) {
|
2019-01-31 09:55:36 +01:00
|
|
|
if (fileIsTXT) {
|
|
|
|
// Don't use convertor for text files
|
|
|
|
useConvertor = false;
|
|
|
|
}
|
2018-10-31 23:20:55 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// See https://github.com/ether/etherpad-lite/issues/2572
|
|
|
|
if (fileIsHTML || !useConvertor) {
|
|
|
|
// if no convertor only rename
|
|
|
|
fs.renameSync(srcFile, destFile);
|
|
|
|
} else {
|
|
|
|
// @TODO - no Promise interface for convertors (yet)
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
convertor.convertFile(srcFile, destFile, exportExtension, function(err) {
|
|
|
|
// catch convert errors
|
|
|
|
if (err) {
|
|
|
|
console.warn("Converting Error:", err);
|
|
|
|
reject("convertFailed");
|
2018-10-31 23:20:55 +01:00
|
|
|
}
|
2019-01-31 09:55:36 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
if (!useConvertor && !req.directDatabaseAccess) {
|
2019-01-31 09:55:36 +01:00
|
|
|
// Read the file with no encoding for raw buffer access.
|
|
|
|
let buf = await fsp_readFile(destFile);
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// Check if there are only ascii chars in the uploaded file
|
|
|
|
let isAscii = ! Array.prototype.some.call(buf, c => (c > 240));
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
if (!isAscii) {
|
|
|
|
throw "uploadFailed";
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// get the pad object
|
|
|
|
let pad = await padManager.getPad(padId);
|
2018-10-31 23:24:56 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// read the text
|
|
|
|
let text;
|
2018-10-31 23:24:56 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
if (!req.directDatabaseAccess) {
|
2019-01-31 09:55:36 +01:00
|
|
|
text = await fsp_readFile(destFile, "utf8");
|
2014-12-29 20:57:58 +01:00
|
|
|
|
2020-03-29 14:06:31 +02:00
|
|
|
/*
|
|
|
|
* The <title> tag needs to be stripped out, otherwise it is appended to the
|
|
|
|
* pad.
|
|
|
|
*
|
|
|
|
* Moreover, when using LibreOffice to convert the file, some classes are
|
|
|
|
* added to the <title> tag. This is a quick & dirty way of matching the
|
|
|
|
* title and comment it out independently on the classes that are set on it.
|
|
|
|
*/
|
|
|
|
text = text.replace("<title", "<!-- <title");
|
2019-01-31 09:55:36 +01:00
|
|
|
text = text.replace("</title>","</title>-->");
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// node on windows has a delay on releasing of the file lock.
|
|
|
|
// We add a 100ms delay to work around this
|
|
|
|
if (os.type().indexOf("Windows") > -1){
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:27:22 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// change text of the pad and broadcast the changeset
|
2019-02-08 15:46:05 +01:00
|
|
|
if (!req.directDatabaseAccess) {
|
2019-01-31 09:55:36 +01:00
|
|
|
if (importHandledByPlugin || useConvertor || fileIsHTML) {
|
|
|
|
try {
|
|
|
|
importHtml.setPadHTML(pad, text);
|
|
|
|
} catch (e) {
|
|
|
|
apiLogger.warn("Error importing, possibly caused by malformed HTML");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pad.setText(text);
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:27:22 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// Load the Pad into memory then broadcast updates to all clients
|
|
|
|
padManager.unloadPad(padId);
|
|
|
|
pad = await padManager.getPad(padId);
|
|
|
|
padManager.unloadPad(padId);
|
2014-12-29 20:57:58 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
// direct Database Access means a pad user should perform a switchToPad
|
|
|
|
// and not attempt to receive updated pad data
|
2019-02-08 15:46:05 +01:00
|
|
|
if (req.directDatabaseAccess) {
|
|
|
|
return;
|
2019-01-31 09:55:36 +01:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
// tell clients to update
|
|
|
|
await padMessageHandler.updatePadClients(pad);
|
2018-10-31 23:28:52 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
// clean up temporary files
|
2018-10-31 23:28:52 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
/*
|
|
|
|
* TODO: directly delete the file and handle the eventual error. Checking
|
|
|
|
* before for existence is prone to race conditions, and does not handle any
|
|
|
|
* errors anyway.
|
|
|
|
*/
|
|
|
|
if (await fsp_exists(srcFile)) {
|
|
|
|
fsp_unlink(srcFile);
|
2019-01-31 09:55:36 +01:00
|
|
|
}
|
2019-02-19 00:48:50 +01:00
|
|
|
|
2019-02-08 15:46:05 +01:00
|
|
|
if (await fsp_exists(destFile)) {
|
|
|
|
fsp_unlink(destFile);
|
|
|
|
}
|
2019-01-31 09:55:36 +01:00
|
|
|
}
|
2019-02-19 00:48:50 +01:00
|
|
|
|
2019-01-31 09:55:36 +01:00
|
|
|
exports.doImport = function (req, res, padId)
|
|
|
|
{
|
2019-02-08 15:46:05 +01:00
|
|
|
/**
|
|
|
|
* NB: abuse the 'req' object by storing an additional
|
|
|
|
* 'directDatabaseAccess' property on it so that it can
|
|
|
|
* be passed back in the HTML below.
|
|
|
|
*
|
|
|
|
* this is necessary because in the 'throw' paths of
|
|
|
|
* the function above there's no other way to return
|
|
|
|
* a value to the caller.
|
|
|
|
*/
|
2019-01-31 09:55:36 +01:00
|
|
|
let status = "ok";
|
2019-02-08 15:46:05 +01:00
|
|
|
doImport(req, res, padId).catch(err => {
|
2019-02-08 23:20:57 +01:00
|
|
|
// check for known errors and replace the status
|
|
|
|
if (err == "uploadFailed" || err == "convertFailed" || err == "padHasData") {
|
2012-02-27 00:22:53 +01:00
|
|
|
status = err;
|
2019-01-31 09:55:36 +01:00
|
|
|
} else {
|
|
|
|
throw err;
|
2011-10-19 21:46:28 +02:00
|
|
|
}
|
2019-02-08 15:46:05 +01:00
|
|
|
}).then(() => {
|
|
|
|
// close the connection
|
|
|
|
res.send(
|
2019-01-31 09:55:36 +01:00
|
|
|
"<head> \
|
|
|
|
<script type='text/javascript' src='../../static/js/jquery.js'></script> \
|
|
|
|
</head> \
|
|
|
|
<script> \
|
|
|
|
$(window).load(function(){ \
|
2019-02-08 15:46:05 +01:00
|
|
|
var impexp = window.parent.padimpexp.handleFrameCall('" + req.directDatabaseAccess +"', '" + status + "'); \
|
2019-01-31 09:55:36 +01:00
|
|
|
}) \
|
|
|
|
</script>"
|
2019-02-08 15:46:05 +01:00
|
|
|
);
|
|
|
|
});
|
2011-07-21 21:13:58 +02:00
|
|
|
}
|