mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-24 18:32:41 +00:00
Admin script to compress old brews
Also fully delete the text field after compressed version is saved instead of leaving just an empty field.
This commit is contained in:
@@ -3,6 +3,7 @@ const router = require('express').Router();
|
||||
const Moment = require('moment');
|
||||
const render = require('vitreum/steps/render');
|
||||
const templateFn = require('../client/template.js');
|
||||
const zlib = require('zlib');
|
||||
|
||||
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
||||
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
|
||||
@@ -26,7 +27,7 @@ const mw = {
|
||||
};
|
||||
|
||||
|
||||
/* Removes all empty brews that are older than 3 days and that are shorter than a tweet */
|
||||
/* Search for brews that are older than 3 days and that are shorter than a tweet */
|
||||
const junkBrewQuery = HomebrewModel.find({
|
||||
'$where' : 'this.text.length < 140',
|
||||
createdAt : {
|
||||
@@ -34,6 +35,11 @@ const junkBrewQuery = HomebrewModel.find({
|
||||
}
|
||||
}).limit(100).maxTime(60000);
|
||||
|
||||
/* Search for brews that aren't compressed (missing the compressed text field) */
|
||||
const uncompressedBrewQuery = HomebrewModel.find({
|
||||
'textBin' : null
|
||||
}).limit(50).distinct('_id');
|
||||
|
||||
router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{
|
||||
junkBrewQuery.exec((err, objs)=>{
|
||||
if(err) return res.status(500).send(err);
|
||||
@@ -58,6 +64,32 @@ router.get('/admin/lookup/:id', mw.adminOnly, (req, res, next)=>{
|
||||
});
|
||||
});
|
||||
|
||||
/* Find 50 brews that aren't compressed yet */
|
||||
router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
|
||||
uncompressedBrewQuery.exec((err, objs)=>{
|
||||
if(err) return res.status(500).send(err);
|
||||
return res.json({ count: objs.length, ids: objs});
|
||||
});
|
||||
});
|
||||
|
||||
/* Compresses the "text" field of a brew to binary */
|
||||
router.put('/admin/compress/:id', (req, res)=>{
|
||||
HomebrewModel.get({ _id: req.params.id })
|
||||
.then((brew)=>{
|
||||
brew.textBin = zlib.deflateRawSync(brew.text); // Compress brew text to binary before saving
|
||||
brew.text = undefined; // Delete the non-binary text field since it's not needed anymore
|
||||
|
||||
brew.save((err, obj)=>{
|
||||
if(err) throw err;
|
||||
return res.status(200).send(obj);
|
||||
});
|
||||
})
|
||||
.catch((err)=>{
|
||||
console.log(err);
|
||||
return res.status(500).send('Error while saving');
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/admin/stats', mw.adminOnly, (req, res)=>{
|
||||
HomebrewModel.count({}, (err, count)=>{
|
||||
return res.json({
|
||||
|
||||
@@ -38,7 +38,7 @@ router.post('/api', (req, res)=>{
|
||||
}
|
||||
|
||||
newHomebrew.textBin = zlib.deflateRawSync(newHomebrew.text); // Compress brew text to binary before saving
|
||||
newHomebrew.text = ''; // Clear out the non-binary text field so its not saved twice
|
||||
newHomebrew.text = undefined; // Delete the non-binary text field since it's not needed anymore
|
||||
|
||||
newHomebrew.save((err, obj)=>{
|
||||
if(err){
|
||||
@@ -54,7 +54,7 @@ router.put('/api/update/:id', (req, res)=>{
|
||||
.then((brew)=>{
|
||||
brew = _.merge(brew, req.body);
|
||||
brew.textBin = zlib.deflateRawSync(req.body.text); // Compress brew text to binary before saving
|
||||
brew.text = ''; // Clear out the non-binary text field so its not saved twice
|
||||
brew.text = undefined; // Clear out the non-binary text field so its not saved twice
|
||||
brew.updatedAt = new Date();
|
||||
|
||||
if(req.account) brew.authors = _.uniq(_.concat(brew.authors, req.account.username));
|
||||
|
||||
@@ -53,7 +53,7 @@ HomebrewSchema.statics.get = function(query){
|
||||
return new Promise((resolve, reject)=>{
|
||||
Homebrew.find(query, (err, brews)=>{
|
||||
if(err || !brews.length) return reject('Can not find brew');
|
||||
if(!_.isUndefined(brews[0].textBin)) { // Uncompress zipped text field
|
||||
if(!_.isNil(brews[0].textBin)) { // Uncompress zipped text field
|
||||
unzipped = zlib.inflateRawSync(brews[0].textBin);
|
||||
brews[0].text = unzipped.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user