0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-09 13:42:38 +00:00

Shift functionality to new file in attempt to reduce code duplication in server.js

This commit is contained in:
G.Ambatte
2021-01-17 22:07:26 +13:00
parent 715ddf2b8c
commit 7fccb7e03e
3 changed files with 26 additions and 29 deletions

View File

@@ -62,7 +62,7 @@ const SharePage = createClass({
<Nav.item href={`/source/${this.processShareId()}`} color='teal' icon='fa-code'> <Nav.item href={`/source/${this.processShareId()}`} color='teal' icon='fa-code'>
view source view source
</Nav.item> </Nav.item>
<Nav.item href={`/source_dl/${this.processShareId()}`} color='teal' icon='fa-download'> <Nav.item href={`/source_dl/${this.processShareId()}`} color='red' icon='fa-download'>
download source download source
</Nav.item> </Nav.item>
<RecentNavItem brew={this.props.brew} storageKey='view' /> <RecentNavItem brew={this.props.brew} storageKey='view' />

View File

@@ -0,0 +1,14 @@
const sourceDL = function(res, brew) {
const fileName = brew.title.replaceAll(' ', '-').replaceAll(':', '').replaceAll('/', '');
res.status(200);
res.set({
'Cache-Control' : 'no-cache',
'Content-Type' : 'text/plain',
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
});
return res;
};
module.exports = sourceDL;

View File

@@ -97,22 +97,14 @@ app.get('/source/:id', (req, res)=>{
//Source download page //Source download page
app.get('/source_dl/:id', (req, res)=>{ app.get('/source_dl/:id', (req, res)=>{
const sourceDL = require ('./client/homebrew/pages/sharePage/source_dl.jsx');
if(req.params.id.length > 12) { if(req.params.id.length > 12) {
const googleId = req.params.id.slice(0, -12); const googleId = req.params.id.slice(0, -12);
const shareId = req.params.id.slice(-12); const shareId = req.params.id.slice(-12);
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share') GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
.then((brew)=>{ .then((brew)=>{
const text = brew.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;'); res = sourceDL(res, brew);
const fileName = brew.title.replaceAll(' ', '-'); return res.send(brew.text);
res.status(200);
res.set({
'Cache-Control' : 'no-cache',
'Content-Type' : 'text/plain',
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
});
return res.send(text);
}) })
.catch((err)=>{ .catch((err)=>{
console.log(err); console.log(err);
@@ -120,23 +112,14 @@ app.get('/source_dl/:id', (req, res)=>{
}); });
} else { } else {
HomebrewModel.get({ shareId: req.params.id }) HomebrewModel.get({ shareId: req.params.id })
.then((brew)=>{ .then((brew)=>{
const text = brew.text.replaceAll('<', '&lt;').replaceAll('>', '&gt;'); res = sourceDL(res, brew);
const fileName = brew.title.replaceAll(' ', '-'); return res.send(brew.text);
})
res.status(200); .catch((err)=>{
res.set({ console.log(err);
'Cache-Control' : 'no-cache', return res.status(404).send('Could not find Homebrew with that id');
'Content-Type' : 'text/plain', });
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
});
return res.send(text);
})
.catch((err)=>{
console.log(err);
return res.status(404).send('Could not find Homebrew with that id');
});
} }
}); });