2017-08-02 03:58:13 +02:00
|
|
|
const logger = require('winston');
|
2017-08-10 19:49:19 +02:00
|
|
|
const { serveFile, showFile, showFileLite, getShortIdFromClaimId } = require('../helpers/serveHelpers.js');
|
|
|
|
const { getAssetByChannel, getAssetByShortId, getAssetByClaimId, getAssetByName } = require('../controllers/serveController.js');
|
2017-08-03 02:13:02 +02:00
|
|
|
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
2017-08-04 06:59:22 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-08-01 02:02:39 +02:00
|
|
|
const SERVE = 'SERVE';
|
|
|
|
const SHOW = 'SHOW';
|
|
|
|
const SHOWLITE = 'SHOWLITE';
|
|
|
|
const CHANNEL = 'CHANNEL';
|
|
|
|
const SHORTURL = 'SHORTURL';
|
|
|
|
const CLAIMID = 'CLAIMID';
|
2017-08-03 20:14:50 +02:00
|
|
|
const NAME = 'NAME';
|
2017-07-19 18:11:08 +02:00
|
|
|
|
2017-08-10 19:49:19 +02:00
|
|
|
function getAsset (claimType, channelName, shortId, fullClaimId, name) {
|
2017-08-01 02:02:39 +02:00
|
|
|
switch (claimType) {
|
|
|
|
case CHANNEL:
|
|
|
|
return getAssetByChannel(channelName, name);
|
|
|
|
case SHORTURL:
|
2017-08-10 19:49:19 +02:00
|
|
|
return getAssetByShortId(shortId, name);
|
2017-08-01 02:02:39 +02:00
|
|
|
case CLAIMID:
|
|
|
|
return getAssetByClaimId(fullClaimId, name);
|
2017-08-03 20:14:50 +02:00
|
|
|
case NAME:
|
|
|
|
return getAssetByName(name);
|
2017-08-01 02:02:39 +02:00
|
|
|
default:
|
|
|
|
return new Error('that claim type was not found');
|
|
|
|
}
|
2017-07-25 10:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 06:59:22 +02:00
|
|
|
function serveOrShowAsset (fileInfo, method, headers, originalUrl, ip, res) {
|
2017-08-03 20:14:50 +02:00
|
|
|
// add file extension to the file info
|
|
|
|
fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.'));
|
|
|
|
// serve or show
|
|
|
|
switch (method) {
|
|
|
|
case SERVE:
|
|
|
|
serveFile(fileInfo, res);
|
2017-08-04 06:59:22 +02:00
|
|
|
sendGoogleAnalytics(method, headers, ip, originalUrl);
|
2017-08-03 20:14:50 +02:00
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
return fileInfo;
|
|
|
|
case SHOWLITE:
|
|
|
|
showFileLite(fileInfo, res);
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
return fileInfo;
|
|
|
|
case SHOW:
|
2017-08-10 19:49:19 +02:00
|
|
|
return getShortIdFromClaimId(fileInfo.claimId, fileInfo.height, fileInfo.name)
|
|
|
|
.then(shortId => {
|
|
|
|
fileInfo['shortId'] = shortId;
|
2017-08-03 20:14:50 +02:00
|
|
|
showFile(fileInfo, res);
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
return fileInfo;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log('thowing error...');
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
logger.error('I did not recognize that method');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-04 01:46:44 +02:00
|
|
|
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-04 01:46:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:10:14 +02:00
|
|
|
module.exports = (app) => {
|
2017-07-06 22:37:03 +02:00
|
|
|
// route to serve a specific asset
|
2017-08-01 02:02:39 +02:00
|
|
|
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-08-04 01:46:44 +02:00
|
|
|
let identifier = params.identifier;
|
2017-08-01 02:02:39 +02:00
|
|
|
let name = params.name;
|
|
|
|
let claimType;
|
|
|
|
let channelName = null;
|
2017-08-10 19:49:19 +02:00
|
|
|
let shortId = null;
|
2017-08-01 02:02:39 +02:00
|
|
|
let fullClaimId = null;
|
2017-08-04 01:46:44 +02:00
|
|
|
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);
|
2017-08-09 00:41:14 +02:00
|
|
|
/* patch because twitter player preview adds '>' before file extension */
|
|
|
|
if (name.indexOf('>') >= 0) {
|
|
|
|
name = name.substring(0, name.indexOf('>'));
|
|
|
|
}
|
|
|
|
/* end patch */
|
2017-08-04 01:46:44 +02:00
|
|
|
logger.debug('file extension =', extension);
|
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
method = SHOWLITE;
|
|
|
|
} else {
|
|
|
|
method = SERVE;
|
|
|
|
}
|
|
|
|
} else {
|
2017-08-07 19:54:07 +02:00
|
|
|
method = SHOW;
|
2017-08-04 01:46:44 +02:00
|
|
|
}
|
2017-08-09 00:41:14 +02:00
|
|
|
/* patch for backwards compatability with spee.ch/name/claim_id */
|
2017-08-10 19:49:19 +02:00
|
|
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
2017-08-04 01:46:44 +02:00
|
|
|
let tempName = name;
|
|
|
|
name = identifier;
|
|
|
|
identifier = tempName;
|
|
|
|
}
|
2017-08-04 06:59:22 +02:00
|
|
|
/* end */
|
2017-08-04 01:46:44 +02:00
|
|
|
logger.debug('claim name =', name);
|
|
|
|
logger.debug('method =', method);
|
|
|
|
// parse identifier for whether it is a channel, short url, or claim_id
|
2017-08-01 02:02:39 +02:00
|
|
|
if (identifier.charAt(0) === '@') {
|
|
|
|
channelName = identifier.substring(1);
|
|
|
|
logger.debug('channel name =', channelName);
|
|
|
|
claimType = CHANNEL;
|
2017-08-02 03:58:13 +02:00
|
|
|
} else if (identifier.length === 40) {
|
2017-08-01 02:02:39 +02:00
|
|
|
fullClaimId = identifier;
|
|
|
|
logger.debug('full claim id =', fullClaimId);
|
|
|
|
claimType = CLAIMID;
|
2017-08-02 03:58:13 +02:00
|
|
|
} else if (identifier.length < 40) {
|
2017-08-10 19:49:19 +02:00
|
|
|
shortId = identifier;
|
|
|
|
logger.debug('short claim id =', shortId);
|
2017-08-01 02:02:39 +02:00
|
|
|
claimType = SHORTURL;
|
2017-08-02 03:58:13 +02:00
|
|
|
} else {
|
2017-08-04 06:59:22 +02:00
|
|
|
logger.error('The URL provided could not be parsed');
|
2017-08-02 03:58:13 +02:00
|
|
|
res.send('that url is invalid');
|
|
|
|
return;
|
2017-08-01 02:02:39 +02:00
|
|
|
};
|
2017-08-02 22:16:39 +02:00
|
|
|
// 1. retrieve the asset and information
|
2017-08-10 19:49:19 +02:00
|
|
|
getAsset(claimType, channelName, shortId, fullClaimId, name)
|
2017-08-02 22:16:39 +02:00
|
|
|
// 2. serve or show
|
|
|
|
.then(fileInfo => {
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(200).render('noClaims');
|
2017-08-03 20:14:50 +02:00
|
|
|
} else {
|
2017-08-04 06:59:22 +02:00
|
|
|
return serveOrShowAsset(fileInfo, method, headers, originalUrl, ip, res);
|
2017-08-02 22:16:39 +02:00
|
|
|
}
|
|
|
|
})
|
2017-08-04 20:32:21 +02:00
|
|
|
// 3. update the file
|
2017-08-04 01:46:44 +02:00
|
|
|
.then(fileInfoForUpdate => {
|
2017-08-04 20:32:21 +02:00
|
|
|
// if needed, this is where we would update the file
|
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-07-17 22:16:11 +02:00
|
|
|
// 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-03 20:14:50 +02:00
|
|
|
if (name.indexOf('.') !== -1) {
|
|
|
|
method = SERVE;
|
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
method = SHOWLITE;
|
|
|
|
}
|
2017-08-04 07:09:36 +02:00
|
|
|
fileExtension = name.substring(name.indexOf('.'));
|
2017-08-03 20:14:50 +02:00
|
|
|
name = name.substring(0, name.indexOf('.'));
|
2017-08-04 07:09:36 +02:00
|
|
|
logger.debug('file extension =', fileExtension);
|
2017-08-03 20:14:50 +02:00
|
|
|
} else {
|
|
|
|
method = SHOW;
|
|
|
|
if (headers['accept'] && !headers['accept'].split(',').includes('text/html')) {
|
|
|
|
method = SERVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.debug('claim name = ', name);
|
|
|
|
logger.debug('method =', method);
|
|
|
|
// 1. retrieve the asset and information
|
|
|
|
getAsset(NAME, null, null, null, name)
|
|
|
|
// 2. serve or show
|
|
|
|
.then(fileInfo => {
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(200).render('noClaims');
|
|
|
|
} else {
|
2017-08-04 06:59:22 +02:00
|
|
|
return serveOrShowAsset(fileInfo, method, headers, originalUrl, ip, res);
|
2017-08-03 20:14:50 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// 3. update the database
|
2017-08-04 01:46:44 +02:00
|
|
|
.then(fileInfoForUpdate => {
|
2017-08-04 20:32:21 +02:00
|
|
|
// if needed, this is where we would update the file
|
2017-08-03 20:14:50 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|