2017-07-19 23:11:47 +02:00
|
|
|
const { serveClaimByName, serveClaimByClaimId, serveClaimByShortUrl } = require('../controllers/serveController.js');
|
2017-06-30 07:26:29 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-07-06 22:37:03 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
2017-07-17 22:16:11 +02:00
|
|
|
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
|
2017-07-25 09:22:50 +02:00
|
|
|
const { showClaimByName, showClaimByClaimId, showClaimByShortUrl } = require('../controllers/showController.js');
|
|
|
|
const logger = require('winston');
|
2017-06-30 02:10:14 +02:00
|
|
|
|
2017-07-25 09:22:50 +02:00
|
|
|
function retrieveAssetServeInfo (name, claimId) {
|
2017-07-19 18:11:08 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
// if claim id is full 40 chars, retrieve the shortest possible url
|
|
|
|
if (claimId.length === 40) {
|
2017-07-19 23:11:47 +02:00
|
|
|
resolve(serveClaimByClaimId(name, claimId));
|
2017-07-19 18:11:08 +02:00
|
|
|
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
|
|
|
|
} else if (claimId.length < 40) {
|
2017-07-19 23:11:47 +02:00
|
|
|
resolve(serveClaimByShortUrl(name, claimId));
|
2017-07-19 18:11:08 +02:00
|
|
|
} else {
|
|
|
|
reject(new Error('That Claim Id is longer than 40 characters.'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
}
|
|
|
|
|
2017-07-25 09:22:50 +02:00
|
|
|
function retrieveAssetShowInfo (name, claimId) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
// if claim id is full 40 chars, retrieve the shortest possible url
|
|
|
|
if (claimId.length === 40) {
|
|
|
|
resolve(showClaimByClaimId(name, claimId));
|
|
|
|
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
|
|
|
|
} else if (claimId.length < 40) {
|
|
|
|
resolve(showClaimByShortUrl(name, claimId));
|
|
|
|
} else {
|
|
|
|
reject(new Error('That Claim Id is longer than 40 characters.'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
}
|
|
|
|
|
2017-07-25 10:52:31 +02:00
|
|
|
function serveClaimByNameWrapper (name, headers, originalUrl, ip, res) {
|
|
|
|
// begin image-serve processes
|
|
|
|
serveClaimByName(name)
|
|
|
|
.then(fileInfo => {
|
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the file or the show route
|
|
|
|
if (headers['accept']) { // note: added b/c some requests errored out due to no accept param in header
|
|
|
|
const mimetypes = headers['accept'].split(',');
|
|
|
|
if (mimetypes.includes('text/html')) {
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
|
|
|
} else {
|
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function serveAssetByClaimIdWrapper (name, claimId, headers, originalUrl, ip, res) {
|
|
|
|
retrieveAssetServeInfo(name, claimId)
|
|
|
|
.then((fileInfo) => {
|
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the file or the show route
|
|
|
|
if (headers['accept']) {
|
|
|
|
const mimetypes = headers['accept'].split(',');
|
|
|
|
if (mimetypes.includes('text/html')) {
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
|
|
|
} else {
|
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
serveFile(fileInfo, res);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-07-05 18:39:32 +02:00
|
|
|
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
2017-07-25 09:22:50 +02:00
|
|
|
// decide to serve or show
|
|
|
|
const dotIndex = params.claim_id.lastIndexOf('.');
|
|
|
|
if (dotIndex === 0) {
|
2017-07-25 09:38:03 +02:00
|
|
|
logger.error('a file extension with no name was submitted');
|
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no claim id provided'), res);
|
2017-07-25 09:22:50 +02:00
|
|
|
// if an image extension was given, serve the image directly
|
|
|
|
} else if (dotIndex > 0) {
|
2017-07-25 09:38:03 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
2017-07-25 10:52:31 +02:00
|
|
|
// parse params
|
2017-07-25 09:22:50 +02:00
|
|
|
const fileExtension = params.claim_id.substring(dotIndex);
|
2017-07-25 09:38:03 +02:00
|
|
|
const claimId = params.claim_id.substring(0, dotIndex);
|
2017-07-25 09:22:50 +02:00
|
|
|
logger.debug('file extension is:', fileExtension);
|
2017-07-25 10:52:31 +02:00
|
|
|
// start image-serve processes
|
|
|
|
serveAssetByClaimIdWrapper(params.name, claimId, headers, originalUrl, ip, res);
|
2017-07-25 09:22:50 +02:00
|
|
|
// if no image extension was given, show the asset with details
|
|
|
|
} else if (dotIndex === -1) {
|
2017-07-25 09:38:03 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
2017-07-25 10:52:31 +02:00
|
|
|
// for backwards compatability: make sure the client can acept html, if not serve the file.
|
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
// begin image-show processes
|
|
|
|
retrieveAssetShowInfo(params.name, params.claim_id)
|
|
|
|
.then((fileInfo) => {
|
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the file or the show route
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
res.status(200).render('show', { layout: 'show', fileInfo });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// start image-serve processes
|
|
|
|
serveAssetByClaimIdWrapper(params.name, params.claim_id, headers, originalUrl, ip, res);
|
|
|
|
}
|
2017-07-25 09:22:50 +02:00
|
|
|
}
|
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-07-25 09:22:50 +02:00
|
|
|
// decide to serve or show
|
|
|
|
const dotIndex = params.name.lastIndexOf('.');
|
|
|
|
if (dotIndex === 0) {
|
2017-07-25 09:38:03 +02:00
|
|
|
logger.error('a file extension with no name was submitted');
|
|
|
|
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no name provided'), res);
|
2017-07-25 09:22:50 +02:00
|
|
|
// if an image extension was given, serve the image directly
|
|
|
|
} else if (dotIndex > 0) {
|
2017-07-25 09:38:03 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
2017-07-25 10:52:31 +02:00
|
|
|
// parse name param
|
2017-07-25 09:22:50 +02:00
|
|
|
const fileExtension = params.name.substring(dotIndex);
|
2017-07-25 09:38:03 +02:00
|
|
|
const claimName = params.name.substring(0, dotIndex);
|
2017-07-25 09:22:50 +02:00
|
|
|
logger.debug('file extension is:', fileExtension);
|
2017-07-25 10:52:31 +02:00
|
|
|
// start image-serve processes
|
|
|
|
serveClaimByNameWrapper(claimName, headers, originalUrl, ip, res);
|
2017-07-25 09:22:50 +02:00
|
|
|
// if no image extension was given, show the asset with details
|
|
|
|
} else if (dotIndex === -1) {
|
2017-07-25 09:38:03 +02:00
|
|
|
// google analytics
|
|
|
|
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
2017-07-25 10:52:31 +02:00
|
|
|
// for backwards compatability: make sure the client can receive text/html, or else serve the asset directly
|
|
|
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
|
|
// begin image-show process
|
|
|
|
showClaimByName(params.name)
|
|
|
|
.then(fileInfo => {
|
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the show route
|
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
|
|
|
res.status(200).render('show', { layout: 'show', fileInfo });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// start image serve process
|
|
|
|
serveClaimByNameWrapper(params.name, headers, originalUrl, ip, res);
|
|
|
|
}
|
2017-07-25 09:22:50 +02:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
};
|