0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 12:22:44 +00:00

admin fixes, the rest( i think)

This commit is contained in:
Víctor Losada Hernández
2024-01-24 22:20:01 +01:00
parent 0243b5f491
commit 067a7cd507

View File

@@ -40,19 +40,25 @@ const uncompressedBrewQuery = HomebrewModel.find({
'text' : { '$exists': true } 'text' : { '$exists': true }
}).lean().limit(10000).select('_id'); }).lean().limit(10000).select('_id');
router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{
junkBrewQuery.exec((err, objs)=>{ router.get('/admin/cleanup', mw.adminOnly, (req, res) => {
if(err) return res.status(500).send(err); junkBrewQuery.exec()
return res.json({ count: objs.length }); .then((objs) => res.json({ count: objs.length }))
}); .catch((error) => {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
});
}); });
/* Removes all empty brews that are older than 3 days and that are shorter than a tweet */ /* Removes all empty brews that are older than 3 days and that are shorter than a tweet */
router.post('/admin/cleanup', mw.adminOnly, (req, res)=>{ router.post('/admin/cleanup', mw.adminOnly, (req, res) => {
junkBrewQuery.remove().exec((err, objs)=>{ junkBrewQuery.remove().exec()
if(err) return res.status(500).send(err); .then((result) => res.json({ count: result.n })) // 'n' represents the number of documents removed
return res.json({ count: objs.length }); .catch((error) => {
}); console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
});
}); });
/* Searches for matching edit or share id, also attempts to partial match */ /* Searches for matching edit or share id, also attempts to partial match */
@@ -78,32 +84,39 @@ router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next) => {
}); });
/* Find 50 brews that aren't compressed yet */ /* Find 50 brews that aren't compressed yet */
router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{ router.get('/admin/finduncompressed', mw.adminOnly, async (req, res) => {
uncompressedBrewQuery.exec((err, objs)=>{ try {
if(err) return res.status(500).send(err); const objs = await uncompressedBrewQuery.exec();
objs = objs.map((obj)=>{return obj._id;}); const ids = objs.map((obj) => obj._id);
return res.json({ count: objs.length, ids: objs }); return res.json({ count: objs.length, ids: ids });
}); } catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal Server Error' });
}
}); });
/* Compresses the "text" field of a brew to binary */ /* Compresses the "text" field of a brew to binary */
router.put('/admin/compress/:id', (req, res)=>{ router.put('/admin/compress/:id', (req, res) => {
HomebrewModel.get({ _id: req.params.id }) HomebrewModel.findOne({ _id: req.params.id })
.then((brew)=>{ .then((brew) => {
brew.textBin = zlib.deflateRawSync(brew.text); // Compress brew text to binary before saving if (!brew) {
brew.text = undefined; // Delete the non-binary text field since it's not needed anymore return res.status(404).send('Brew not found');
}
brew.save((err, obj)=>{
if(err) throw err; brew.textBin = zlib.deflateRawSync(brew.text);
return res.status(200).send(obj); brew.text = undefined;
});
}) return brew.save();
.catch((err)=>{ })
console.log(err); .then((obj) => res.status(200).send(obj))
return res.status(500).send('Error while saving'); .catch((err) => {
}); console.error(err);
res.status(500).send('Error while saving');
});
}); });
router.get('/admin/stats', mw.adminOnly, async (req, res) => { router.get('/admin/stats', mw.adminOnly, async (req, res) => {
try { try {
const totalBrewsCount = await HomebrewModel.countDocuments({}); const totalBrewsCount = await HomebrewModel.countDocuments({});