Move getStaticImageOptions and getImage to a middlewares

This commit is contained in:
Daniel García Aubert 2017-12-30 15:21:20 +01:00
parent 500cbb959f
commit 731fe4c00f

View File

@ -40,6 +40,8 @@ NamedMapsController.prototype.register = function(app) {
this.prepareContext,
this.getNamedMapProvider(),
this.prepareLayerFilterFromPreviewLayers(),
this.getStaticImageOptions(),
this.getImage(),
this.staticMap.bind(this)
);
};
@ -166,47 +168,23 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
var cdbUser = res.locals.user;
var 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 { namedMapProvider, image, headers, stats } = res.locals;
const { namedMapProvider } = res.locals;
step(
function prepareImageOptions(err) {
assert.ifError(err);
self.getStaticImageOptions(cdbUser, res.locals, namedMapProvider, this);
},
function getImage(err, imageOpts) {
assert.ifError(err);
var width = +req.params.width;
var height = +req.params.height;
if (!_.isUndefined(imageOpts.zoom) && imageOpts.center) {
self.previewBackend.getImage(
namedMapProvider, format, width, height, imageOpts.zoom, imageOpts.center, this);
} else {
self.previewBackend.getImage(
namedMapProvider, format, width, height, imageOpts.bounds, this);
}
},
function incrementMapViews(err, image, headers, stats) {
assert.ifError(err);
function incrementMapViews() {
var next = this;
namedMapProvider.getMapConfig(function(mapConfigErr, mapConfig) {
self.metadataBackend.incMapviewCount(cdbUser, mapConfig.obj().stat_tag, function(sErr) {
if (err) {
if (sErr) {
global.logger.log("ERROR: failed to increment mapview count for user '%s': %s", cdbUser, sErr);
}
next(err, image, headers, stats);
next(null, image, headers, stats);
});
});
},
function handleImage(err, image, headers, stats) {
req.profiler.done('render-' + format);
req.profiler.done('render-' + res.locals.format);
req.profiler.add(stats || {});
if (err) {
@ -231,30 +209,37 @@ function numMapper(n) {
return +n;
}
NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) {
NamedMapsController.prototype.getStaticImageOptions = function () {
return function getStaticImageOptionsMiddleware(req, res, next) {
var self = this;
if ([params.zoom, params.lon, params.lat].map(numMapper).every(Number.isFinite)) {
return callback(null, {
zoom: params.zoom,
const { user, namedMapProvider, zoom, lon, lat, bbox } = res.locals;
if ([zoom, lon, lat].map(numMapper).every(Number.isFinite)) {
res.locals.imageOpts = {
zoom: zoom,
center: {
lng: params.lon,
lat: params.lat
lng: lon,
lat: lat
}
});
};
return next();
}
if (params.bbox) {
var bbox = params.bbox.split(',').map(numMapper);
if (bbox.length === 4 && bbox.every(Number.isFinite)) {
return callback(null, {
if (bbox) {
var _bbox = bbox.split(',').map(numMapper);
if (_bbox.length === 4 && _bbox.every(Number.isFinite)) {
res.locals.imageOpts = {
bounds: {
west: bbox[0],
south: bbox[1],
east: bbox[2],
north: bbox[3]
west: _bbox[0],
south: _bbox[1],
east: _bbox[2],
north: _bbox[3]
}
});
};
return next();
}
}
@ -268,8 +253,8 @@ NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params,
if (template.view) {
var zoomCenter = templateZoomCenter(template.view);
if (zoomCenter) {
if (Number.isFinite(+params.zoom)) {
zoomCenter.zoom = +params.zoom;
if (Number.isFinite(+zoom)) {
zoomCenter.zoom = +zoom;
}
return zoomCenter;
}
@ -287,28 +272,75 @@ NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params,
return imageOpts;
}
var next = this;
var _next = this;
namedMapProvider.getAffectedTablesAndLastUpdatedTime(function(err, affectedTablesAndLastUpdate) {
if (err) {
return next(null);
return _next(null);
}
var affectedTables = affectedTablesAndLastUpdate.tables || [];
if (affectedTables.length === 0) {
return next(null);
return _next(null);
}
self.tablesExtentApi.getBounds(cdbUser, affectedTables, function(err, result) {
return next(null, result);
self.tablesExtentApi.getBounds(user, affectedTables, function (err, result) {
return _next(null, result);
});
});
},
function returnCallback(err, imageOpts) {
return callback(err, imageOpts || DEFAULT_ZOOM_CENTER);
res.locals.imageOpts = imageOpts || DEFAULT_ZOOM_CENTER;
return next();
}
);
}.bind(this);
};
NamedMapsController.prototype.getImage = function () {
return function getImageMiddleware (req, res, next) {
const { imageOpts, namedMapProvider } = res.locals;
const { zoom, center, bounds } = imageOpts;
let { width, height } = req.params;
width = +width;
height = +height;
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';
if (!_.isUndefined(zoom) && center) {
return this.previewBackend.getImage(namedMapProvider, format, width, height, zoom, center,
(err, image, headers, stats) => {
if (err) {
return next(err);
}
res.locals.image = image;
res.locals.headers = headers;
res.locals.stats = stats;
next();
});
}
this.previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
if (err) {
return next(err);
}
res.locals.image = image;
res.locals.headers = headers;
res.locals.stats = stats;
next();
});
}.bind(this);
};
function templateZoomCenter(view) {