mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-06 12:12:42 +00:00
Integrated download function into server.js
Remove new function from `Homebrew.Model.js` Remove `sourceFunctions.jsx` module
This commit is contained in:
@@ -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;
|
|
||||||
53
server.js
53
server.js
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable max-lines */
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const jwt = require('jwt-simple');
|
const jwt = require('jwt-simple');
|
||||||
const expressStaticGzip = require('express-static-gzip');
|
const expressStaticGzip = require('express-static-gzip');
|
||||||
@@ -7,7 +8,7 @@ const app = express();
|
|||||||
const homebrewApi = require('./server/homebrew.api.js');
|
const homebrewApi = require('./server/homebrew.api.js');
|
||||||
const GoogleActions = require('./server/googleActions.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
|
// Serve brotli-compressed static files if available
|
||||||
app.use('/', expressStaticGzip(`${__dirname}/build`, {
|
app.use('/', expressStaticGzip(`${__dirname}/build`, {
|
||||||
@@ -70,6 +71,56 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
return res.sendFile(`${__dirname}/robots.txt`);
|
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('&', '&').replaceAll('<','<').replaceAll('>', '>')}</pre></code>`;
|
||||||
|
} else if(type == 'download') {
|
||||||
|
return brewText;
|
||||||
|
} else {
|
||||||
|
console.log('Unhandled source share type');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Source page
|
//Source page
|
||||||
app.get('/source/:id', (req, res)=>{
|
app.get('/source/:id', (req, res)=>{
|
||||||
return shareFunction(req, res, 'source');
|
return shareFunction(req, res, 'source');
|
||||||
|
|||||||
@@ -34,16 +34,6 @@ HomebrewSchema.methods.sanatize = function(full=false){
|
|||||||
return brew;
|
return brew;
|
||||||
};
|
};
|
||||||
|
|
||||||
HomebrewSchema.methods.escapeTextForHtmlDisplay = function(){
|
|
||||||
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
|
||||||
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(){
|
HomebrewSchema.methods.increaseView = async function(){
|
||||||
this.lastViewed = new Date();
|
this.lastViewed = new Date();
|
||||||
this.views = this.views + 1;
|
this.views = this.views + 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user