made serve url parsing more DRY

This commit is contained in:
bill bittner 2017-12-06 15:35:40 -08:00
parent 6e20f89317
commit 4f8cdc06b0
2 changed files with 15 additions and 8 deletions

View file

@ -92,7 +92,7 @@ module.exports = (app) => {
if (result === true) { if (result === true) {
res.status(200).json(true); res.status(200).json(true);
} else { } else {
logger.debug(`Rejecting '${params.name}' because that name has already been claimed on spee.ch`); // logger.debug(`Rejecting '${params.name}' because that name has already been claimed on spee.ch`);
res.status(200).json(false); res.status(200).json(false);
} }
}) })
@ -107,12 +107,11 @@ module.exports = (app) => {
if (result === true) { if (result === true) {
res.status(200).json(true); res.status(200).json(true);
} else { } else {
logger.debug(`Rejecting '${params.name}' because that channel has already been claimed on spee.ch`); // logger.debug(`Rejecting '${params.name}' because that channel has already been claimed on spee.ch`);
res.status(200).json(false); res.status(200).json(false);
} }
}) })
.catch(error => { .catch(error => {
logger.debug('api/channel-is-available/ error', error);
res.status(500).json(error); res.status(500).json(error);
}); });
}); });

View file

@ -143,16 +143,24 @@ function showChannelPageToClient (uri, originalUrl, ip, query, res) {
}); });
} }
function characterExistsInString (character, string) {
return (string.indexOf(character) !== -1);
}
function clientAcceptsHtml (headers) {
return headers['accept'] && headers['accept'].split(',').includes('text/html');
}
function determineResponseType (uri, headers) { function determineResponseType (uri, headers) {
let responseType; let responseType;
if (uri.indexOf('.') !== -1) { if (characterExistsInString('.', uri)) {
responseType = SERVE; responseType = SERVE;
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) { if (clientAcceptsHtml(headers)) { // this is in case a serve request comes from a browser
responseType = SHOWLITE; responseType = SHOWLITE;
} }
} else { } else {
responseType = SHOW; responseType = SHOW;
if (!headers['accept'] || !headers['accept'].split(',').includes('text/html')) { if (!clientAcceptsHtml(headers)) { // this is in case someone embeds a show url
responseType = SERVE; responseType = SERVE;
} }
} }
@ -161,11 +169,11 @@ function determineResponseType (uri, headers) {
function determineName (uri) { function determineName (uri) {
/* patch because twitter player preview adds '>' before file extension. */ /* patch because twitter player preview adds '>' before file extension. */
if (uri.indexOf('>') !== -1) { if (characterExistsInString('>', uri)) {
return uri.substring(0, uri.indexOf('>')); return uri.substring(0, uri.indexOf('>'));
} }
/* end patch */ /* end patch */
if (uri.indexOf('.') !== -1) { if (characterExistsInString('.', uri)) {
return uri.substring(0, uri.indexOf('.')); return uri.substring(0, uri.indexOf('.'));
} }
return uri; return uri;