0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/services/routing/controllers/preview.js
kirrg001 e302be2749 Changed preview controller to support v0.1 and v2
refs #9866

- invent preview api, but only used internally
  - the idea of a preview api is definitiely reaslistic and came up in the past a couple of times
- by that we don't have to differentiate between pages or posts controller
- still support v0.1
- preview controller is not registered for http, only internal handling
2018-10-18 19:41:07 +02:00

54 lines
1.7 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'
};
let resourceType = res.routerOptions.resourceType;
/**
* @TODO:
* Remove fallback to posts if we drop v0.1.
*/
if (!api[resourceType]) {
resourceType = 'posts';
}
api[resourceType]
.read(params)
.then(function then(result) {
const post = result[resourceType][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));
};