mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-27 13:42:38 +00:00
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`.
56 lines
1.8 KiB
JavaScript
56 lines
1.8 KiB
JavaScript
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; |