Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
converted image and maintainance routes
azivneraa57a64c61a4
diff --git a/src/routes/api/anonymization.js b/src/routes/api/anonymization.jsindex 930d8f0fe9..64321ebd2b 100644--- a/src/routes/api/anonymization.js+++ b/src/routes/api/anonymization.js@@ -1,15 +1,11 @@"use strict";-const express = require('express');-const router = express.Router();const anonymization = require('../../services/anonymization');-const auth = require('../../services/auth');-const wrap = require('express-promise-wrap').wrap;-router.post('/anonymize', auth.checkApiAuth, wrap(async (req, res, next) => {+async function anonymize() {await anonymization.anonymize();+}- res.send({});-}));--module.exports = router;\ No newline at end of file+module.exports = {+ anonymize+};\ No newline at end of filediff --git a/src/routes/api/cleanup.js b/src/routes/api/cleanup.jsindex b3afa8c37c..609de022ad 100644--- a/src/routes/api/cleanup.js+++ b/src/routes/api/cleanup.js@@ -1,83 +1,73 @@"use strict";-const express = require('express');-const router = express.Router();const sql = require('../../services/sql');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');-const auth = require('../../services/auth');const log = require('../../services/log');-const wrap = require('express-promise-wrap').wrap;-router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => {- await sql.doInTransaction(async () => {- const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");- const noteIdsSql = noteIdsToDelete- .map(noteId => "'" + utils.sanitizeSql(noteId) + "'")- .join(', ');+async function cleanupSoftDeletedItems() {+ const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");+ const noteIdsSql = noteIdsToDelete+ .map(noteId => "'" + utils.sanitizeSql(noteId) + "'")+ .join(', ');- await sql.execute(`DELETE FROM event_log WHERE noteId IN (${noteIdsSql})`);+ await sql.execute(`DELETE FROM event_log WHERE noteId IN (${noteIdsSql})`);- await sql.execute(`DELETE FROM note_revisions WHERE noteId IN (${noteIdsSql})`);+ await sql.execute(`DELETE FROM note_revisions WHERE noteId IN (${noteIdsSql})`);- await sql.execute(`DELETE FROM note_images WHERE noteId IN (${noteIdsSql})`);+ await sql.execute(`DELETE FROM note_images WHERE noteId IN (${noteIdsSql})`);- await sql.execute(`DELETE FROM labels WHERE noteId IN (${noteIdsSql})`);+ await sql.execute(`DELETE FROM labels WHERE noteId IN (${noteIdsSql})`);- await sql.execute("DELETE FROM branches WHERE isDeleted = 1");+ await sql.execute("DELETE FROM branches WHERE isDeleted = 1");- await sql.execute("DELETE FROM note_images WHERE isDeleted = 1");+ await sql.execute("DELETE FROM note_images WHERE isDeleted = 1");- await sql.execute("DELETE FROM images WHERE isDeleted = 1");+ await sql.execute("DELETE FROM images WHERE isDeleted = 1");- await sql.execute("DELETE FROM notes WHERE isDeleted = 1");+ await sql.execute("DELETE FROM notes WHERE isDeleted = 1");- await sql.execute("DELETE FROM recent_notes");+ await sql.execute("DELETE FROM recent_notes");- await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId");- await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");- await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");- await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");+ await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId");+ await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");+ await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");+ await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");- log.info("Following notes has been completely cleaned from database: " + noteIdsSql);- });+ log.info("Following notes has been completely cleaned from database: " + noteIdsSql);+}- res.send({});-}));--router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, next) => {+async function cleanupUnusedImages() {const sourceId = req.headers.source_id;- await sql.doInTransaction(async () => {- const unusedImageIds = await sql.getColumn(`- SELECT images.imageId- FROM images- LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0- WHERE- images.isDeleted = 0- AND note_images.noteImageId IS NULL`);-- const now = utils.nowDate();+ const unusedImageIds = await sql.getColumn(`+ SELECT images.imageId+ FROM images+ LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0+ WHERE+ images.isDeleted = 0+ AND note_images.noteImageId IS NULL`);- for (const imageId of unusedImageIds) {- log.info(`Deleting unused image: ${imageId}`);+ const now = utils.nowDate();- await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",- [now, imageId]);+ for (const imageId of unusedImageIds) {+ log.info(`Deleting unused image: ${imageId}`);- await sync_table.addImageSync(imageId, sourceId);- }- });+ await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",+ [now, imageId]);- res.send({});-}));+ await sync_table.addImageSync(imageId, sourceId);+ }+}-router.post('/vacuum-database', auth.checkApiAuth, wrap(async (req, res, next) => {+async function vacuumDatabase() {await sql.execute("VACUUM");log.info("Database has been vacuumed.");+}- res.send({});-}));--module.exports = router;\ No newline at end of file+module.exports = {+ cleanupSoftDeletedItems,+ cleanupUnusedImages,+ vacuumDatabase+};\ No newline at end of filediff --git a/src/routes/api/image.js b/src/routes/api/image.jsindex be9a5dc0a4..d8ce83bfbe 100644--- a/src/routes/api/image.js+++ b/src/routes/api/image.js@@ -10,7 +10,7 @@ const wrap = require('express-promise-wrap').wrap;const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;const fs = require('fs');-router.get('/:imageId/:filename', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => {+async function returnImage(req, res) {const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);if (!image) {@@ -24,9 +24,9 @@ router.get('/:imageId/:filename', auth.checkApiAuthOrElectron, wrap(async (req,res.set('Content-Type', 'image/' + image.format);res.send(image.data);-}));+}-router.post('', auth.checkApiAuthOrElectron, multer.single('upload'), wrap(async (req, res, next) => {+async function uploadImage(req, res) {const sourceId = req.headers.source_id;const noteId = req.query.noteId;const file = req.file;@@ -47,6 +47,9 @@ router.post('', auth.checkApiAuthOrElectron, multer.single('upload'), wrap(asyncuploaded: true,url: `/api/images/${imageId}/${fileName}`});-}));+}-module.exports = router;\ No newline at end of file+module.exports = {+ returnImage,+ uploadImage+};\ No newline at end of filediff --git a/src/routes/api/setup.js b/src/routes/api/setup.jsindex 7fbd539087..d9bb665102 100644--- a/src/routes/api/setup.js+++ b/src/routes/api/setup.js@@ -1,33 +1,27 @@"use strict";-const express = require('express');-const router = express.Router();-const auth = require('../../services/auth');const options = require('../../services/options');const sql = require('../../services/sql');const utils = require('../../services/utils');const my_scrypt = require('../../services/my_scrypt');const password_encryption = require('../../services/password_encryption');-const wrap = require('express-promise-wrap').wrap;-router.post('', auth.checkAppNotInitialized, wrap(async (req, res, next) => {+async function setup(req) {const { username, password } = req.body;- await sql.doInTransaction(async () => {- await options.setOption('username', username);+ await options.setOption('username', username);- await options.setOption('password_verification_salt', utils.randomSecureToken(32));- await options.setOption('password_derived_key_salt', utils.randomSecureToken(32));+ await options.setOption('password_verification_salt', utils.randomSecureToken(32));+ await options.setOption('password_derived_key_salt', utils.randomSecureToken(32));- const passwordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(password));- await options.setOption('password_verification_hash', passwordVerificationKey);+ const passwordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(password));+ await options.setOption('password_verification_hash', passwordVerificationKey);- await password_encryption.setDataKey(password, utils.randomSecureToken(16));- });+ await password_encryption.setDataKey(password, utils.randomSecureToken(16));sql.setDbReadyAsResolved();+}- res.send({});-}));--module.exports = router;\ No newline at end of file+module.exports = {+ setup+};\ No newline at end of filediff --git a/src/routes/api/sql.js b/src/routes/api/sql.jsindex 878f1a39bc..86195cb31c 100644--- a/src/routes/api/sql.js+++ b/src/routes/api/sql.js@@ -1,26 +1,24 @@"use strict";-const express = require('express');-const router = express.Router();-const auth = require('../../services/auth');const sql = require('../../services/sql');-const wrap = require('express-promise-wrap').wrap;-router.post('/execute', auth.checkApiAuth, wrap(async (req, res, next) => {+async function execute(req) {const query = req.body.query;try {- res.send({+ return {success: true,rows: await sql.getRows(query)- });+ };}catch (e) {- res.send({+ return {success: false,error: e.message- });+ };}-}));+}-module.exports = router;\ No newline at end of file+module.exports = {+ execute+};\ No newline at end of filediff --git a/src/routes/routes.js b/src/routes/routes.jsindex e6db21329a..67280e2483 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -40,44 +40,35 @@ const auth = require('../services/auth');const cls = require('../services/cls');const sql = require('../services/sql');-function apiRoute(method, path, routeHandler) {- route({- method,- path,- middleware: [auth.checkApiAuth],- routeHandler,- resultHandler: (res, result) => {- // if it's an array and first element is integer then we consider this to be [statusCode, response] format- if (Array.isArray(result) && result.length > 0 && Number.isInteger(result[0])) {- const [statusCode, response] = result;-- res.status(statusCode).send(response);-- if (statusCode !== 200) {- log.info(`${method} ${path} returned ${statusCode} with response ${JSON.stringify(response)}`);- }- }- else if (result === undefined) {- res.status(200).send();- }- else {- res.status(200).send(result);- }+function apiResultHandler(res, result) {+ // if it's an array and first element is integer then we consider this to be [statusCode, response] format+ if (Array.isArray(result) && result.length > 0 && Number.isInteger(result[0])) {+ const [statusCode, response] = result;++ res.status(statusCode).send(response);++ if (statusCode !== 200) {+ log.info(`${method} ${path} returned ${statusCode} with response ${JSON.stringify(response)}`);}- });+ }+ else if (result === undefined) {+ res.status(200).send();+ }+ else {+ res.status(200).send(result);+ }+}++function apiRoute(method, path, routeHandler) {+ route(method, path, [auth.checkApiAuth], routeHandler, apiResultHandler);}// API routes requiring HTTP protocol. This means we ignore route return value and make an electron auth exceptionfunction httpApiRoute(method, path, routeHandler) {- route({- method,- path,- middleware: [auth.checkApiAuth, multer.single('upload')],- routeHandler- })+ route(method, path, [auth.checkApiAuth, multer.single('upload')], routeHandler);}-function route({ method, path, middleware, routeHandler, resultHandler }) {+function route(method, path, middleware, routeHandler, resultHandler) {router[method](path, ...middleware, async (req, res, next) => {try {const result = await cls.init(async () => {@@ -176,14 +167,19 @@ function register(app) {apiRoute(GET, '/api/app-info', appInfoRoute.getAppInfo);httpApiRoute(GET, '/api/export/:noteId', exportRoute.exportNote);-httpApiRoute(POST, '/api/import/:parentNoteId', importRoute.importTar);- app.use('/api/setup', setupApiRoute);- app.use('/api/sql', sqlRoute);- app.use('/api/anonymization', anonymizationRoute);- app.use('/api/cleanup', cleanupRoute);- app.use('/api/images', imageRoute);+ route(POST, '/api/setup', [auth.checkAppNotInitialized], setupApiRoute.setup, apiResultHandler);++ apiRoute(POST, '/api/sql/execute', sqlRoute.execute);+ apiRoute(POST, '/api/anonymization/anonymize', anonymizationRoute.anonymize);++ apiRoute(POST, '/api/cleanup/cleanup-soft-deleted-items', cleanupRoute.cleanupSoftDeletedItems);+ apiRoute(POST, '/api/cleanup/cleanup-unused-images', cleanupRoute.cleanupUnusedImages);+ apiRoute(POST, '/api/cleanup/vacuum-database', cleanupRoute.vacuumDatabase);++ httpApiRoute(GET, '/api/images/:imageId/:filename', imageRoute.returnImage);+ httpApiRoute(POST, '/api/images', imageRoute.uploadImage);app.use('/api/script', scriptRoute);app.use('/api/sender', senderRoute);app.use('/api/files', filesRoute);