spee.ch/routes/serve-routes.js

85 lines
3.4 KiB
JavaScript
Raw Normal View History

const { serveClaimByName, serveClaimByClaimId, serveClaimByShortUrl } = require('../controllers/serveController.js');
2017-06-30 07:26:29 +02:00
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
2017-06-30 02:10:14 +02:00
function retrieveAssetInfo (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(serveClaimByClaimId(name, claimId));
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
} else if (claimId.length < 40) {
resolve(serveClaimByShortUrl(name, claimId));
} else {
reject(new Error('That Claim Id is longer than 40 characters.'));
}
});
return deferred;
}
2017-06-30 02:10:14 +02:00
module.exports = (app) => {
// route to serve a specific asset
2017-07-05 18:39:32 +02:00
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes
retrieveAssetInfo(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
if (headers['accept']) {
const mimetypes = headers['accept'].split(',');
if (mimetypes.includes('text/html')) {
2017-07-13 00:30:31 +02:00
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('showLite', { fileInfo });
} else {
2017-07-13 00:30:31 +02:00
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
} else {
2017-07-13 00:30:31 +02:00
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
2017-06-29 23:48:27 +02:00
errorHandlers.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) => {
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes
serveClaimByName(params.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')) {
2017-07-13 00:30:31 +02:00
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
res.status(200).render('showLite', { fileInfo });
} else {
2017-07-13 00:30:31 +02:00
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
} else {
2017-07-13 00:30:31 +02:00
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
serveFile(fileInfo, res);
}
})
.catch(error => {
2017-06-29 23:48:27 +02:00
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
});
});
};