mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-12 11:02:42 +00:00
Condense Download and Source paths also
This commit is contained in:
102
server.js
102
server.js
@@ -18,17 +18,6 @@ const sanitizeFilename = require('sanitize-filename');
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//Format brew source for viewing in the browser as plain text
|
|
||||||
// const sanitizeSource = (text)=>{
|
|
||||||
// const replaceStrings = { '&' : '&',
|
|
||||||
// '<' : '<',
|
|
||||||
// '>' : '>' };
|
|
||||||
// for (const replaceStr in replaceStrings) {
|
|
||||||
// text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
|
||||||
// }
|
|
||||||
// return `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
|
||||||
// };
|
|
||||||
|
|
||||||
//Get the brew object from the HB database or Google Drive
|
//Get the brew object from the HB database or Google Drive
|
||||||
const getBrewFromId = async (id, accessType)=>{
|
const getBrewFromId = async (id, accessType)=>{
|
||||||
if(accessType !== 'edit' && accessType !== 'share')
|
if(accessType !== 'edit' && accessType !== 'share')
|
||||||
@@ -110,81 +99,34 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
|
|
||||||
|
|
||||||
//Source page
|
//Source page
|
||||||
app.get('/source/:id', (req, res)=>{
|
app.get('/source/:id', async (req, res)=>{
|
||||||
if(req.params.id.length > 12) {
|
const brew = await getBrewFromId(req.params.id, 'share')
|
||||||
const googleId = req.params.id.slice(0, -12);
|
.catch((err)=>{next(err);});
|
||||||
const shareId = req.params.id.slice(-12);
|
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
||||||
.then((brew)=>{
|
let text = brew.text;
|
||||||
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
for (const replaceStr in replaceStrings) {
|
||||||
let text = brew.text;
|
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
||||||
for (const replaceStr in replaceStrings) {
|
|
||||||
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
|
||||||
}
|
|
||||||
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
|
||||||
res.status(200).send(text);
|
|
||||||
})
|
|
||||||
.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)=>{
|
|
||||||
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
|
||||||
let text = brew.text;
|
|
||||||
for (const replaceStr in replaceStrings) {
|
|
||||||
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
|
||||||
}
|
|
||||||
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
|
||||||
res.status(200).send(text);
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(404).send('Could not find Homebrew with that id');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
text = `<code><pre style="white-space: pre-wrap;">${text}</pre></code>`;
|
||||||
|
res.status(200).send(text);
|
||||||
});
|
});
|
||||||
|
|
||||||
//Download brew source page
|
//Download brew source page
|
||||||
app.get('/download/:id', (req, res)=>{
|
app.get('/download/:id', async (req, res)=>{
|
||||||
|
const brew = await getBrewFromId(req.params.id, 'share')
|
||||||
|
.catch((err)=>{next(err);});
|
||||||
|
|
||||||
const prefix = 'HB - ';
|
const prefix = 'HB - ';
|
||||||
|
|
||||||
if(req.params.id.length > 12) {
|
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
||||||
const googleId = req.params.id.slice(0, -12);
|
if(!fileName || !fileName.length) { fileName = `${prefix}-Untitled-Brew`; };
|
||||||
const shareId = req.params.id.slice(-12);
|
res.set({
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
'Cache-Control' : 'no-cache',
|
||||||
.then((brew)=>{
|
'Content-Type' : 'text/plain',
|
||||||
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
||||||
if(!fileName || !fileName.length) { fileName = `${prefix}-Untitled-Brew`; };
|
});
|
||||||
res.set({
|
res.status(200).send(brew.text);
|
||||||
'Cache-Control' : 'no-cache',
|
|
||||||
'Content-Type' : 'text/plain',
|
|
||||||
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
|
||||||
});
|
|
||||||
res.status(200).send(brew.text);
|
|
||||||
})
|
|
||||||
.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)=>{
|
|
||||||
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
|
||||||
if(!fileName || !fileName.length) { fileName = `${prefix}-Untitled-Brew`; };
|
|
||||||
res.set({
|
|
||||||
'Cache-Control' : 'no-cache',
|
|
||||||
'Content-Type' : 'text/plain',
|
|
||||||
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
|
||||||
});
|
|
||||||
res.status(200).send(brew.text);
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.log(err);
|
|
||||||
return res.status(404).send('Could not find Homebrew with that id');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//User Page
|
//User Page
|
||||||
|
|||||||
Reference in New Issue
Block a user