0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/routing/controllers/preview.js
Katharina Irrgang 6b758bda79
Refactored routing config for multiple api versions (#10333)
refs #10124

- one clean v0.1 and v2 config file for routing!
- solves one underlying bug reported in #10124
- the alias handling was just a hotfix to support v2 for the site
- but it was hard to read, ugly
- now we have two clean configs
- we'll see how useful it is
- need to do proper manual testing on Monday
2019-01-04 21:59:39 +01:00

44 lines
1.5 KiB
JavaScript

const debug = require('ghost-ignition').debug('services:routing:controllers:preview'),
urlService = require('../../url'),
filters = require('../../../filters'),
helpers = require('../helpers');
module.exports = function previewController(req, res, next) {
debug('previewController');
const api = require('../../../api')[res.locals.apiVersion];
const params = {
uuid: req.params.uuid,
status: 'all',
include: 'author,authors,tags'
};
api[res.routerOptions.query.controller]
.read(params)
.then(function then(result) {
const post = result[res.routerOptions.query.resource][0];
if (!post) {
return next();
}
if (req.params.options && req.params.options.toLowerCase() === 'edit') {
// CASE: last param of the url is /edit, redirect to admin
return urlService.utils.redirectToAdmin(302, res, '/editor/' + post.id);
} else if (req.params.options) {
// CASE: unknown options param detected, ignore
return next();
}
if (post.status === 'published') {
return urlService.utils.redirect301(res, urlService.getUrlByResourceId(post.id, {withSubdirectory: true}));
}
helpers.secure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(helpers.renderEntry(req, res));
})
.catch(helpers.handleError(next));
};