0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 18:32:41 +00:00

Added sanitizeHtml function to Homebrew model to generate HTML from the brew for the source page.

Moved `source` page generation function to a new function module. Added option to function to create plain text download with a sanitized filename and made it accessible from a new page: `download`.
Added the `download` item to the BrewItem so it appears on each brew on the User page.
Added `sanitize-filename` dependency to `package.json`.
This commit is contained in:
G.Ambatte
2021-01-21 00:02:15 +13:00
parent 7fccb7e03e
commit e8135fcbb4
7 changed files with 79 additions and 67 deletions

View File

@@ -7,6 +7,8 @@ const app = express();
const homebrewApi = require('./server/homebrew.api.js');
const GoogleActions = require('./server/googleActions.js');
const shareFunction = require ('./client/homebrew/pages/sharePage/sourceFunctions.jsx');
// Serve brotli-compressed static files if available
app.use('/', expressStaticGzip(`${__dirname}/build`, {
enableBrotli : true,
@@ -70,57 +72,12 @@ app.get('/robots.txt', (req, res)=>{
//Source page
app.get('/source/:id', (req, res)=>{
if(req.params.id.length > 12) {
const googleId = req.params.id.slice(0, -12);
const shareId = req.params.id.slice(-12);
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
.then((brew)=>{
const text = brew.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
return res.status(200).send(`<code><pre style="white-space: pre-wrap;">${text}</pre></code>`);
})
.catch((err)=>{
console.log(err);
return res.status(400).send('Can\'t get brew from Google');
});
} else {
HomebrewModel.get({ shareId: req.params.id })
.then((brew)=>{
const text = brew.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
return res.status(200).send(`<code><pre style="white-space: pre-wrap;">${text}</pre></code>`);
})
.catch((err)=>{
console.log(err);
return res.status(404).send('Could not find Homebrew with that id');
});
}
return shareFunction(req, res, 'source');
});
//Source download page
app.get('/source_dl/:id', (req, res)=>{
const sourceDL = require ('./client/homebrew/pages/sharePage/source_dl.jsx');
if(req.params.id.length > 12) {
const googleId = req.params.id.slice(0, -12);
const shareId = req.params.id.slice(-12);
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
.then((brew)=>{
res = sourceDL(res, brew);
return res.send(brew.text);
})
.catch((err)=>{
console.log(err);
return res.status(400).send('Can\'t get brew from Google');
});
} else {
HomebrewModel.get({ shareId: req.params.id })
.then((brew)=>{
res = sourceDL(res, brew);
return res.send(brew.text);
})
.catch((err)=>{
console.log(err);
return res.status(404).send('Could not find Homebrew with that id');
});
}
app.get('/download/:id', (req, res)=>{
return shareFunction(req, res, 'download');
});
//User Page