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
run unused image cleanup periodically (once every 4 hours)
azivner26b930226730
diff --git a/src/routes/api/cleanup.js b/src/routes/api/cleanup.jsindex 5726f8b3cc..16932ef0c0 100644--- a/src/routes/api/cleanup.js+++ b/src/routes/api/cleanup.js@@ -5,18 +5,17 @@ const log = require('../../services/log');const repository = require('../../services/repository');async function cleanupUnusedImages() {- const unusedImageIds = await sql.getColumn(`- SELECT images.imageId+ const unusedImages = await repository.getEntities(`+ SELECT images.*FROM imagesLEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0WHEREimages.isDeleted = 0AND note_images.noteImageId IS NULL`);- for (const imageId of unusedImageIds) {- log.info(`Deleting unused image: ${imageId}`);+ for (const image of unusedImages) {+ log.info(`Deleting unused image: ${image.imageId}`);- const image = await repository.getImage(imageId);image.isDeleted = true;image.data = null;await image.save();@@ -29,6 +28,12 @@ async function vacuumDatabase() {log.info("Database has been vacuumed.");}+// Running this periodically is a bit dangerous because it is possible during the normal usage+// that user removed image from its only note, but keeps its URL in clipboard and pastes it into+// a different note. If this cleanup happens during this moment, we delete the image before new note_images+// reference is created. But currently we don't have a better way to do this.+setInterval(cleanupUnusedImages, 4 * 3600 * 1000);+module.exports = {cleanupUnusedImages,vacuumDatabase