2017-06-19 18:37:35 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
2017-07-19 23:11:47 +02:00
|
|
|
const { showClaimByName, showClaimByClaimId, showClaimByShortUrl, showAllClaims } = require('../controllers/showController.js');
|
2017-07-11 02:51:29 +02:00
|
|
|
const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js');
|
2017-06-30 02:10:14 +02:00
|
|
|
|
2017-07-19 23:11:47 +02:00
|
|
|
function retrieveAssetInfo (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(showClaimByClaimId(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(showClaimByShortUrl(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-06-28 05:25:36 +02:00
|
|
|
module.exports = (app) => {
|
2017-07-03 20:16:51 +02:00
|
|
|
// route to show 'about' page for spee.ch
|
|
|
|
app.get('/about', ({ ip, originalUrl }, res) => {
|
|
|
|
// get and render the content
|
|
|
|
res.status(200).render('about');
|
|
|
|
});
|
2017-07-11 02:51:29 +02:00
|
|
|
// route to display a list of the trending images
|
|
|
|
app.get('/trending', ({ params, headers }, res) => {
|
|
|
|
const startDate = new Date();
|
|
|
|
startDate.setDate(startDate.getDate() - 1);
|
|
|
|
getTrendingClaims(startDate)
|
|
|
|
.then(result => {
|
2017-07-21 22:40:01 +02:00
|
|
|
// logger.debug(result);
|
2017-07-11 02:51:29 +02:00
|
|
|
res.status(200).render('trending', { trendingAssets: result });
|
2017-06-21 02:39:41 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-07-11 02:51:29 +02:00
|
|
|
errorHandlers.handleRequestError(error, res);
|
2017-06-21 02:39:41 +02:00
|
|
|
});
|
2017-06-20 07:37:36 +02:00
|
|
|
});
|
2017-06-29 23:34:23 +02:00
|
|
|
// route to show statistics for spee.ch
|
2017-06-30 02:10:14 +02:00
|
|
|
app.get('/stats', ({ ip, originalUrl }, res) => {
|
|
|
|
// get and render the content
|
2017-07-11 02:51:29 +02:00
|
|
|
const startDate = new Date();
|
|
|
|
startDate.setDate(startDate.getDate() - 1);
|
|
|
|
getStatsSummary(startDate)
|
2017-06-29 23:34:23 +02:00
|
|
|
.then(result => {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, null, null, 'success');
|
2017-06-29 23:34:23 +02:00
|
|
|
res.status(200).render('statistics', result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError(error, res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to display all free public claims at a given name
|
2017-06-30 02:10:14 +02:00
|
|
|
app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
|
|
|
|
// get and render the content
|
2017-07-19 23:11:47 +02:00
|
|
|
showAllClaims(params.name)
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(orderedFreePublicClaims => {
|
2017-07-01 00:31:23 +02:00
|
|
|
if (!orderedFreePublicClaims) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, null, null, 'success');
|
2017-06-19 18:37:35 +02:00
|
|
|
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-28 07:41:48 +02:00
|
|
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-06 22:37:03 +02:00
|
|
|
// route to show a specific asset
|
2017-07-07 03:03:29 +02:00
|
|
|
app.get('/show/:name/:claim_id', ({ ip, originalUrl, params }, res) => {
|
2017-07-06 22:37:03 +02:00
|
|
|
// begin image-serve processes
|
2017-07-19 23:11:47 +02:00
|
|
|
retrieveAssetInfo(params.name, params.claim_id)
|
2017-07-19 07:05:34 +02:00
|
|
|
.then((fileInfo) => {
|
|
|
|
console.log('SHORT URL:', fileInfo.shortUrl);
|
2017-07-06 22:37:03 +02:00
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the file or the show route
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-06 22:37:03 +02:00
|
|
|
res.status(200).render('show', { fileInfo });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-07-19 23:11:47 +02:00
|
|
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
2017-07-06 22:37:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to show the winning free, public claim
|
2017-07-07 03:03:29 +02:00
|
|
|
app.get('/show/:name', ({ ip, originalUrl, params }, res) => {
|
2017-07-06 22:37:03 +02:00
|
|
|
// get and render the content
|
2017-07-19 23:11:47 +02:00
|
|
|
showClaimByName(params.name)
|
2017-07-06 22:37:03 +02:00
|
|
|
.then(fileInfo => {
|
|
|
|
// check to make sure a file was found
|
|
|
|
if (!fileInfo) {
|
|
|
|
res.status(307).render('noClaims');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// serve the show route
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-07-06 22:37:03 +02:00
|
|
|
res.status(200).render('show', { fileInfo });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-07-19 23:11:47 +02:00
|
|
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
2017-07-06 22:37:03 +02:00
|
|
|
});
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|