2017-08-01 18:58:13 -07:00
|
|
|
const logger = require('winston');
|
2017-08-23 17:00:16 -07:00
|
|
|
const { getAssetByClaim, getChannelContents, getAssetByChannel, serveOrShowAsset } = require('../controllers/serveController.js');
|
2017-08-02 17:13:02 -07:00
|
|
|
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
2017-09-07 15:31:32 -07:00
|
|
|
|
|
|
|
const SERVE = 'SERVE';
|
|
|
|
const SHOW = 'SHOW';
|
|
|
|
const SHOWLITE = 'SHOWLITE';
|
|
|
|
const CHANNEL = 'CHANNEL';
|
|
|
|
const CLAIM = 'CLAIM';
|
2017-10-12 17:56:23 -07:00
|
|
|
const CLAIM_ID_CHAR = ':';
|
|
|
|
const CHANNEL_CHAR = '@';
|
|
|
|
const CLAIMS_PER_PAGE = 10;
|
2017-10-17 15:09:43 -07:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-10-17 15:50:35 -07:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
2017-07-25 01:52:31 -07:00
|
|
|
|
2017-08-03 16:46:44 -07:00
|
|
|
function isValidClaimId (claimId) {
|
2017-08-03 21:59:22 -07:00
|
|
|
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2017-08-10 10:49:19 -07:00
|
|
|
function isValidShortId (claimId) {
|
2017-08-03 16:46:44 -07:00
|
|
|
return claimId.length === 1; // really it should evaluate the short url itself
|
|
|
|
}
|
|
|
|
|
2017-08-10 10:49:19 -07:00
|
|
|
function isValidShortIdOrClaimId (input) {
|
|
|
|
return (isValidClaimId(input) || isValidShortId(input));
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
|
|
|
|
2017-08-23 17:00:16 -07:00
|
|
|
function getAsset (claimType, channelName, channelId, name, claimId) {
|
|
|
|
switch (claimType) {
|
|
|
|
case CHANNEL:
|
|
|
|
return getAssetByChannel(channelName, channelId, name);
|
|
|
|
case CLAIM:
|
|
|
|
return getAssetByClaim(name, claimId);
|
|
|
|
default:
|
|
|
|
return new Error('that claim type was not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 17:56:23 -07:00
|
|
|
function getPage (query) {
|
|
|
|
if (query.p) {
|
|
|
|
return parseInt(query.p);
|
|
|
|
}
|
2017-10-12 19:08:04 -07:00
|
|
|
return 1;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractPageFromClaims (claims, pageNumber) {
|
|
|
|
logger.debug('claims is array?', Array.isArray(claims));
|
2017-10-12 18:55:26 -07:00
|
|
|
logger.debug(`pageNumber ${pageNumber} is number?`, Number.isInteger(pageNumber));
|
2017-10-12 19:08:04 -07:00
|
|
|
const claimStartIndex = (pageNumber - 1) * CLAIMS_PER_PAGE;
|
2017-10-12 18:55:26 -07:00
|
|
|
const claimEndIndex = claimStartIndex + 10;
|
|
|
|
const pageOfClaims = claims.slice(claimStartIndex, claimEndIndex);
|
|
|
|
return pageOfClaims;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function determineTotalPages (totalClaims) {
|
|
|
|
if (totalClaims === 0) {
|
2017-10-12 19:08:04 -07:00
|
|
|
return 0;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
if (totalClaims < CLAIMS_PER_PAGE) {
|
2017-10-12 19:08:04 -07:00
|
|
|
return 1;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
const fullPages = Math.floor(totalClaims / CLAIMS_PER_PAGE);
|
|
|
|
const remainder = totalClaims % CLAIMS_PER_PAGE;
|
|
|
|
if (remainder === 0) {
|
2017-10-12 19:08:04 -07:00
|
|
|
return fullPages;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
2017-10-12 19:08:04 -07:00
|
|
|
return fullPages + 1;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function determinePreviousPage (currentPage) {
|
2017-10-12 19:08:04 -07:00
|
|
|
if (currentPage === 1) {
|
|
|
|
return null;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
return currentPage - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineNextPage (totalPages, currentPage) {
|
|
|
|
if (currentPage === totalPages) {
|
2017-10-12 19:08:04 -07:00
|
|
|
return null;
|
2017-10-12 17:56:23 -07:00
|
|
|
}
|
|
|
|
return currentPage + 1;
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:10:14 -07:00
|
|
|
module.exports = (app) => {
|
2017-07-06 13:37:03 -07:00
|
|
|
// route to serve a specific asset
|
2017-07-31 17:02:39 -07:00
|
|
|
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-08-03 16:46:44 -07:00
|
|
|
let identifier = params.identifier;
|
2017-07-31 17:02:39 -07:00
|
|
|
let name = params.name;
|
2017-10-17 15:50:35 -07:00
|
|
|
let claimOrChannel;
|
2017-07-31 17:02:39 -07:00
|
|
|
let channelName = null;
|
2017-08-23 17:00:16 -07:00
|
|
|
let claimId = null;
|
2017-08-21 18:03:57 -07:00
|
|
|
let channelId = null;
|
2017-08-03 16:46:44 -07:00
|
|
|
let method;
|
2017-09-07 09:45:00 -07:00
|
|
|
let fileExtension;
|
2017-08-03 16:46:44 -07:00
|
|
|
// parse the name
|
|
|
|
const positionOfExtension = name.indexOf('.');
|
|
|
|
if (positionOfExtension >= 0) {
|
2017-09-07 09:45:00 -07:00
|
|
|
fileExtension = name.substring(positionOfExtension + 1);
|
2017-08-03 22:09:36 -07:00
|
|
|
name = name.substring(0, positionOfExtension);
|
2017-08-08 15:41:14 -07:00
|
|
|
/* patch because twitter player preview adds '>' before file extension */
|
|
|
|
if (name.indexOf('>') >= 0) {
|
|
|
|
name = name.substring(0, name.indexOf('>'));
|
|
|
|
}
|
|
|
|
/* end patch */
|
2017-09-07 09:45:00 -07:00
|
|
|
logger.debug('file extension =', fileExtension);
|
2017-08-03 16:46:44 -07:00
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
method = SHOWLITE;
|
|
|
|
} else {
|
|
|
|
method = SERVE;
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-07 10:54:07 -07:00
|
|
|
method = SHOW;
|
2017-08-03 16:46:44 -07:00
|
|
|
}
|
2017-08-08 15:41:14 -07:00
|
|
|
/* patch for backwards compatability with spee.ch/name/claim_id */
|
2017-08-10 10:49:19 -07:00
|
|
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
2017-08-03 16:46:44 -07:00
|
|
|
let tempName = name;
|
|
|
|
name = identifier;
|
|
|
|
identifier = tempName;
|
|
|
|
}
|
2017-08-18 16:52:58 -07:00
|
|
|
/* end patch */
|
2017-08-03 16:46:44 -07:00
|
|
|
logger.debug('claim name =', name);
|
|
|
|
logger.debug('method =', method);
|
|
|
|
// parse identifier for whether it is a channel, short url, or claim_id
|
2017-07-31 17:02:39 -07:00
|
|
|
if (identifier.charAt(0) === '@') {
|
2017-08-21 18:03:57 -07:00
|
|
|
channelName = identifier;
|
2017-10-17 15:50:35 -07:00
|
|
|
claimOrChannel = CHANNEL;
|
2017-10-12 17:56:23 -07:00
|
|
|
const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
|
2017-08-21 18:03:57 -07:00
|
|
|
if (channelIdIndex !== -1) {
|
|
|
|
channelId = channelName.substring(channelIdIndex + 1);
|
|
|
|
channelName = channelName.substring(0, channelIdIndex);
|
|
|
|
}
|
2017-08-22 15:50:20 -07:00
|
|
|
logger.debug('channel name =', channelName);
|
2017-08-01 18:58:13 -07:00
|
|
|
} else {
|
2017-08-23 17:00:16 -07:00
|
|
|
claimId = identifier;
|
|
|
|
logger.debug('claim id =', claimId);
|
2017-10-17 15:50:35 -07:00
|
|
|
claimOrChannel = CLAIM;
|
2017-08-23 17:00:16 -07:00
|
|
|
}
|
2017-08-02 13:16:39 -07:00
|
|
|
// 1. retrieve the asset and information
|
2017-10-17 15:50:35 -07:00
|
|
|
getAsset(claimOrChannel, channelName, channelId, name, claimId)
|
2017-08-02 13:16:39 -07:00
|
|
|
// 2. serve or show
|
2017-10-17 15:50:35 -07:00
|
|
|
.then(result => {
|
2017-10-18 11:16:07 -07:00
|
|
|
logger.debug('getAsset result:', result);
|
|
|
|
if (result === NO_CLAIM) {
|
|
|
|
res.status(200).render('noClaim');
|
2017-10-17 15:50:35 -07:00
|
|
|
return;
|
|
|
|
} else if (result === NO_CHANNEL) {
|
2017-10-18 11:16:07 -07:00
|
|
|
res.status(200).render('noChannel');
|
2017-10-17 15:50:35 -07:00
|
|
|
return;
|
2017-08-02 13:16:39 -07:00
|
|
|
}
|
2017-10-17 15:50:35 -07:00
|
|
|
return serveOrShowAsset(result, fileExtension, method, headers, originalUrl, ip, res);
|
2017-08-02 13:16:39 -07:00
|
|
|
})
|
2017-08-04 11:32:21 -07:00
|
|
|
// 3. update the file
|
2017-08-03 16:46:44 -07:00
|
|
|
.then(fileInfoForUpdate => {
|
2017-08-04 11:32:21 -07:00
|
|
|
// if needed, this is where we would update the file
|
2017-08-02 13:16:39 -07:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-07-17 13:16:11 -07:00
|
|
|
// route to serve the winning asset at a claim
|
2017-10-12 17:56:23 -07:00
|
|
|
app.get('/:name', ({ headers, ip, originalUrl, params, query }, res) => {
|
2017-08-03 11:14:50 -07:00
|
|
|
// parse name param
|
|
|
|
let name = params.name;
|
|
|
|
let method;
|
2017-08-03 22:09:36 -07:00
|
|
|
let fileExtension;
|
2017-08-23 12:21:15 -07:00
|
|
|
let channelName = null;
|
|
|
|
let channelId = null;
|
2017-10-12 17:56:23 -07:00
|
|
|
// (a) handle channel requests
|
|
|
|
if (name.charAt(0) === CHANNEL_CHAR) {
|
2017-08-23 12:21:15 -07:00
|
|
|
channelName = name;
|
2017-10-12 17:56:23 -07:00
|
|
|
const paginationPage = getPage(query);
|
|
|
|
const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
|
2017-08-23 12:21:15 -07:00
|
|
|
if (channelIdIndex !== -1) {
|
|
|
|
channelId = channelName.substring(channelIdIndex + 1);
|
|
|
|
channelName = channelName.substring(0, channelIdIndex);
|
2017-08-03 11:14:50 -07:00
|
|
|
}
|
2017-08-23 12:21:15 -07:00
|
|
|
logger.debug('channel name =', channelName);
|
|
|
|
logger.debug('channel Id =', channelId);
|
|
|
|
// 1. retrieve the channel contents
|
|
|
|
getChannelContents(channelName, channelId)
|
|
|
|
// 2. respond to the request
|
2017-09-28 10:51:02 -07:00
|
|
|
.then(result => {
|
2017-10-17 15:09:43 -07:00
|
|
|
if (result === NO_CHANNEL) { // no channel found
|
2017-10-18 11:16:07 -07:00
|
|
|
res.status(200).render('noChannel');
|
2017-10-17 15:09:43 -07:00
|
|
|
} else if (!result.claims) { // channel found, but no claims
|
2017-10-16 21:25:29 -07:00
|
|
|
res.status(200).render('channel', {
|
2017-11-08 10:24:40 -08:00
|
|
|
layout : 'channel',
|
2017-10-16 21:25:29 -07:00
|
|
|
channelName : result.channelName,
|
|
|
|
longChannelId : result.longChannelId,
|
|
|
|
shortChannelId: result.shortChannelId,
|
|
|
|
claims : [],
|
|
|
|
previousPage : 0,
|
|
|
|
currentPage : 0,
|
|
|
|
nextPage : 0,
|
|
|
|
totalPages : 0,
|
|
|
|
totalResults : 0,
|
|
|
|
});
|
2017-10-17 15:09:43 -07:00
|
|
|
} else { // channel found, with claims
|
2017-10-16 21:25:29 -07:00
|
|
|
const totalPages = determineTotalPages(result.claims.length);
|
2017-10-12 17:56:23 -07:00
|
|
|
res.status(200).render('channel', {
|
2017-11-08 10:24:40 -08:00
|
|
|
layout : 'channel',
|
2017-10-12 17:56:23 -07:00
|
|
|
channelName : result.channelName,
|
|
|
|
longChannelId : result.longChannelId,
|
|
|
|
shortChannelId: result.shortChannelId,
|
|
|
|
claims : extractPageFromClaims(result.claims, paginationPage),
|
|
|
|
previousPage : determinePreviousPage(paginationPage),
|
|
|
|
currentPage : paginationPage,
|
|
|
|
nextPage : determineNextPage(totalPages, paginationPage),
|
2017-10-12 18:55:26 -07:00
|
|
|
totalPages : totalPages,
|
2017-10-12 17:56:23 -07:00
|
|
|
totalResults : result.claims.length,
|
|
|
|
});
|
2017-08-23 12:21:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
2017-10-12 17:56:23 -07:00
|
|
|
// (b) handle stream requests
|
2017-08-03 11:14:50 -07:00
|
|
|
} else {
|
2017-08-23 12:21:15 -07:00
|
|
|
if (name.indexOf('.') !== -1) {
|
2017-08-03 11:14:50 -07:00
|
|
|
method = SERVE;
|
2017-08-23 12:21:15 -07:00
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
method = SHOWLITE;
|
|
|
|
}
|
2017-09-07 09:45:00 -07:00
|
|
|
fileExtension = name.substring(name.indexOf('.') + 1);
|
2017-08-23 12:21:15 -07:00
|
|
|
name = name.substring(0, name.indexOf('.'));
|
|
|
|
logger.debug('file extension =', fileExtension);
|
2017-08-03 11:14:50 -07:00
|
|
|
} else {
|
2017-08-23 12:21:15 -07:00
|
|
|
method = SHOW;
|
2017-08-03 11:14:50 -07:00
|
|
|
}
|
2017-08-23 12:21:15 -07:00
|
|
|
logger.debug('claim name = ', name);
|
|
|
|
logger.debug('method =', method);
|
|
|
|
// 1. retrieve the asset and information
|
2017-08-24 09:42:56 -07:00
|
|
|
getAsset(CLAIM, null, null, name, null)
|
2017-08-23 12:21:15 -07:00
|
|
|
// 2. respond to the request
|
2017-10-18 11:16:07 -07:00
|
|
|
.then(result => {
|
|
|
|
logger.debug('getAsset result', result);
|
|
|
|
if (result === NO_CLAIM) {
|
|
|
|
res.status(200).render('noClaim');
|
2017-08-23 12:21:15 -07:00
|
|
|
} else {
|
2017-10-18 11:16:07 -07:00
|
|
|
return serveOrShowAsset(result, fileExtension, method, headers, originalUrl, ip, res);
|
2017-08-23 12:21:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// 3. update the database
|
|
|
|
.then(fileInfoForUpdate => {
|
|
|
|
// if needed, this is where we would update the file
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|