clean up deleted q
This commit is contained in:
@@ -212,57 +212,59 @@ export default (router, tpl) => {
|
||||
posts,
|
||||
page,
|
||||
pages,
|
||||
stats: { total: posts.length },
|
||||
tmp: null
|
||||
}, req)
|
||||
});
|
||||
});
|
||||
|
||||
const deleteItem = async (id) => {
|
||||
const f0ck = await db`
|
||||
select dest, mime
|
||||
from "items"
|
||||
where
|
||||
id = ${id}
|
||||
limit 1
|
||||
`;
|
||||
|
||||
if (f0ck.length > 0) {
|
||||
console.log(`[ADMIN DENY] Found item, deleting files: ${f0ck[0].dest}`);
|
||||
// Delete files
|
||||
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(e => console.log('File error pub/b:', e.message));
|
||||
await fs.unlink(`./public/t/${id}.webp`).catch(e => console.log('File error pub/t:', e.message));
|
||||
await fs.unlink(`./deleted/b/${f0ck[0].dest}`).catch(e => console.log('File error del/b:', e.message));
|
||||
await fs.unlink(`./deleted/t/${id}.webp`).catch(e => console.log('File error del/t:', e.message));
|
||||
|
||||
if (f0ck[0].mime.startsWith('audio')) {
|
||||
await fs.unlink(`./public/ca/${id}.webp`).catch(() => { });
|
||||
await fs.unlink(`./deleted/ca/${id}.webp`).catch(() => { });
|
||||
}
|
||||
|
||||
// Delete DB entries
|
||||
console.log('[ADMIN DENY] Deleting DB entries...');
|
||||
try {
|
||||
await db`delete from "tags_assign" where item_id = ${id}`;
|
||||
await db`delete from "favorites" where item_id = ${id}`;
|
||||
await db`delete from "comments" where item_id = ${id}`.catch(() => { });
|
||||
await db`delete from "items" where id = ${id}`;
|
||||
console.log('[ADMIN DENY] Deleted successfully');
|
||||
return true;
|
||||
} catch (dbErr) {
|
||||
console.error('[ADMIN DENY DB ERROR]', dbErr);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
console.log('[ADMIN DENY] Item not found in DB');
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
router.get(/^\/admin\/deny\/?/, lib.auth, async (req, res) => {
|
||||
console.log('[ADMIN DENY] Logs initiated');
|
||||
if (req.url.qs?.id) {
|
||||
const id = +req.url.qs.id;
|
||||
console.log(`[ADMIN DENY] Denying ID: ${id}`);
|
||||
|
||||
try {
|
||||
const f0ck = await db`
|
||||
select dest, mime
|
||||
from "items"
|
||||
where
|
||||
id = ${id}
|
||||
limit 1
|
||||
`;
|
||||
|
||||
if (f0ck.length > 0) {
|
||||
console.log(`[ADMIN DENY] Found item, deleting files: ${f0ck[0].dest}`);
|
||||
// Delete files
|
||||
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(e => console.log('File error pub/b:', e.message));
|
||||
await fs.unlink(`./public/t/${id}.webp`).catch(e => console.log('File error pub/t:', e.message));
|
||||
await fs.unlink(`./deleted/b/${f0ck[0].dest}`).catch(e => console.log('File error del/b:', e.message));
|
||||
await fs.unlink(`./deleted/t/${id}.webp`).catch(e => console.log('File error del/t:', e.message));
|
||||
|
||||
if (f0ck[0].mime.startsWith('audio')) {
|
||||
await fs.unlink(`./public/ca/${id}.webp`).catch(() => { });
|
||||
await fs.unlink(`./deleted/ca/${id}.webp`).catch(() => { });
|
||||
}
|
||||
|
||||
// Delete DB entries
|
||||
console.log('[ADMIN DENY] Deleting DB entries...');
|
||||
try {
|
||||
await db`delete from "tags_assign" where item_id = ${id}`;
|
||||
await db`delete from "favorites" where item_id = ${id}`;
|
||||
await db`delete from "comments" where item_id = ${id}`.catch(() => { });
|
||||
await db`delete from "items" where id = ${id}`;
|
||||
console.log('[ADMIN DENY] Deleted successfully');
|
||||
} catch (dbErr) {
|
||||
console.error('[ADMIN DENY DB ERROR]', dbErr);
|
||||
}
|
||||
} else {
|
||||
console.log('[ADMIN DENY] Item not found in DB');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[ADMIN DENY ERROR]', err);
|
||||
}
|
||||
|
||||
await deleteItem(id);
|
||||
return res.writeHead(302, {
|
||||
"Location": `/admin/approve`
|
||||
}).end();
|
||||
@@ -272,5 +274,22 @@ export default (router, tpl) => {
|
||||
return res.writeHead(302, { "Location": "/admin/approve" }).end();
|
||||
});
|
||||
|
||||
router.post(/^\/admin\/deny-multi\/?/, lib.auth, async (req, res) => {
|
||||
try {
|
||||
const ids = req.post.ids;
|
||||
if (!Array.isArray(ids)) throw new Error('ids must be an array');
|
||||
|
||||
console.log(`[ADMIN DENY MULTI] Denying ${ids.length} items`);
|
||||
for (const id of ids) {
|
||||
await deleteItem(+id);
|
||||
}
|
||||
|
||||
return res.reply({ success: true });
|
||||
} catch (err) {
|
||||
console.error('[ADMIN DENY MULTI ERROR]', err);
|
||||
return res.reply({ success: false, msg: err.message }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
return router;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user