optional cleanup of unused images

This commit is contained in:
azivner
2018-01-07 14:07:59 -05:00
parent bde9e825c8
commit 31b4186e17
6 changed files with 63 additions and 5 deletions

View File

@@ -22,6 +22,10 @@ router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, r
await sql.execute("DELETE FROM notes_tree WHERE is_deleted = 1");
await sql.execute("DELETE FROM notes_image WHERE is_deleted = 1");
await sql.execute("DELETE FROM images WHERE is_deleted = 1");
await sql.execute("DELETE FROM notes WHERE is_deleted = 1");
await sql.execute("DELETE FROM recent_notes");
@@ -37,6 +41,33 @@ router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, r
res.send({});
}));
router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, next) => {
const sourceId = req.headers.source_id;
await sql.doInTransaction(async () => {
const unusedImageIds = await sql.getFirstColumn(`
SELECT images.image_id
FROM images
LEFT JOIN notes_image ON notes_image.image_id = images.image_id AND notes_image.is_deleted = 0
WHERE
images.is_deleted = 0
AND notes_image.note_image_id IS NULL`);
const now = utils.nowDate();
for (const imageId of unusedImageIds) {
log.info(`Deleting unused image: ${imageId}`);
await sql.execute("UPDATE images SET is_deleted = 1, data = null, date_modified = ? WHERE image_id = ?",
[now, imageId]);
await sync_table.addImageSync(imageId, sourceId);
}
});
res.send({});
}));
router.post('/vacuum-database', auth.checkApiAuth, wrap(async (req, res, next) => {
await sql.execute("VACUUM");