From fea8f157a7d41b88447a574da1aa404e01883e83 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Fri, 25 Oct 2024 17:45:12 +1300 Subject: [PATCH] Change script clean to use Homebrew API update --- server/admin.api.js | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/server/admin.api.js b/server/admin.api.js index 4a1569950..e0ff2131c 100644 --- a/server/admin.api.js +++ b/server/admin.api.js @@ -7,6 +7,7 @@ const zlib = require('zlib'); const HomebrewAPI = require('./homebrew.api.js'); const asyncHandler = require('express-async-handler'); +const { splitTextStyleAndMetadata } = require('../shared/helpers.js'); process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin'; process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3'; @@ -100,35 +101,25 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{ }); /* Cleans `` 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}`); function cleanText(text){return text.replaceAll(/(<\/?s)cript/gi, '');}; - HomebrewModel.findOne({ shareId: req.params.id }) - .then((brew)=>{ - if(!brew) - return res.status(404).send('Brew not found'); + const brew = req.brew; - if(!brew.text && brew.textBin) { - brew.text = zlib.inflateRawSync(brew.textBin); - } + const properties = ['text', 'description', 'title']; + 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']; - properties.forEach((property)=>{ - brew[property] = cleanText(brew[property]); - }); + splitTextStyleAndMetadata(brew); - brew.textBin = zlib.deflateRawSync(brew.text); - brew.text = undefined; + req.body = brew; - return brew.save(); - }) - .then((obj)=>res.status(200).send(obj)) - .catch((err)=>{ - console.error(err); - res.status(500).send('Error while saving'); - }); + return await HomebrewAPI.updateBrew(req, res); }); /* Compresses the "text" field of a brew to binary */