2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2011-07-21 20:13:58 +01:00
|
|
|
/**
|
|
|
|
* Handles the import requests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01: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 20:13:58 +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 padManager = require('../db/PadManager');
|
|
|
|
const padMessageHandler = require('./PadMessageHandler');
|
2021-02-07 18:39:36 -05:00
|
|
|
const fs = require('fs').promises;
|
2020-11-23 13:24:19 -05:00
|
|
|
const path = require('path');
|
|
|
|
const settings = require('../utils/Settings');
|
2022-01-28 21:16:05 -05:00
|
|
|
const {Formidable} = require('formidable');
|
2020-11-23 13:24:19 -05:00
|
|
|
const os = require('os');
|
|
|
|
const importHtml = require('../utils/ImportHtml');
|
|
|
|
const importEtherpad = require('../utils/ImportEtherpad');
|
|
|
|
const log4js = require('log4js');
|
2021-01-21 21:06:52 +00:00
|
|
|
const hooks = require('../../static/js/pluginfw/hooks.js');
|
2019-01-31 08:55:36 +00:00
|
|
|
|
2021-02-07 21:13:52 -05:00
|
|
|
const logger = log4js.getLogger('ImportHandler');
|
|
|
|
|
2021-02-07 19:21:53 -05:00
|
|
|
// `status` must be a string supported by `importErrorMessage()` in `src/static/js/pad_impexp.js`.
|
|
|
|
class ImportError extends Error {
|
|
|
|
constructor(status, ...args) {
|
|
|
|
super(...args);
|
|
|
|
if (Error.captureStackTrace) Error.captureStackTrace(this, ImportError);
|
|
|
|
this.name = 'ImportError';
|
|
|
|
this.status = status;
|
|
|
|
const msg = this.message == null ? '' : String(this.message);
|
|
|
|
if (status !== '') this.message = msg === '' ? status : `${status}: ${msg}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 18:35:00 -05:00
|
|
|
const rm = async (path) => {
|
|
|
|
try {
|
2021-02-07 18:39:36 -05:00
|
|
|
await fs.unlink(path);
|
2021-02-07 18:35:00 -05:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== 'ENOENT') throw err;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-17 18:53:07 -04:00
|
|
|
let converter = null;
|
2020-11-23 13:24:19 -05:00
|
|
|
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
|
2021-01-21 21:06:52 +00:00
|
|
|
if (settings.abiword != null && settings.soffice == null) {
|
2021-03-17 18:53:07 -04:00
|
|
|
converter = 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) {
|
2021-03-17 18:53:07 -04:00
|
|
|
converter = require('../utils/LibreOffice');
|
2020-11-23 13:24:19 -05:00
|
|
|
exportExtension = 'html';
|
2018-03-08 12:55:00 +01:00
|
|
|
}
|
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 20:13:58 +01:00
|
|
|
/**
|
|
|
|
* do a requested import
|
2019-02-08 23:20:57 +01:00
|
|
|
*/
|
2022-02-17 00:01:07 -05:00
|
|
|
const doImport = async (req, res, padId, authorId) => {
|
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
|
2020-11-23 13:24:19 -05:00
|
|
|
const randNum = Math.floor(Math.random() * 0xFFFFFFFF);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2021-03-17 18:53:07 -04:00
|
|
|
// setting flag for whether to use converter or not
|
|
|
|
let useConverter = (converter != null);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2022-01-28 21:16:05 -05:00
|
|
|
const form = new Formidable({
|
|
|
|
keepExtensions: true,
|
|
|
|
uploadDir: tmpDirectory,
|
|
|
|
maxFileSize: settings.importMaxFileSize,
|
|
|
|
});
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// locally wrapped Promise, since form.parse requires a callback
|
|
|
|
let srcFile = await new Promise((resolve, reject) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
form.parse(req, (err, fields, files) => {
|
2021-02-18 03:42:41 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.warn(`Import failed due to form error: ${err.stack || err}`);
|
2020-04-14 10:02:21 +00:00
|
|
|
// I hate doing indexOf here but I can't see anything to use...
|
2020-11-23 13:24:19 -05:00
|
|
|
if (err && err.stack && err.stack.indexOf('maxFileSize') !== -1) {
|
2021-02-07 19:21:53 -05:00
|
|
|
return reject(new ImportError('maxFileSize'));
|
2020-04-14 10:02:21 +00:00
|
|
|
}
|
2021-02-07 19:21:53 -05:00
|
|
|
return reject(new ImportError('uploadFailed'));
|
2018-10-31 23:00:45 +01:00
|
|
|
}
|
2021-02-18 03:42:41 -05:00
|
|
|
if (!files.file) {
|
|
|
|
logger.warn('Import failed because form had no file');
|
2021-02-07 19:21:53 -05:00
|
|
|
return reject(new ImportError('uploadFailed'));
|
2020-04-03 10:30:12 +00:00
|
|
|
}
|
2022-01-28 21:16:05 -05:00
|
|
|
resolve(files.file.filepath);
|
2019-01-31 08:55:36 +00:00
|
|
|
});
|
|
|
|
});
|
2018-10-31 23:00:45 +01:00
|
|
|
|
2019-01-31 08:55:36 +00: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
|
2020-11-23 13:24:19 -05:00
|
|
|
const fileEnding = path.extname(srcFile).toLowerCase();
|
2021-01-21 21:06:52 +00:00
|
|
|
const knownFileEndings =
|
|
|
|
['.txt', '.doc', '.docx', '.pdf', '.odt', '.html', '.htm', '.etherpad', '.rtf'];
|
2020-11-23 13:24:19 -05:00
|
|
|
const fileEndingUnknown = (knownFileEndings.indexOf(fileEnding) < 0);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
if (fileEndingUnknown) {
|
|
|
|
// the file ending is not known
|
2018-10-31 23:09:27 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
if (settings.allowUnknownFileEnds === true) {
|
|
|
|
// we need to rename this file with a .txt ending
|
2020-11-23 13:24:19 -05:00
|
|
|
const oldSrcFile = srcFile;
|
2018-10-31 23:09:27 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
srcFile = path.join(path.dirname(srcFile), `${path.basename(srcFile, fileEnding)}.txt`);
|
2021-02-07 18:39:36 -05:00
|
|
|
await fs.rename(oldSrcFile, srcFile);
|
2019-01-31 08:55:36 +00:00
|
|
|
} else {
|
2021-02-18 03:42:41 -05:00
|
|
|
logger.warn(`Not allowing unknown file type to be imported: ${fileEnding}`);
|
2021-02-07 19:21:53 -05:00
|
|
|
throw new ImportError('uploadFailed');
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const destFile = path.join(tmpDirectory, `etherpad_import_${randNum}.${exportExtension}`);
|
2021-12-10 02:34:13 -05:00
|
|
|
const context = {srcFile, destFile, fileEnding, padId, ImportError};
|
|
|
|
const importHandledByPlugin = (await hooks.aCallAll('import', context)).some((x) => x);
|
2020-11-23 13:24:19 -05:00
|
|
|
const fileIsEtherpad = (fileEnding === '.etherpad');
|
|
|
|
const fileIsHTML = (fileEnding === '.html' || fileEnding === '.htm');
|
|
|
|
const fileIsTXT = (fileEnding === '.txt');
|
2018-10-31 23:15:01 +01:00
|
|
|
|
2020-10-05 17:47:50 -04:00
|
|
|
let directDatabaseAccess = false;
|
2019-01-31 08:55:36 +00:00
|
|
|
if (fileIsEtherpad) {
|
2022-02-17 16:50:21 -05:00
|
|
|
// Use '\n' to avoid the default pad text if the pad doesn't yet exist.
|
2022-02-17 00:01:07 -05:00
|
|
|
const pad = await padManager.getPad(padId, '\n', authorId);
|
2022-02-17 16:24:59 -05:00
|
|
|
const headCount = pad.head;
|
2019-01-31 08:55:36 +00:00
|
|
|
if (headCount >= 10) {
|
2021-02-07 21:13:52 -05:00
|
|
|
logger.warn('Aborting direct database import attempt of a pad that already has content');
|
2021-02-07 19:21:53 -05:00
|
|
|
throw new ImportError('padHasData');
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
2022-02-17 16:24:59 -05:00
|
|
|
const text = await fs.readFile(srcFile, 'utf8');
|
2020-10-05 17:47:50 -04:00
|
|
|
directDatabaseAccess = true;
|
2022-02-17 00:01:07 -05:00
|
|
|
await importEtherpad.setPadRaw(padId, text, authorId);
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert file to html if necessary
|
2020-10-05 17:47:50 -04:00
|
|
|
if (!importHandledByPlugin && !directDatabaseAccess) {
|
2019-01-31 08:55:36 +00:00
|
|
|
if (fileIsTXT) {
|
2021-03-17 18:53:07 -04:00
|
|
|
// Don't use converter for text files
|
|
|
|
useConverter = false;
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
2018-10-31 23:20:55 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// See https://github.com/ether/etherpad-lite/issues/2572
|
2021-03-17 18:53:07 -04:00
|
|
|
if (fileIsHTML || !useConverter) {
|
|
|
|
// if no converter only rename
|
2021-02-07 18:39:36 -05:00
|
|
|
await fs.rename(srcFile, destFile);
|
2019-01-31 08:55:36 +00:00
|
|
|
} else {
|
2021-03-18 01:01:47 -04:00
|
|
|
try {
|
|
|
|
await converter.convertFile(srcFile, destFile, exportExtension);
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`Converting Error: ${err.stack || err}`);
|
|
|
|
throw new ImportError('convertFailed');
|
|
|
|
}
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2021-03-17 18:53:07 -04:00
|
|
|
if (!useConverter && !directDatabaseAccess) {
|
2019-01-31 08:55:36 +00:00
|
|
|
// Read the file with no encoding for raw buffer access.
|
2021-02-07 18:39:36 -05:00
|
|
|
const buf = await fs.readFile(destFile);
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// Check if there are only ascii chars in the uploaded file
|
2020-11-23 13:24:19 -05:00
|
|
|
const isAscii = !Array.prototype.some.call(buf, (c) => (c > 240));
|
2018-10-31 23:22:50 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
if (!isAscii) {
|
2021-02-18 03:42:41 -05:00
|
|
|
logger.warn('Attempt to import non-ASCII file');
|
2021-02-07 19:21:53 -05:00
|
|
|
throw new ImportError('uploadFailed');
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2022-02-23 13:41:26 -05:00
|
|
|
// Use '\n' to avoid the default pad text if the pad doesn't yet exist.
|
|
|
|
let pad = await padManager.getPad(padId, '\n', authorId);
|
2018-10-31 23:24:56 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// read the text
|
|
|
|
let text;
|
2018-10-31 23:24:56 +01:00
|
|
|
|
2020-10-05 17:47:50 -04:00
|
|
|
if (!directDatabaseAccess) {
|
2021-02-07 18:39:36 -05:00
|
|
|
text = await fs.readFile(destFile, 'utf8');
|
2014-12-29 20:57:58 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// node on windows has a delay on releasing of the file lock.
|
|
|
|
// We add a 100ms delay to work around this
|
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
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:27:22 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// change text of the pad and broadcast the changeset
|
2020-10-05 17:47:50 -04:00
|
|
|
if (!directDatabaseAccess) {
|
2021-03-17 18:53:07 -04:00
|
|
|
if (importHandledByPlugin || useConverter || fileIsHTML) {
|
2019-01-31 08:55:36 +00:00
|
|
|
try {
|
2022-02-17 00:01:07 -05:00
|
|
|
await importHtml.setPadHTML(pad, text, authorId);
|
2021-02-18 03:42:41 -05:00
|
|
|
} catch (err) {
|
|
|
|
logger.warn(`Error importing, possibly caused by malformed HTML: ${err.stack || err}`);
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-17 00:01:07 -05:00
|
|
|
await pad.setText(text, authorId);
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-31 23:27:22 +01:00
|
|
|
|
2019-01-31 08:55:36 +00:00
|
|
|
// Load the Pad into memory then broadcast updates to all clients
|
|
|
|
padManager.unloadPad(padId);
|
2022-02-23 13:41:26 -05:00
|
|
|
pad = await padManager.getPad(padId, '\n', authorId);
|
2019-01-31 08:55:36 +00:00
|
|
|
padManager.unloadPad(padId);
|
2014-12-29 20:57:58 +01:00
|
|
|
|
2021-10-28 15:55:47 -04:00
|
|
|
// Direct database access means a pad user should reload the pad and not attempt to receive
|
|
|
|
// updated pad data.
|
2020-10-05 17:47:50 -04:00
|
|
|
if (directDatabaseAccess) return true;
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-02-08 14:46:05 +00:00
|
|
|
// tell clients to update
|
|
|
|
await padMessageHandler.updatePadClients(pad);
|
2018-10-31 23:28:52 +01:00
|
|
|
|
2019-02-08 14:46:05 +00:00
|
|
|
// clean up temporary files
|
2021-02-07 18:35:00 -05:00
|
|
|
rm(srcFile);
|
|
|
|
rm(destFile);
|
2020-10-05 17:47:50 -04:00
|
|
|
|
|
|
|
return false;
|
2021-01-21 21:06:52 +00:00
|
|
|
};
|
2019-02-19 00:48:50 +01:00
|
|
|
|
2022-02-17 00:01:07 -05:00
|
|
|
exports.doImport = async (req, res, padId, authorId = '') => {
|
2020-10-05 22:22:44 -04:00
|
|
|
let httpStatus = 200;
|
|
|
|
let code = 0;
|
|
|
|
let message = 'ok';
|
2020-10-05 17:47:50 -04:00
|
|
|
let directDatabaseAccess;
|
|
|
|
try {
|
2022-02-17 00:01:07 -05:00
|
|
|
directDatabaseAccess = await doImport(req, res, padId, authorId);
|
2020-10-05 17:47:50 -04:00
|
|
|
} catch (err) {
|
2020-10-05 22:22:44 -04:00
|
|
|
const known = err instanceof ImportError && err.status;
|
|
|
|
if (!known) logger.error(`Internal error during import: ${err.stack || err}`);
|
|
|
|
httpStatus = known ? 400 : 500;
|
|
|
|
code = known ? 1 : 2;
|
|
|
|
message = known ? err.status : 'internalError';
|
2020-10-05 17:47:50 -04:00
|
|
|
}
|
2020-10-05 22:22:44 -04:00
|
|
|
res.status(httpStatus).json({code, message, data: {directDatabaseAccess}});
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|