run unused image cleanup periodically (once every 4 hours)

This commit is contained in:
azivner 2018-11-01 16:40:25 +01:00
parent de14e808c7
commit 26b9302267

View File

@ -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 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}`);
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