Ghost/core/server/services/routing/controllers/preview.js
Katharina Irrgang 7b0d5d465b 🐛 Fixed preview url and Zapier on subdirectory (#9683)
closes #9675

- with dynamic routing we have introduced a breaking change, which we have overseen
- Ghost does not return absolute urls, that's why the clients need to concat the blog url and the resource url
- with 1.24.0 Ghost returned resource urls including the subdirectory
- this caused trouble for e.g. zapier or the preview feature in the admin client
- revert breaking change and ensure we only expose resource urls without subdirectory
2018-06-12 16:36:58 +01:00

43 lines
1.4 KiB
JavaScript

const debug = require('ghost-ignition').debug('services:routing:controllers:preview'),
api = require('../../../api'),
urlService = require('../../url'),
filters = require('../../../filters'),
helpers = require('../helpers');
module.exports = function previewController(req, res, next) {
debug('previewController');
const params = {
uuid: req.params.uuid,
status: 'all',
include: 'author,authors,tags'
};
api.posts.read(params)
.then(function then(result) {
const post = result.posts[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));
};