mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
run unused image cleanup periodically (once every 4 hours)
This commit is contained in:
parent
de14e808c7
commit
26b9302267
@ -5,18 +5,17 @@ const log = require('../../services/log');
|
|||||||
const repository = require('../../services/repository');
|
const repository = require('../../services/repository');
|
||||||
|
|
||||||
async function cleanupUnusedImages() {
|
async function cleanupUnusedImages() {
|
||||||
const unusedImageIds = await sql.getColumn(`
|
const unusedImages = await repository.getEntities(`
|
||||||
SELECT images.imageId
|
SELECT images.*
|
||||||
FROM images
|
FROM images
|
||||||
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0
|
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0
|
||||||
WHERE
|
WHERE
|
||||||
images.isDeleted = 0
|
images.isDeleted = 0
|
||||||
AND note_images.noteImageId IS NULL`);
|
AND note_images.noteImageId IS NULL`);
|
||||||
|
|
||||||
for (const imageId of unusedImageIds) {
|
for (const image of unusedImages) {
|
||||||
log.info(`Deleting unused image: ${imageId}`);
|
log.info(`Deleting unused image: ${image.imageId}`);
|
||||||
|
|
||||||
const image = await repository.getImage(imageId);
|
|
||||||
image.isDeleted = true;
|
image.isDeleted = true;
|
||||||
image.data = null;
|
image.data = null;
|
||||||
await image.save();
|
await image.save();
|
||||||
@ -29,6 +28,12 @@ async function vacuumDatabase() {
|
|||||||
log.info("Database has been vacuumed.");
|
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 = {
|
module.exports = {
|
||||||
cleanupUnusedImages,
|
cleanupUnusedImages,
|
||||||
vacuumDatabase
|
vacuumDatabase
|
||||||
|
Loading…
x
Reference in New Issue
Block a user