2017-10-09 12:56:44 +01:00
|
|
|
var utils = require('../utils'),
|
|
|
|
filters = require('../filters'),
|
|
|
|
handleError = require('./frontend/error'),
|
|
|
|
postLookup = require('./frontend/post-lookup'),
|
|
|
|
renderPost = require('./frontend/render-post'),
|
|
|
|
setRequestIsSecure = require('./frontend/secure');
|
|
|
|
|
|
|
|
module.exports = function single(req, res, next) {
|
|
|
|
// Query database to find post
|
|
|
|
return postLookup(req.path).then(function then(lookup) {
|
|
|
|
var post = lookup ? lookup.post : false;
|
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
|
|
|
|
if (lookup.isUnknownOption) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: last param is of url is /edit, redirect to admin
|
|
|
|
if (lookup.isEditURL) {
|
2017-10-12 13:36:50 +01:00
|
|
|
return utils.url.redirectToAdmin(302, res, '#/editor/' + post.id);
|
2017-10-09 12:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
|
|
|
|
if (post.url !== req.path) {
|
2017-10-12 13:36:50 +01:00
|
|
|
return utils.url.redirect301(res, post.url);
|
2017-10-09 12:56:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setRequestIsSecure(req, post);
|
|
|
|
|
|
|
|
filters.doFilter('prePostsRender', post, res.locals)
|
|
|
|
.then(renderPost(req, res));
|
|
|
|
}).catch(handleError(next));
|
|
|
|
};
|