diff --git a/client/homebrew/pages/sharePage/sourceFunctions.jsx b/client/homebrew/pages/sharePage/sourceFunctions.jsx deleted file mode 100644 index 2491c56a3..000000000 --- a/client/homebrew/pages/sharePage/sourceFunctions.jsx +++ /dev/null @@ -1,59 +0,0 @@ -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.escapeTextForHtmlDisplay()); - } else if(type == 'download') { - let fileName = sanitizeFilename(brew.title); - fileName = fileName.replaceAll(' ', '-'); - if(!fileName || !fileName.length) { - fileName = 'Untitled-Brew'; - } - 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.escapeTextForHtmlDisplay()); - } 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; \ No newline at end of file diff --git a/server.js b/server.js index c0ea06c96..d596cc769 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,4 @@ +/* eslint-disable max-lines */ const _ = require('lodash'); const jwt = require('jwt-simple'); const expressStaticGzip = require('express-static-gzip'); @@ -7,7 +8,7 @@ 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'); +const sanitizeFilename = require('sanitize-filename'); // Serve brotli-compressed static files if available app.use('/', expressStaticGzip(`${__dirname}/build`, { @@ -70,6 +71,56 @@ app.get('/robots.txt', (req, res)=>{ return res.sendFile(`${__dirname}/robots.txt`); }); +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)=>{ + setSourceHeaders(res, brew.title, type); + res.send(getSourceText(brew.text, 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)=>{ + setSourceHeaders(res, brew.title, type); + res.send(getSourceText(brew.text, type)); + }) + .catch((err)=>{ + console.log(err); + return res.status(404).send('Could not find Homebrew with that id'); + }); + } +}; + +const setSourceHeaders = function (res, title, type) { + res.status(200); + if (type == 'download') { + let fileName = sanitizeFilename(title).replaceAll(' ', '-'); + + res.set({ + 'Cache-Control' : 'no-cache', + 'Content-Type' : 'text/plain', + 'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"` + }); + } +} + +const getSourceText = function (brewText, type) { + if(type == 'source') { + return `
${brewText.replaceAll('&', '&').replaceAll('<','<').replaceAll('>', '>')}
`; + } else if(type == 'download') { + return brewText; + } else { + console.log('Unhandled source share type'); + return; + } +} + //Source page app.get('/source/:id', (req, res)=>{ return shareFunction(req, res, 'source'); diff --git a/server/homebrew.model.js b/server/homebrew.model.js index 3d417f9e8..05a9ab673 100644 --- a/server/homebrew.model.js +++ b/server/homebrew.model.js @@ -34,16 +34,6 @@ HomebrewSchema.methods.sanatize = function(full=false){ return brew; }; -HomebrewSchema.methods.escapeTextForHtmlDisplay = function(){ - const replaceStrings = { '&': '&', '<': '<', '>': '>' }; - text = this.text; - for (const replaceStr in replaceStrings) { - text = text.replaceAll(replaceStr, replaceStrings[replaceStr]); - } - text = `
${text}
`; - return text; -}; - HomebrewSchema.methods.increaseView = async function(){ this.lastViewed = new Date(); this.views = this.views + 1;