mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 02:11:44 +03:00
df7e64fafa
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
59 lines
2.0 KiB
JavaScript
59 lines
2.0 KiB
JavaScript
const debug = require('ghost-ignition').debug('services:routing:controllers:preview'),
|
|
urlService = require('../../url'),
|
|
urlUtils = require('../../../../server/lib/url-utils'),
|
|
helpers = require('../helpers');
|
|
|
|
/**
|
|
* @description Preview Controller.
|
|
* @param {Object} req
|
|
* @param {Object} res
|
|
* @param {Function} next
|
|
* @returns {Promise}
|
|
*/
|
|
module.exports = function previewController(req, res, next) {
|
|
debug('previewController');
|
|
|
|
const api = require('../../../../server/api')[res.locals.apiVersion];
|
|
|
|
const params = {
|
|
uuid: req.params.uuid,
|
|
status: 'all',
|
|
// @TODO: Remove "author" if we drop v0.1
|
|
include: 'author,authors,tags'
|
|
};
|
|
|
|
return 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') {
|
|
// @TODO: we don't know which resource type it is, because it's a generic preview handler and the
|
|
// preview API returns {previews: []}
|
|
// @TODO: figure out how to solve better
|
|
const resourceType = post.page ? 'page' : 'post';
|
|
|
|
// CASE: last param of the url is /edit, redirect to admin
|
|
return urlUtils.redirectToAdmin(302, res, `/editor/${resourceType}/${post.id}`);
|
|
} else if (req.params.options) {
|
|
// CASE: unknown options param detected, ignore
|
|
return next();
|
|
}
|
|
|
|
if (post.status === 'published') {
|
|
return urlUtils.redirect301(res, urlService.getUrlByResourceId(post.id, {withSubdirectory: true}));
|
|
}
|
|
|
|
// @TODO: See helpers/secure
|
|
helpers.secure(req, post);
|
|
|
|
const renderer = helpers.renderEntry(req, res);
|
|
return renderer(post);
|
|
})
|
|
.catch(helpers.handleError(next));
|
|
};
|