mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 10:12:41 +00:00
(WIP) Unwinding refactoring to improve clarity and readability of new functions
This commit is contained in:
@@ -65,7 +65,7 @@ const BrewItem = createClass({
|
|||||||
</a>;
|
</a>;
|
||||||
},
|
},
|
||||||
|
|
||||||
renderSourceLink : function(path, icon){
|
renderSourceLink : function(){
|
||||||
if(!this.props.brew.shareId) return;
|
if(!this.props.brew.shareId) return;
|
||||||
|
|
||||||
let shareLink = this.props.brew.shareId;
|
let shareLink = this.props.brew.shareId;
|
||||||
@@ -73,8 +73,21 @@ const BrewItem = createClass({
|
|||||||
shareLink = this.props.brew.googleId + shareLink;
|
shareLink = this.props.brew.googleId + shareLink;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <a href={`/${path}/${shareLink}`} target=`${(path != 'download') ? '_blank' : ''}` rel=`${(path != 'download') ? 'noopener noreferrer') ? '_blank' : ''}`>
|
return <a href={`/share/${shareLink}`} target='_blank' rel='noopener noreferrer'>
|
||||||
<i className={`fa ${icon}`} />
|
<i className={'fa fa-share-alt' />
|
||||||
|
</a>;
|
||||||
|
},
|
||||||
|
|
||||||
|
renderDownloadLink : function(){
|
||||||
|
if(!this.props.brew.shareId) return;
|
||||||
|
|
||||||
|
let shareLink = this.props.brew.shareId;
|
||||||
|
if(this.props.brew.googleId) {
|
||||||
|
shareLink = this.props.brew.googleId + shareLink;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <a href={`/download/${shareLink}`}>
|
||||||
|
<i className={'fa fa-download' />
|
||||||
</a>;
|
</a>;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -107,9 +120,9 @@ const BrewItem = createClass({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='links'>
|
<div className='links'>
|
||||||
{this.renderSourceLink('share', 'fa-share-alt')}
|
{this.renderShareLink()}
|
||||||
{this.renderEditLink()}
|
{this.renderEditLink()}
|
||||||
{this.renderSourceLink('download', 'fa-download')}
|
{this.renderDownloadLink()}
|
||||||
{this.renderDeleteBrewLink()}
|
{this.renderDeleteBrewLink()}
|
||||||
</div>
|
</div>
|
||||||
</div>;
|
</div>;
|
||||||
|
|||||||
100
server.js
100
server.js
@@ -71,14 +71,21 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
return res.sendFile(`${__dirname}/robots.txt`);
|
return res.sendFile(`${__dirname}/robots.txt`);
|
||||||
});
|
});
|
||||||
|
|
||||||
const shareFunction = function(req, res, type) {
|
|
||||||
|
//Source page
|
||||||
|
app.get('/source/:id', (req, res)=>{
|
||||||
if(req.params.id.length > 12) {
|
if(req.params.id.length > 12) {
|
||||||
const googleId = req.params.id.slice(0, -12);
|
const googleId = req.params.id.slice(0, -12);
|
||||||
const shareId = req.params.id.slice(-12);
|
const shareId = req.params.id.slice(-12);
|
||||||
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
GoogleActions.readFileMetadata(config.get('google_api_key'), googleId, shareId, 'share')
|
||||||
.then((brew)=>{
|
.then((brew)=>{
|
||||||
setSourceHeaders(res, brew.title, type);
|
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
||||||
res.send(getSourceText(brew.text, type));
|
const 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)=>{
|
.catch((err)=>{
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@@ -86,54 +93,53 @@ const shareFunction = function(req, res, type) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
HomebrewModel.get({ shareId: req.params.id })
|
HomebrewModel.get({ shareId: req.params.id })
|
||||||
.then((brew)=>{
|
.then((brew)=>{
|
||||||
setSourceHeaders(res, brew.title, type);
|
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
||||||
res.send(getSourceText(brew.text, type));
|
const text = brew.text;
|
||||||
})
|
for (const replaceStr in replaceStrings) {
|
||||||
.catch((err)=>{
|
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
||||||
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);
|
||||||
}
|
})
|
||||||
};
|
.catch((err)=>{
|
||||||
|
console.log(err);
|
||||||
const setSourceHeaders = function (res, title, type) {
|
return res.status(404).send('Could not find Homebrew with that id');
|
||||||
res.status(200);
|
|
||||||
if(type == 'download') {
|
|
||||||
const 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') {
|
|
||||||
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
|
||||||
const text = brewText;
|
|
||||||
for (const replaceStr in replaceStrings) {
|
|
||||||
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
|
||||||
}
|
|
||||||
return `<code><pre style="white-space: pre-wrap;">${text}</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');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Source download page
|
//Download brew source page
|
||||||
app.get('/download/:id', (req, res)=>{
|
app.get('/download/:id', (req, res)=>{
|
||||||
return shareFunction(req, res, 'download');
|
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)=>{
|
||||||
|
const fileName = sanitizeFilename(title).replaceAll(' ', '-');
|
||||||
|
if (!fileName || !fileName.length) { fileName = 'Untitled-Brew'; };
|
||||||
|
res.set({
|
||||||
|
'Cache-Control' : 'no-cache',
|
||||||
|
'Content-Type' : 'text/plain',
|
||||||
|
'Content-Disposition' : `attachment; filename="HomeBrewery-${fileName}.txt"`
|
||||||
|
});
|
||||||
|
res.status(200).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');
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//User Page
|
//User Page
|
||||||
|
|||||||
Reference in New Issue
Block a user