From 4d457f629638f1b00b2840b52a97d652ddb6c9cb Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 10 Dec 2021 02:34:13 -0500 Subject: [PATCH] ImportHandler: Pass `ImportError` to `import` hook --- CHANGELOG.md | 1 + doc/api/hooks_server-side.md | 13 +++++++++++++ src/node/handler/ImportHandler.js | 4 ++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b41449a8..8d044260d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ * New APIs for processing attributes: `ep_etherpad-lite/static/js/attributes` (low-level API) and `ep_etherpad-lite/static/js/AttributeMap` (high-level API). +* The `import` server-side hook has a new `ImportError` context property. ### Compatibility changes diff --git a/doc/api/hooks_server-side.md b/doc/api/hooks_server-side.md index 34a906ca6..f7aa6b0b6 100644 --- a/doc/api/hooks_server-side.md +++ b/doc/api/hooks_server-side.md @@ -821,6 +821,19 @@ Context properties: period** (examples: `'.docx'`, `'.html'`, `'.etherpad'`). * `padId`: The identifier of the destination pad. * `srcFile`: The document to convert. +* `ImportError`: Subclass of Error that can be thrown to provide a specific + error message to the user. The constructor's first argument must be a string + matching one of the [known error + identifiers](https://github.com/ether/etherpad-lite/blob/1.8.16/src/static/js/pad_impexp.js#L80-L86). + +Example: + +```javascript +exports.import = async (hookName, {fileEnding, ImportError}) => { + // Reject all *.etherpad imports with a permission denied message. + if (fileEnding === '.etherpad') throw new ImportError('permission'); +}; +``` ## `userJoin` diff --git a/src/node/handler/ImportHandler.js b/src/node/handler/ImportHandler.js index c865dcf98..9f2a53843 100644 --- a/src/node/handler/ImportHandler.js +++ b/src/node/handler/ImportHandler.js @@ -142,8 +142,8 @@ const doImport = async (req, res, padId) => { } const destFile = path.join(tmpDirectory, `etherpad_import_${randNum}.${exportExtension}`); - const importHandledByPlugin = - (await hooks.aCallAll('import', {srcFile, destFile, fileEnding, padId})).some((x) => x); + const context = {srcFile, destFile, fileEnding, padId, ImportError}; + const importHandledByPlugin = (await hooks.aCallAll('import', context)).some((x) => x); const fileIsEtherpad = (fileEnding === '.etherpad'); const fileIsHTML = (fileEnding === '.html' || fileEnding === '.htm'); const fileIsTXT = (fileEnding === '.txt');