2017-08-02 03:58:13 +02:00
|
|
|
const logger = require('winston');
|
2017-12-06 20:53:31 +01:00
|
|
|
const { getClaimId, getChannelInfoAndContent, getLocalFileRecord } = require('../controllers/serveController.js');
|
2017-11-27 23:35:29 +01:00
|
|
|
const serveHelpers = require('../helpers/serveHelpers.js');
|
2017-08-03 02:13:02 +02:00
|
|
|
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
2017-11-21 21:53:43 +01:00
|
|
|
const db = require('../models');
|
2017-09-08 00:31:32 +02:00
|
|
|
|
|
|
|
const SERVE = 'SERVE';
|
|
|
|
const SHOW = 'SHOW';
|
|
|
|
const SHOWLITE = 'SHOWLITE';
|
2017-10-13 02:56:23 +02:00
|
|
|
const CLAIM_ID_CHAR = ':';
|
|
|
|
const CHANNEL_CHAR = '@';
|
|
|
|
const CLAIMS_PER_PAGE = 10;
|
2017-10-18 00:09:43 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-10-18 00:50:35 +02:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
2017-11-28 00:57:20 +01:00
|
|
|
const NO_FILE = 'NO_FILE';
|
2017-07-25 10:52:31 +02:00
|
|
|
|
2017-08-04 01:46:44 +02:00
|
|
|
function isValidClaimId (claimId) {
|
2017-08-04 06:59:22 +02:00
|
|
|
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
2017-08-04 01:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:49:19 +02:00
|
|
|
function isValidShortId (claimId) {
|
2017-11-27 23:35:29 +01:00
|
|
|
return claimId.length === 1; // it should really evaluate the short url itself
|
2017-08-04 01:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 19:49:19 +02:00
|
|
|
function isValidShortIdOrClaimId (input) {
|
|
|
|
return (isValidClaimId(input) || isValidShortId(input));
|
2017-08-04 01:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:15:09 +01:00
|
|
|
function isUriAChannel (name) {
|
|
|
|
return (name.charAt(0) === CHANNEL_CHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnChannelNameFromUri (uri) {
|
|
|
|
if (uri.indexOf(CLAIM_ID_CHAR) !== -1) {
|
|
|
|
return uri.substring(0, uri.indexOf(CLAIM_ID_CHAR));
|
|
|
|
}
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnChannelIdFromUri (uri) {
|
|
|
|
if (uri.indexOf(CLAIM_ID_CHAR) !== -1) {
|
|
|
|
return uri.substring(uri.indexOf(CLAIM_ID_CHAR) + 1);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-13 02:56:23 +02:00
|
|
|
function getPage (query) {
|
|
|
|
if (query.p) {
|
|
|
|
return parseInt(query.p);
|
|
|
|
}
|
2017-10-13 04:08:04 +02:00
|
|
|
return 1;
|
2017-10-13 02:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractPageFromClaims (claims, pageNumber) {
|
2017-11-27 22:15:09 +01:00
|
|
|
if (!claims) {
|
|
|
|
return []; // if no claims, return this default
|
|
|
|
}
|
2017-10-13 02:56:23 +02:00
|
|
|
logger.debug('claims is array?', Array.isArray(claims));
|
2017-10-13 03:55:26 +02:00
|
|
|
logger.debug(`pageNumber ${pageNumber} is number?`, Number.isInteger(pageNumber));
|
2017-10-13 04:08:04 +02:00
|
|
|
const claimStartIndex = (pageNumber - 1) * CLAIMS_PER_PAGE;
|
2017-10-13 03:55:26 +02:00
|
|
|
const claimEndIndex = claimStartIndex + 10;
|
|
|
|
const pageOfClaims = claims.slice(claimStartIndex, claimEndIndex);
|
|
|
|
return pageOfClaims;
|
2017-10-13 02:56:23 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:15:09 +01:00
|
|
|
function determineTotalPages (claims) {
|
|
|
|
if (!claims) {
|
2017-10-13 04:08:04 +02:00
|
|
|
return 0;
|
2017-11-27 22:15:09 +01:00
|
|
|
} else {
|
|
|
|
const totalClaims = claims.length;
|
|
|
|
if (totalClaims < CLAIMS_PER_PAGE) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const fullPages = Math.floor(totalClaims / CLAIMS_PER_PAGE);
|
|
|
|
const remainder = totalClaims % CLAIMS_PER_PAGE;
|
|
|
|
if (remainder === 0) {
|
|
|
|
return fullPages;
|
|
|
|
}
|
|
|
|
return fullPages + 1;
|
2017-10-13 02:56:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function determinePreviousPage (currentPage) {
|
2017-10-13 04:08:04 +02:00
|
|
|
if (currentPage === 1) {
|
|
|
|
return null;
|
2017-10-13 02:56:23 +02:00
|
|
|
}
|
|
|
|
return currentPage - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineNextPage (totalPages, currentPage) {
|
|
|
|
if (currentPage === totalPages) {
|
2017-10-13 04:08:04 +02:00
|
|
|
return null;
|
2017-10-13 02:56:23 +02:00
|
|
|
}
|
|
|
|
return currentPage + 1;
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:15:09 +01:00
|
|
|
function determineTotalClaims (result) {
|
|
|
|
if (!result.claims) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return result.claims.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnOptionsForChannelPageRendering (result, query) {
|
|
|
|
const totalPages = determineTotalPages(result.claims);
|
|
|
|
const paginationPage = getPage(query);
|
|
|
|
const options = {
|
2017-12-05 19:18:49 +01:00
|
|
|
layout : 'channel',
|
|
|
|
channelName : result.channelName,
|
|
|
|
longChannelClaimId : result.longChannelClaimId,
|
|
|
|
shortChannelClaimId: result.shortChannelClaimId,
|
|
|
|
claims : extractPageFromClaims(result.claims, paginationPage),
|
|
|
|
previousPage : determinePreviousPage(paginationPage),
|
|
|
|
currentPage : paginationPage,
|
|
|
|
nextPage : determineNextPage(totalPages, paginationPage),
|
|
|
|
totalPages : totalPages,
|
|
|
|
totalResults : determineTotalClaims(result),
|
2017-11-27 22:15:09 +01:00
|
|
|
};
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-12-06 20:53:31 +01:00
|
|
|
function sendChannelInfoAndContentToClient (result, query, res) {
|
2017-11-27 22:15:09 +01:00
|
|
|
if (result === NO_CHANNEL) { // (a) no channel found
|
|
|
|
res.status(200).render('noChannel');
|
|
|
|
} else { // (b) channel found
|
|
|
|
const options = returnOptionsForChannelPageRendering(result, query);
|
|
|
|
res.status(200).render('channel', options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 23:35:29 +01:00
|
|
|
function showChannelPageToClient (uri, originalUrl, ip, query, res) {
|
2017-11-27 22:15:09 +01:00
|
|
|
let channelName = returnChannelNameFromUri(uri);
|
|
|
|
logger.debug('channel name =', channelName);
|
2017-12-05 19:18:49 +01:00
|
|
|
let channelClaimId = returnChannelIdFromUri(uri);
|
|
|
|
logger.debug('channel Id =', channelClaimId);
|
2017-11-27 22:15:09 +01:00
|
|
|
// 1. retrieve the channel contents
|
2017-12-06 20:53:31 +01:00
|
|
|
getChannelInfoAndContent(channelName, channelClaimId)
|
2017-11-27 22:15:09 +01:00
|
|
|
.then(result => {
|
2017-12-06 20:53:31 +01:00
|
|
|
sendChannelInfoAndContentToClient(result, query, res);
|
2017-11-27 22:15:09 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineResponseType (uri, headers) {
|
|
|
|
let responseType;
|
|
|
|
if (uri.indexOf('.') !== -1) {
|
|
|
|
responseType = SERVE;
|
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
responseType = SHOWLITE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
responseType = SHOW;
|
2017-11-29 23:36:51 +01:00
|
|
|
if (!headers['accept'] || !headers['accept'].split(',').includes('text/html')) {
|
|
|
|
responseType = SERVE;
|
|
|
|
}
|
2017-11-27 22:15:09 +01:00
|
|
|
}
|
|
|
|
return responseType;
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineName (uri) {
|
2017-11-29 23:36:51 +01:00
|
|
|
/* patch because twitter player preview adds '>' before file extension. */
|
2017-11-27 23:35:29 +01:00
|
|
|
if (uri.indexOf('>') !== -1) {
|
|
|
|
return uri.substring(0, uri.indexOf('>'));
|
|
|
|
}
|
|
|
|
/* end patch */
|
2017-11-27 22:15:09 +01:00
|
|
|
if (uri.indexOf('.') !== -1) {
|
|
|
|
return uri.substring(0, uri.indexOf('.'));
|
|
|
|
}
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2017-11-27 23:35:29 +01:00
|
|
|
function showAssetToClient (claimId, name, res) {
|
|
|
|
return Promise
|
2017-12-06 20:53:31 +01:00
|
|
|
.all([db.Claim.resolveClaim(name, claimId), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
2017-11-27 23:52:46 +01:00
|
|
|
.then(([claimInfo, shortClaimId]) => {
|
|
|
|
logger.debug('claimInfo:', claimInfo);
|
|
|
|
logger.debug('shortClaimId:', shortClaimId);
|
|
|
|
return serveHelpers.showFile(claimInfo, shortClaimId, res);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-27 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function showPlainAssetToClient (claimId, name, res) {
|
|
|
|
return Promise
|
2017-12-06 20:53:31 +01:00
|
|
|
.all([db.Claim.resolveClaim(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
2017-11-27 23:52:46 +01:00
|
|
|
.then(([claimInfo, shortClaimId]) => {
|
|
|
|
logger.debug('claimInfo:', claimInfo);
|
|
|
|
logger.debug('shortClaimId:', shortClaimId);
|
|
|
|
return serveHelpers.showFileLite(claimInfo, shortClaimId, res);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-27 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function serveAssetToClient (claimId, name, res) {
|
|
|
|
return getLocalFileRecord(claimId, name)
|
2017-11-27 23:52:46 +01:00
|
|
|
.then(fileInfo => {
|
|
|
|
logger.debug('fileInfo:', fileInfo);
|
2017-11-28 00:57:20 +01:00
|
|
|
if (fileInfo === NO_FILE) {
|
2017-12-05 19:18:49 +01:00
|
|
|
res.status(307).redirect(`/api/claim-get/${name}/${claimId}`);
|
2017-11-28 00:57:20 +01:00
|
|
|
} else {
|
|
|
|
return serveHelpers.serveFile(fileInfo, claimId, name, res);
|
2017-11-27 23:52:46 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error;
|
|
|
|
});
|
2017-11-27 23:35:29 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 03:12:58 +01:00
|
|
|
function showOrServeAsset (responseType, claimId, claimName, res) {
|
|
|
|
switch (responseType) {
|
|
|
|
case SHOW:
|
|
|
|
return showAssetToClient(claimId, claimName, res);
|
|
|
|
case SHOWLITE:
|
|
|
|
return showPlainAssetToClient(claimId, claimName, res);
|
|
|
|
case SERVE:
|
|
|
|
return serveAssetToClient(claimId, claimName, res);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function flipClaimNameAndIdForBackwardsCompatibility (identifier, name) {
|
2017-11-30 19:02:18 +01:00
|
|
|
// this is a patch for backwards compatability with 'spee.ch/name/claim_id' url format
|
2017-11-28 03:12:58 +01:00
|
|
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
|
|
|
const tempName = name;
|
|
|
|
name = identifier;
|
|
|
|
identifier = tempName;
|
|
|
|
}
|
|
|
|
return [identifier, name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function logRequestData (responseType, claimName, channelName, claimId) {
|
|
|
|
logger.debug('responseType ===', responseType);
|
|
|
|
logger.debug('claim name === ', claimName);
|
|
|
|
logger.debug('channel name ===', channelName);
|
|
|
|
logger.debug('claim id ===', claimId);
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:10:14 +02:00
|
|
|
module.exports = (app) => {
|
2017-11-30 19:02:18 +01:00
|
|
|
// route to serve a specific asset using the channel or claim id
|
2017-08-01 02:02:39 +02:00
|
|
|
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-12-06 00:11:16 +01:00
|
|
|
let identifier = params.identifier; // '@channel', '@channel:channelClaimId', or 'claimId'
|
2017-11-30 19:02:18 +01:00
|
|
|
let name = params.name; // 'example' or 'example.ext'
|
|
|
|
[identifier, name] = flipClaimNameAndIdForBackwardsCompatibility(identifier, name);
|
2017-08-01 02:02:39 +02:00
|
|
|
let channelName = null;
|
2017-08-24 02:00:16 +02:00
|
|
|
let claimId = null;
|
2017-12-06 00:11:16 +01:00
|
|
|
let channelClaimId = null;
|
2017-11-27 23:35:29 +01:00
|
|
|
let responseType = determineResponseType(name, headers);
|
|
|
|
let claimName = determineName(name);
|
2017-11-27 22:15:09 +01:00
|
|
|
if (isUriAChannel(identifier)) {
|
|
|
|
channelName = returnChannelNameFromUri(identifier);
|
2017-12-06 00:11:16 +01:00
|
|
|
channelClaimId = returnChannelIdFromUri(identifier);
|
2017-08-02 03:58:13 +02:00
|
|
|
} else {
|
2017-08-24 02:00:16 +02:00
|
|
|
claimId = identifier;
|
|
|
|
}
|
2017-11-30 19:02:18 +01:00
|
|
|
// log the request data for debugging
|
2017-11-28 03:12:58 +01:00
|
|
|
logRequestData(responseType, claimName, channelName, claimId);
|
2017-11-30 19:02:18 +01:00
|
|
|
// get the claim Id and then serve/show the asset
|
2017-12-06 00:11:16 +01:00
|
|
|
getClaimId(channelName, channelClaimId, claimName, claimId)
|
2017-11-28 00:57:20 +01:00
|
|
|
.then(fullClaimId => {
|
|
|
|
if (fullClaimId === NO_CLAIM) {
|
2017-11-28 03:12:58 +01:00
|
|
|
return res.status(200).render('noClaim');
|
2017-11-28 00:57:20 +01:00
|
|
|
} else if (fullClaimId === NO_CHANNEL) {
|
2017-11-28 03:12:58 +01:00
|
|
|
return res.status(200).render('noChannel');
|
2017-08-02 22:16:39 +02:00
|
|
|
}
|
2017-11-28 03:12:58 +01:00
|
|
|
showOrServeAsset(responseType, fullClaimId, claimName, res);
|
2017-08-02 22:16:39 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-11-30 19:02:18 +01:00
|
|
|
// route to serve the winning asset at a claim or a channel page
|
2017-11-27 22:15:09 +01:00
|
|
|
app.get('/:uri', ({ headers, ip, originalUrl, params, query }, res) => {
|
|
|
|
let uri = params.uri;
|
|
|
|
if (isUriAChannel(uri)) {
|
2017-11-27 23:35:29 +01:00
|
|
|
showChannelPageToClient(uri, originalUrl, ip, query, res);
|
2017-08-03 20:14:50 +02:00
|
|
|
} else {
|
2017-11-27 22:15:09 +01:00
|
|
|
let responseType = determineResponseType(uri, headers);
|
2017-11-28 00:57:20 +01:00
|
|
|
let claimName = determineName(uri);
|
2017-11-30 19:02:18 +01:00
|
|
|
// log the request data for debugging
|
2017-11-28 03:12:58 +01:00
|
|
|
logRequestData(responseType, claimName, null, null);
|
2017-11-30 19:02:18 +01:00
|
|
|
// get the claim Id and then serve/show the asset
|
2017-11-28 00:57:20 +01:00
|
|
|
getClaimId(null, null, claimName, null)
|
|
|
|
.then(fullClaimId => {
|
|
|
|
if (fullClaimId === NO_CLAIM) {
|
2017-11-28 03:12:58 +01:00
|
|
|
return res.status(200).render('noClaim');
|
2017-11-21 21:53:43 +01:00
|
|
|
}
|
2017-11-28 03:12:58 +01:00
|
|
|
showOrServeAsset(responseType, fullClaimId, claimName, res);
|
2017-11-21 21:53:43 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-11-27 23:35:29 +01:00
|
|
|
handleRequestError(responseType, originalUrl, ip, error, res);
|
2017-11-21 21:53:43 +01:00
|
|
|
});
|
2017-08-23 21:21:15 +02:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|