From b711ff6acf976a3f8808325411cf244d55c5a0e7 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 5 Oct 2020 22:22:44 -0400 Subject: [PATCH] import: Ajaxify pad import This eliminates an inline script (good for Content Security Policy) and improves the user experience. --- src/node/handler/ImportHandler.js | 21 +++-- src/static/js/pad_impexp.js | 77 +++++++------------ .../backend/specs/api/importexportGetPost.js | 45 +++++++++-- 3 files changed, 76 insertions(+), 67 deletions(-) diff --git a/src/node/handler/ImportHandler.js b/src/node/handler/ImportHandler.js index 5732beada..67bef01ce 100644 --- a/src/node/handler/ImportHandler.js +++ b/src/node/handler/ImportHandler.js @@ -258,21 +258,18 @@ const doImport = async (req, res, padId) => { }; exports.doImport = async (req, res, padId) => { - let status = 'ok'; + let httpStatus = 200; + let code = 0; + let message = 'ok'; let directDatabaseAccess; try { directDatabaseAccess = await doImport(req, res, padId); } catch (err) { - if (!(err instanceof ImportError) || !err.status) throw err; - status = err.status; + 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'; } - // close the connection - res.send([ - '', - ].join('\n')); + res.status(httpStatus).json({code, message, data: {directDatabaseAccess}}); }; diff --git a/src/static/js/pad_impexp.js b/src/static/js/pad_impexp.js index 07848d141..60a51354f 100644 --- a/src/static/js/pad_impexp.js +++ b/src/static/js/pad_impexp.js @@ -23,9 +23,9 @@ */ const padimpexp = (() => { - // /// import - let currentImportTimer = null; + let pad; + // /// import const addImportFrames = () => { $('#import .importframe').remove(); const iframe = $('