Ghost/core/frontend/services/routing/controllers/entry.js
Naz Gargol df7e64fafa
Extracted frontend folder (#10780)
refs #10790

- Moved /core/apps into core/frontend
- Moved /core/server/helpers to /core/frontend/helpers along with /core/server/services/themes
- Changed helper location in overrides
- Moved /core/server/services/routing to /core/frontend/services
- Moved /core/server/services/url to /core/frontend/services
- Moved /core/server/data/meta to /core/frontend/meta
- Moved /core/server/services/rss to /core/frontend/services
- Moved /core/server/data/xml to /core/frontend/services
2019-06-19 11:30:28 +02:00

94 lines
3.4 KiB
JavaScript

const debug = require('ghost-ignition').debug('services:routing:controllers:entry'),
url = require('url'),
urlService = require('../../../services/url'),
urlUtils = require('../../../../server/lib/url-utils'),
helpers = require('../helpers');
/**
* @description Entry controller.
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @returns {Promise}
*/
module.exports = function entryController(req, res, next) {
debug('entryController', res.routerOptions);
return helpers.entryLookup(req.path, res.routerOptions, res.locals)
.then(function then(lookup) {
// Format data 1
const entry = lookup ? lookup.entry : false;
if (!entry) {
debug('no entry');
return next();
}
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
if (lookup.isUnknownOption) {
debug('isUnknownOption');
return next();
}
// CASE: last param is of url is /edit, redirect to admin
if (lookup.isEditURL) {
debug('redirect. is edit url');
const resourceType = entry.page ? 'page' : 'post';
return urlUtils.redirectToAdmin(302, res, `/editor/${resourceType}/${entry.id}`);
}
/**
* CASE: check if type of router owns this resource
*
* Static pages have a hardcoded permalink, which is `/:slug/`.
* Imagine you define a collection under `/` with the permalink `/:slug/`.
*
* The router hierarchy is:
*
* 1. collections
* 2. static pages
*
* Both permalinks are registered in express. If you serve a static page, the
* collection router will try to serve this as a post resource.
*
* That's why we have to check against the router type.
*/
if (urlService.getResourceById(entry.id).config.type !== res.routerOptions.resourceType) {
debug('not my resource type');
return next();
}
/**
* CASE: Permalink is not valid anymore, we redirect him permanently to the correct one
* This should only happen if you have date permalinks enabled and you change
* your publish date.
*
* @NOTE:
*
* Ensure we redirect to the correct post url including subdirectory.
*
* @NOTE:
* Keep in mind, that the logic here is used for v0.1 and v2.
* v0.1 returns relative urls, v2 returns absolute urls.
*
* @TODO:
* Simplify if we drop v0.1.
*/
if (urlUtils.absoluteToRelative(entry.url, {withoutSubdirectory: true}) !== req.path) {
debug('redirect');
return urlUtils.redirect301(res, url.format({
pathname: url.parse(entry.url).pathname,
search: url.parse(req.originalUrl).search
}));
}
helpers.secure(req, entry);
const renderer = helpers.renderEntry(req, res);
return renderer(entry);
})
.catch(helpers.handleError(next));
};