From 9b4037079437a8f1e2e9fc17f95f04da5776be2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Tue, 6 Mar 2018 12:44:17 +0100 Subject: [PATCH] Now that mapConfigProvider is linked to 'res.locals' do not pass the whole 'res.locals' to map-config-provider to avoid converting circular structure to JSON --- lib/cartodb/controllers/layergroup.js | 39 +++++++++++++++------------ 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index 0e995c8a..7afd2346 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -83,13 +83,15 @@ LayergroupController.prototype.register = function(app) { this.attributes(this.attributesBackend) ); + const forcedFormat = 'png'; + app.get( app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format', cors(), userMiddleware, allowQueryParams(['layer']), this.prepareContext, - this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), + this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), this.center(this.previewBackend) ); @@ -99,7 +101,7 @@ LayergroupController.prototype.register = function(app) { userMiddleware, allowQueryParams(['layer']), this.prepareContext, - this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi), + this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi, forcedFormat), this.bbox(this.previewBackend) ); @@ -188,11 +190,27 @@ LayergroupController.prototype.analysisNodeStatus = function (analysisStatusBack }.bind(this); }; -LayergroupController.prototype.getMapStoreMapConfigProvider = function (mapStore, userLimitsApi) { +function getRequestParams(locals) { + const params = Object.assign({}, locals); + + delete params.mapConfigProvider; + delete params.allowedQueryParams; + + return params; +} + +LayergroupController.prototype.getMapStoreMapConfigProvider = function (mapStore, userLimitsApi, forcedFormat = null) { return function getMapStoreMapConfigProviderMiddleware (req, res, next) { const { user } = res.locals; - res.locals.mapConfigProvider = new MapStoreMapConfigProvider(mapStore, user, userLimitsApi, res.locals); + const params = getRequestParams(res.locals); + + if (forcedFormat) { + params.format = forcedFormat; + params.layer = params.layer || 'all'; + } + + res.locals.mapConfigProvider = new MapStoreMapConfigProvider(mapStore, user, userLimitsApi, params); next(); }; @@ -338,12 +356,6 @@ LayergroupController.prototype.center = function (previewBackend) { }; const format = req.params.format === 'jpg' ? 'jpeg' : 'png'; - - // We force always the tile to be generated using PNG because - // is the only format we support by now - res.locals.format = 'png'; - res.locals.layer = res.locals.layer || 'all'; - const { mapConfigProvider } = res.locals; previewBackend.getImage(mapConfigProvider, format, width, height, zoom, center,(err, image, headers, stats) => { @@ -371,14 +383,7 @@ LayergroupController.prototype.bbox = function (previewBackend) { east: +req.params.east, south: +req.params.south }; - const format = req.params.format === 'jpg' ? 'jpeg' : 'png'; - - // We force always the tile to be generated using PNG because - // is the only format we support by now - res.locals.format = 'png'; - res.locals.layer = res.locals.layer || 'all'; - const { mapConfigProvider } = res.locals; previewBackend.getImage(mapConfigProvider, format, width, height, bounds, (err, image, headers, stats) => {