Move method to a middleware

This commit is contained in:
Daniel García Aubert 2017-12-30 14:13:23 +01:00
parent 108a319143
commit 500cbb959f

View File

@ -39,6 +39,7 @@ NamedMapsController.prototype.register = function(app) {
allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
this.prepareContext,
this.getNamedMapProvider(),
this.prepareLayerFilterFromPreviewLayers(),
this.staticMap.bind(this)
);
};
@ -61,6 +62,51 @@ NamedMapsController.prototype.getNamedMapProvider = function () {
}.bind(this);
};
NamedMapsController.prototype.prepareLayerFilterFromPreviewLayers = function () {
return function prepareLayerFilterFromPreviewLayersMiddleware (req, res, next) {
const { user, namedMapProvider } = res.locals;
const { template_id } = req.params;
const { config, auth_token } = req.query;
namedMapProvider.getTemplate((err, template) => {
if (err) {
return next(err);
}
if (!template || !template.view || !template.view.preview_layers) {
return next();
}
var previewLayers = template.view.preview_layers;
var layerVisibilityFilter = [];
template.layergroup.layers.forEach(function (layer, index) {
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
layerVisibilityFilter.push(''+index);
}
});
if (!layerVisibilityFilter.length) {
return next();
}
// overwrites 'all' default filter
res.locals.layer = layerVisibilityFilter.join(',');
// recreates the provider
this.namedMapProviderCache.get(user, template_id, config, auth_token, res.locals, (err, provider) => {
if (err) {
return next(err);
}
res.locals.namedMapProvider = provider;
next();
});
});
}.bind(this);
};
NamedMapsController.prototype.sendResponse = function(req, res, body, headers, namedMapProvider) {
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(res.locals.user, namedMapProvider.getTemplateName()));
res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png');
@ -128,9 +174,6 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
const { namedMapProvider } = res.locals;
step(
function prepareLayerVisibility() {
self.prepareLayerFilterFromPreviewLayers(cdbUser, req, res.locals, namedMapProvider, this);
},
function prepareImageOptions(err) {
assert.ifError(err);
self.getStaticImageOptions(cdbUser, res.locals, namedMapProvider, this);
@ -176,51 +219,6 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
);
};
NamedMapsController.prototype.prepareLayerFilterFromPreviewLayers = function (
user,
req,
params,
namedMapProvider,
callback
) {
var self = this;
namedMapProvider.getTemplate(function (err, template) {
if (err) {
return callback(err);
}
if (!template || !template.view || !template.view.preview_layers) {
return callback();
}
var previewLayers = template.view.preview_layers;
var layerVisibilityFilter = [];
template.layergroup.layers.forEach(function (layer, index) {
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
layerVisibilityFilter.push(''+index);
}
});
if (!layerVisibilityFilter.length) {
return callback();
}
// overwrites 'all' default filter
params.layer = layerVisibilityFilter.join(',');
// recreates the provider
self.namedMapProviderCache.get(
user,
req.params.template_id,
req.query.config,
req.query.auth_token,
params,
callback
);
});
};
var DEFAULT_ZOOM_CENTER = {
zoom: 1,
center: {