Skip to content

Commit

Permalink
fix(handlers): validate handlers before adding them
Browse files Browse the repository at this point in the history
Fixes #912

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jan 2, 2024
1 parent 35df513 commit 04884bd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/services/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export default class Viewer {
* @param {Handler} handler a new unregistered handler
*/
registerHandler(handler) {
const err = this.validateHandler(handler)
if (err) {
console.error(err, handler)
return
}

this._state.handlers.push(handler)
const handledMimes = [
...handler.mimes,
Expand All @@ -110,6 +116,28 @@ export default class Viewer {
}
}

validateHandler({ id, mimes, mimesAliases, component }) {
// checking valid handler id
if (!id || id.trim() === '' || typeof id !== 'string') {
return 'The handler doesn\'t have a valid id'
}

// checking if handler is not already registered
if (this._state.handlers.find(h => h.id === id)) {
return 'The handler is already registered'
}

// Nothing available to process! Failure
if (!(mimes && Array.isArray(mimes)) && !mimesAliases) {
return 'Handler needs a valid mime array or mimesAliases'
}

// checking valid handler component data
if ((!component || (typeof component !== 'object' && typeof component !== 'function'))) {
return 'The handler doesn\'t have a valid component'
}
}

/**
* Get the current opened file
*
Expand Down

0 comments on commit 04884bd

Please sign in to comment.