0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-03 12:42:41 +00:00

Change script clean to use Homebrew API update

This commit is contained in:
G.Ambatte
2024-10-25 17:45:12 +13:00
parent 898be28af3
commit fea8f157a7

View File

@@ -7,6 +7,7 @@ const zlib = require('zlib');
const HomebrewAPI = require('./homebrew.api.js'); const HomebrewAPI = require('./homebrew.api.js');
const asyncHandler = require('express-async-handler'); const asyncHandler = require('express-async-handler');
const { splitTextStyleAndMetadata } = require('../shared/helpers.js');
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin'; process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3'; process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
@@ -100,35 +101,25 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
}); });
/* Cleans `<script` and `</script>` from the "text" field of a brew */ /* Cleans `<script` and `</script>` from the "text" field of a brew */
router.put('/admin/clean/script/:id', (req, res)=>{ router.put('/admin/clean/script/:id', asyncHandler(HomebrewAPI.getBrew('admin', false)), async (req, res)=>{
console.log(`[ADMIN] Cleaning script tags from ShareID ${req.params.id}`); console.log(`[ADMIN] Cleaning script tags from ShareID ${req.params.id}`);
function cleanText(text){return text.replaceAll(/(<\/?s)cript/gi, '');}; function cleanText(text){return text.replaceAll(/(<\/?s)cript/gi, '');};
HomebrewModel.findOne({ shareId: req.params.id }) const brew = req.brew;
.then((brew)=>{
if(!brew)
return res.status(404).send('Brew not found');
if(!brew.text && brew.textBin) { const properties = ['text', 'description', 'title'];
brew.text = zlib.inflateRawSync(brew.textBin); properties.forEach((property)=>{
} brew[property] = cleanText(brew[property]);
});
// Tag cleaning is commented out as it is impossible to enter a script tag in tags
// brew.tags = cleanText(brew.tags.join('\n')).split('\n');
const properties = ['text', 'description', 'title']; splitTextStyleAndMetadata(brew);
properties.forEach((property)=>{
brew[property] = cleanText(brew[property]);
});
brew.textBin = zlib.deflateRawSync(brew.text); req.body = brew;
brew.text = undefined;
return brew.save(); return await HomebrewAPI.updateBrew(req, res);
})
.then((obj)=>res.status(200).send(obj))
.catch((err)=>{
console.error(err);
res.status(500).send('Error while saving');
});
}); });
/* Compresses the "text" field of a brew to binary */ /* Compresses the "text" field of a brew to binary */