0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 18:32:41 +00:00

Integrated download function into server.js

Remove new function from `Homebrew.Model.js`
Remove `sourceFunctions.jsx` module
This commit is contained in:
G.Ambatte
2021-01-21 20:27:46 +13:00
parent 0e8348f360
commit 5c7a9c92d1
3 changed files with 52 additions and 70 deletions

View File

@@ -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;

View File

@@ -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 `<code><pre style="white-space: pre-wrap;">${brewText.replaceAll('&', '&amp;').replaceAll('<','&lt;').replaceAll('>', '&gt;')}</pre></code>`;
} 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');

View File

@@ -34,16 +34,6 @@ HomebrewSchema.methods.sanatize = function(full=false){
return brew;
};
HomebrewSchema.methods.escapeTextForHtmlDisplay = function(){
const replaceStrings = { '&': '&amp;', '<': '&lt;', '>': '&gt;' };
text = this.text;
for (const replaceStr in replaceStrings) {
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
}
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
return text;
};
HomebrewSchema.methods.increaseView = async function(){
this.lastViewed = new Date();
this.views = this.views + 1;