trilium/routes/api/cleanup.js

83 lines
3 KiB
JavaScript
Raw Normal View History

"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');
2017-12-23 22:57:20 +08:00
const auth = require('../../services/auth');
2017-12-24 02:16:18 +08:00
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 () => {
2018-01-29 08:30:14 +08:00
const noteIdsToDelete = await sql.getFirstColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
const noteIdsSql = noteIdsToDelete
.map(noteId => "'" + utils.sanitizeSql(noteId) + "'")
.join(', ');
2018-01-29 08:30:14 +08:00
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_images WHERE noteId IN (${noteIdsSql})`);
2018-01-29 08:30:14 +08:00
await sql.execute(`DELETE FROM attributes WHERE noteId IN (${noteIdsSql})`);
await sql.execute("DELETE FROM note_tree WHERE isDeleted = 1");
await sql.execute("DELETE FROM note_images WHERE isDeleted = 1");
2018-01-08 03:07:59 +08:00
2018-01-29 08:30:14 +08:00
await sql.execute("DELETE FROM images WHERE isDeleted = 1");
2018-01-08 03:07:59 +08:00
2018-01-29 08:30:14 +08:00
await sql.execute("DELETE FROM notes WHERE isDeleted = 1");
await sql.execute("DELETE FROM recent_notes");
2018-01-29 08:30:14 +08:00
await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId");
await sync_table.cleanupSyncRowsForMissingEntities("note_tree", "noteTreeId");
await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");
2018-01-29 08:30:14 +08:00
await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "noteTreeId");
2017-12-24 02:16:18 +08:00
log.info("Following notes has been completely cleaned from database: " + noteIdsSql);
});
res.send({});
}));
2018-01-08 03:07:59 +08:00
router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, next) => {
2018-01-29 08:30:14 +08:00
const sourceId = req.headers.sourceId;
2018-01-08 03:07:59 +08:00
await sql.doInTransaction(async () => {
const unusedImageIds = await sql.getFirstColumn(`
2018-01-29 08:30:14 +08:00
SELECT images.imageId
2018-01-08 03:07:59 +08:00
FROM images
LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0
2018-01-08 03:07:59 +08:00
WHERE
2018-01-29 08:30:14 +08:00
images.isDeleted = 0
AND note_images.noteImageId IS NULL`);
2018-01-08 03:07:59 +08:00
const now = utils.nowDate();
for (const imageId of unusedImageIds) {
log.info(`Deleting unused image: ${imageId}`);
2018-01-29 08:30:14 +08:00
await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",
2018-01-08 03:07:59 +08:00
[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");
2017-12-24 02:16:18 +08:00
log.info("Database has been vacuumed.");
res.send({});
}));
module.exports = router;