From 4f8cdc06b0aa1ea079cc76f81f07b1c949d163ed Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 6 Dec 2017 15:35:40 -0800 Subject: [PATCH] made serve url parsing more DRY --- routes/api-routes.js | 5 ++--- routes/serve-routes.js | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/routes/api-routes.js b/routes/api-routes.js index 0a3d231b..63414870 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -92,7 +92,7 @@ module.exports = (app) => { if (result === true) { res.status(200).json(true); } 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); } }) @@ -107,12 +107,11 @@ module.exports = (app) => { if (result === true) { res.status(200).json(true); } 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); } }) .catch(error => { - logger.debug('api/channel-is-available/ error', error); res.status(500).json(error); }); }); diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 6cdcf0fd..82c14450 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -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) { let responseType; - if (uri.indexOf('.') !== -1) { + if (characterExistsInString('.', uri)) { 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; } } else { 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; } } @@ -161,11 +169,11 @@ function determineResponseType (uri, headers) { function determineName (uri) { /* patch because twitter player preview adds '>' before file extension. */ - if (uri.indexOf('>') !== -1) { + if (characterExistsInString('>', uri)) { return uri.substring(0, uri.indexOf('>')); } /* end patch */ - if (uri.indexOf('.') !== -1) { + if (characterExistsInString('.', uri)) { return uri.substring(0, uri.indexOf('.')); } return uri;