formidable migration

test-formidable
John McLear 2023-07-05 11:20:28 +01:00 committed by webzwo0i
parent 3f079e6786
commit 15d4f87f03
3 changed files with 34 additions and 32 deletions

View File

@ -89,24 +89,24 @@ const doImport = async (req, res, padId, authorId) => {
maxFileSize: settings.importMaxFileSize, maxFileSize: settings.importMaxFileSize,
}); });
// locally wrapped Promise, since form.parse requires a callback let srcFile;
let srcFile = await new Promise((resolve, reject) => { let files;
form.parse(req, (err, fields, files) => { let fields;
if (err != null) { try {
logger.warn(`Import failed due to form error: ${err.stack || err}`); [fields, files] = await form.parse(req);
// I hate doing indexOf here but I can't see anything to use... } catch (err) {
if (err && err.stack && err.stack.indexOf('maxFileSize') !== -1) { logger.warn(`Import failed due to form error: ${err.stack || err}`);
return reject(new ImportError('maxFileSize')); if (err.code === Formidable.formidableErrors.biggerThanMaxFileSize) {
} throw new ImportError('maxFileSize');
return reject(new ImportError('uploadFailed')); }
} throw new ImportError('uploadFailed');
if (!files.file) { }
logger.warn('Import failed because form had no file'); if (!files.file) {
return reject(new ImportError('uploadFailed')); logger.warn('Import failed because form had no file');
} throw new ImportError('uploadFailed');
resolve(files.file.filepath); } else {
}); srcFile = files.file[0].filepath;
}); }
// ensure this is a file ending we know, else we change the file ending to .txt // 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 // this allows us to accept source code files like .c or .java

View File

@ -8,20 +8,19 @@ const util = require('util');
exports.expressPreSession = async (hookName, {app}) => { exports.expressPreSession = async (hookName, {app}) => {
// The Etherpad client side sends information about how a disconnect happened // The Etherpad client side sends information about how a disconnect happened
app.post('/ep/pad/connection-diagnostic-info', (req, res) => { app.post('/ep/pad/connection-diagnostic-info', async (req, res) => {
new Formidable().parse(req, (err, fields, files) => { const [fields, files] = await (new Formidable({})).parse(req);
clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`); clientLogger.info(`DIAGNOSTIC-INFO: ${fields.diagnosticInfo}`);
res.end('OK'); res.end('OK');
});
}); });
const parseJserrorForm = async (req) => await new Promise((resolve, reject) => { const parseJserrorForm = async (req) => {
const form = new Formidable({ const form = new Formidable({
maxFileSize: 1, // Files are not expected. Not sure if 0 means unlimited, so 1 is used. maxFileSize: 1, // Files are not expected. Not sure if 0 means unlimited, so 1 is used.
}); });
form.on('error', (err) => reject(err)); const [fields, files] = await form.parse(req);
form.parse(req, (err, fields) => err != null ? reject(err) : resolve(fields.errorInfo)); return fields.errorInfo;
}); };
// The Etherpad client side sends information about client side javscript errors // The Etherpad client side sends information about client side javscript errors
app.post('/jserror', (req, res, next) => { app.post('/jserror', (req, res, next) => {

View File

@ -15,8 +15,7 @@
*/ */
const OpenAPIBackend = require('openapi-backend').default; const OpenAPIBackend = require('openapi-backend').default;
const formidable = require('formidable'); const IncomingForm = require('formidable').IncomingForm;
const {promisify} = require('util');
const cloneDeep = require('lodash.clonedeep'); const cloneDeep = require('lodash.clonedeep');
const createHTTPError = require('http-errors'); const createHTTPError = require('http-errors');
@ -596,9 +595,13 @@ exports.expressPreSession = async (hookName, {app}) => {
// read form data if method was POST // read form data if method was POST
let formData = {}; let formData = {};
if (c.request.method === 'post') { if (c.request.method === 'post') {
const form = new formidable.IncomingForm(); const form = new IncomingForm();
const parseForm = promisify(form.parse).bind(form); formData = (await form.parse(req))[0];
formData = await parseForm(req); for (const k of Object.keys(formData)) {
if (formData[k] instanceof Array) {
formData[k] = formData[k][0];
}
}
} }
const fields = Object.assign({}, header, params, query, formData); const fields = Object.assign({}, header, params, query, formData);