spee.ch/routes/serve-routes.js

173 lines
5.8 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
2017-08-24 02:00:16 +02:00
const { getAssetByClaim, getChannelContents, getAssetByChannel, serveOrShowAsset } = require('../controllers/serveController.js');
2017-08-03 02:13:02 +02:00
const { handleRequestError } = require('../helpers/errorHandlers.js');
2017-08-24 02:00:16 +02:00
const { SERVE, SHOW, SHOWLITE, CHANNEL, CLAIM, CHANNELID_INDICATOR } = require('../helpers/constants.js');
function isValidClaimId (claimId) {
2017-08-04 06:59:22 +02:00
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
}
2017-08-10 19:49:19 +02:00
function isValidShortId (claimId) {
return claimId.length === 1; // really it should evaluate the short url itself
}
2017-08-10 19:49:19 +02:00
function isValidShortIdOrClaimId (input) {
return (isValidClaimId(input) || isValidShortId(input));
}
2017-08-24 02:00:16 +02: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-06-30 02:10:14 +02:00
module.exports = (app) => {
// route to serve a specific asset
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
let identifier = params.identifier;
let name = params.name;
let claimType;
let channelName = null;
2017-08-24 02:00:16 +02:00
let claimId = null;
2017-08-22 03:03:57 +02:00
let channelId = null;
let method;
let extension;
// parse the name
const positionOfExtension = name.indexOf('.');
if (positionOfExtension >= 0) {
extension = name.substring(positionOfExtension);
2017-08-04 07:09:36 +02:00
name = name.substring(0, positionOfExtension);
/* patch because twitter player preview adds '>' before file extension */
if (name.indexOf('>') >= 0) {
name = name.substring(0, name.indexOf('>'));
}
/* end patch */
logger.debug('file extension =', extension);
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
method = SHOWLITE;
} else {
method = SERVE;
}
} else {
method = SHOW;
}
/* patch for backwards compatability with spee.ch/name/claim_id */
2017-08-10 19:49:19 +02:00
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
let tempName = name;
name = identifier;
identifier = tempName;
}
/* end patch */
logger.debug('claim name =', name);
logger.debug('method =', method);
// parse identifier for whether it is a channel, short url, or claim_id
if (identifier.charAt(0) === '@') {
2017-08-22 03:03:57 +02:00
channelName = identifier;
claimType = CHANNEL;
2017-08-22 03:03:57 +02:00
const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR);
if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1);
channelName = channelName.substring(0, channelIdIndex);
}
logger.debug('channel name =', channelName);
} else {
2017-08-24 02:00:16 +02:00
claimId = identifier;
logger.debug('claim id =', claimId);
claimType = CLAIM;
}
// 1. retrieve the asset and information
2017-08-24 02:00:16 +02:00
getAsset(claimType, channelName, channelId, name, claimId)
// 2. serve or show
.then(fileInfo => {
2017-08-21 01:56:37 +02:00
logger.debug('fileInfo', fileInfo);
if (!fileInfo) {
res.status(200).render('noClaims');
2017-08-03 20:14:50 +02:00
} else {
return serveOrShowAsset(fileInfo, extension, method, headers, originalUrl, ip, res);
}
})
2017-08-04 20:32:21 +02:00
// 3. update the file
.then(fileInfoForUpdate => {
2017-08-04 20:32:21 +02:00
// if needed, this is where we would update the file
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);
});
});
// route to serve the winning asset at a claim
2017-07-05 18:26:22 +02:00
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
2017-08-03 20:14:50 +02:00
// parse name param
let name = params.name;
let method;
2017-08-04 07:09:36 +02:00
let fileExtension;
2017-08-23 21:21:15 +02:00
let channelName = null;
let channelId = null;
if (name.charAt(0) === '@') {
channelName = name;
const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR);
if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1);
channelName = channelName.substring(0, channelIdIndex);
2017-08-03 20:14:50 +02:00
}
2017-08-23 21:21:15 +02:00
logger.debug('channel name =', channelName);
logger.debug('channel Id =', channelId);
// 1. retrieve the channel contents
getChannelContents(channelName, channelId)
// 2. respond to the request
.then(channelContents => {
if (!channelContents) {
res.status(200).render('noChannel');
} else {
const handlebarsData = {
channelName,
channelContents,
};
res.status(200).render('channel', handlebarsData);
}
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);
});
2017-08-03 20:14:50 +02:00
} else {
2017-08-23 21:21:15 +02:00
if (name.indexOf('.') !== -1) {
2017-08-03 20:14:50 +02:00
method = SERVE;
2017-08-23 21:21:15 +02:00
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
method = SHOWLITE;
}
fileExtension = name.substring(name.indexOf('.'));
name = name.substring(0, name.indexOf('.'));
logger.debug('file extension =', fileExtension);
2017-08-03 20:14:50 +02:00
} else {
2017-08-23 21:21:15 +02:00
method = SHOW;
if (headers['accept'] && !headers['accept'].split(',').includes('text/html')) {
method = SERVE;
}
2017-08-03 20:14:50 +02:00
}
2017-08-23 21:21:15 +02:00
logger.debug('claim name = ', name);
logger.debug('method =', method);
// 1. retrieve the asset and information
2017-08-24 02:00:16 +02:00
getAsset(CLAIM, null, null, null, name, null)
2017-08-23 21:21:15 +02:00
// 2. respond to the request
.then(fileInfo => {
if (!fileInfo) {
res.status(200).render('noClaims');
} else {
return serveOrShowAsset(fileInfo, null, method, headers, originalUrl, ip, res);
}
})
// 3. update the database
.then(fileInfoForUpdate => {
// if needed, this is where we would update the file
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);
});
}
});
};