mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-02 02:02:43 +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:
56
client/homebrew/pages/sharePage/sourceFunctions.jsx
Normal file
56
client/homebrew/pages/sharePage/sourceFunctions.jsx
Normal 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;
|
||||
Reference in New Issue
Block a user