Skip to content

Build your personal knowledge base with Trilium Notes

fix custom resource handler, closes #1125

zadam74a78020883d
diff --git a/src/routes/custom.js b/src/routes/custom.js
index 7527b94b26..c8535c6046 100644
--- a/src/routes/custom.js
+++ b/src/routes/custom.js
@@ -2,67 +2,76 @@ const repository = require('../services/repository');
const log = require('../services/log');
const fileUploadService = require('./api/files.js');
const scriptService = require('../services/script');
+const cls = require('../services/cls');
-function register(router) {
- // explicitly no CSRF middleware since it's meant to allow integration from external services
+async function handleRequest(req, res) {
+ // express puts content after first slash into 0 index element
- router.all('/custom/:path*', async (req, res, next) => {
- // express puts content after first slash into 0 index element
- const path = req.params.path + req.params[0];
+ const path = req.params.path + req.params[0];
+
+ const attrs = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");
+
+ for (const attr of attrs) {
+ const regex = new RegExp(attr.value);
+ let match;
+
+ try {
+ match = path.match(regex);
+ }
+ catch (e) {
+ log.error(`Testing path for label ${attr.attributeId}, regex=${attr.value} failed with error ` + e.stack);
+ continue;
+ }
+
+ if (!match) {
+ continue;
+ }
- const attrs = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");
+ if (attr.name === 'customRequestHandler') {
+ const note = await attr.getNote();
- for (const attr of attrs) {
- const regex = new RegExp(attr.value);
- let match;
+ log.info(`Handling custom request "${path}" with note ${note.noteId}`);
try {
- match = path.match(regex);
+ await scriptService.executeNote(note, {
+ pathParams: match.slice(1),
+ req,
+ res
+ });
}
catch (e) {
- log.error(`Testing path for label ${attr.attributeId}, regex=${attr.value} failed with error ` + e.stack);
- continue;
- }
+ log.error(`Custom handler ${note.noteId} failed with ${e.message}`);
- if (!match) {
- continue;
+ res.status(500).send(e.message);
}
+ }
+ else if (attr.name === 'customResourceProvider') {
+ await fileUploadService.downloadNoteFile(attr.noteId, res);
+ }
+ else {
+ throw new Error("Unrecognized attribute name " + attr.name);
+ }
- if (attr.name === 'customRequestHandler') {
- const note = await attr.getNote();
-
- log.info(`Handling custom request "${path}" with note ${note.noteId}`);
+ return; // only first handler is executed
+ }
- try {
- await scriptService.executeNote(note, {
- pathParams: match.slice(1),
- req,
- res
- });
- }
- catch (e) {
- log.error(`Custom handler ${note.noteId} failed with ${e.message}`);
+ const message = `No handler matched for custom ${path} request.`;
- res.status(500).send(e.message);
- }
- }
- else if (attr.name === 'customResourceProvider') {
- await fileUploadService.downloadNoteFile(attr.noteId, res);
- }
- else {
- throw new Error("Unrecognized attribute name " + attr.name);
- }
+ log.info(message);
+ res.status(404).send(message);
+}
- return; // only first handler is executed
- }
+function register(router) {
+ // explicitly no CSRF middleware since it's meant to allow integration from external services
- const message = `No handler matched for custom ${path} request.`;
+ router.all('/custom/:path*', async (req, res, next) => {
+ cls.namespace.bindEmitter(req);
+ cls.namespace.bindEmitter(res);
- log.info(message);
- res.status(404).send(message);
+ cls.init(() => handleRequest(req, res));
});
}
module.exports = {
register
-};
\ No newline at end of file
+};