0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 22:12:39 +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

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

View File

@@ -0,0 +1,56 @@
const HomebrewModel = require.main.require('./server/homebrew.model.js').model;
const sanitizeFilename = require('sanitize-filename');
const shareFunction = function(req, res, type) {
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)=>{
if(type == 'source') {
return res.status(200).send(brew.sanitizeHtml());
} else if(type == 'download') {
let fileName = sanitizeFilename(brew.title);
fileName = fileName.replaceAll(' ', '-');
res.status(200);
res.set({
'Cache-Control' : 'no-cache',
'Content-Type' : 'text/plain',
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
});
return res.send(brew.text);
} else {
console.log('Unhandled source share type');
}
})
.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)=>{
if(type == 'source') {
return res.status(200).send(brew.sanitizeHtml());
} else if(type == 'download') {
let fileName = sanitizeFilename(brew.title);
fileName = fileName.replaceAll(' ', '-');
res.status(200);
res.set({
'Cache-Control' : 'no-cache',
'Content-Type' : 'text/plain',
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
});
return res.send(brew.text);
} else {
console.log('Unhandled share type');
}
})
.catch((err)=>{
console.log(err);
return res.status(404).send('Could not find Homebrew with that id');
});
}
};
module.exports = shareFunction;

View File

@@ -1,14 +0,0 @@
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

@@ -65,7 +65,7 @@ const BrewItem = createClass({
</a>;
},
renderShareLink : function(){
renderSourceLink : function(path, icon){
if(!this.props.brew.shareId) return;
let shareLink = this.props.brew.shareId;
@@ -73,8 +73,8 @@ const BrewItem = createClass({
shareLink = this.props.brew.googleId + shareLink;
}
return <a href={`/share/${shareLink}`} target='_blank' rel='noopener noreferrer'>
<i className='fa fa-share-alt' />
return <a href={`/${path}/${shareLink}`} target='_blank' rel='noopener noreferrer'>
<i className={`fa ${icon}`} />
</a>;
},
@@ -107,8 +107,9 @@ const BrewItem = createClass({
</div>
<div className='links'>
{this.renderShareLink()}
{this.renderSourceLink('share', 'fa-share-alt')}
{this.renderEditLink()}
{this.renderSourceLink('download', 'fa-download')}
{this.renderDeleteBrewLink()}
</div>
</div>;