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.prepareContext,
this.getNamedMapProvider(), this.getNamedMapProvider(),
this.prepareLayerFilterFromPreviewLayers(), this.prepareLayerFilterFromPreviewLayers(),
this.getStaticImageOptions(),
this.getImage(),
this.staticMap.bind(this) this.staticMap.bind(this)
); );
}; };
@ -166,47 +168,23 @@ NamedMapsController.prototype.staticMap = function(req, res, next) {
var cdbUser = res.locals.user; var cdbUser = res.locals.user;
var format = req.params.format === 'jpg' ? 'jpeg' : 'png'; const { namedMapProvider, image, headers, stats } = res.locals;
// 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 } = res.locals;
step( step(
function prepareImageOptions(err) { function incrementMapViews() {
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);
var next = this; var next = this;
namedMapProvider.getMapConfig(function(mapConfigErr, mapConfig) { namedMapProvider.getMapConfig(function(mapConfigErr, mapConfig) {
self.metadataBackend.incMapviewCount(cdbUser, mapConfig.obj().stat_tag, function(sErr) { 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); 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) { function handleImage(err, image, headers, stats) {
req.profiler.done('render-' + format); req.profiler.done('render-' + res.locals.format);
req.profiler.add(stats || {}); req.profiler.add(stats || {});
if (err) { if (err) {
@ -231,84 +209,138 @@ function numMapper(n) {
return +n; return +n;
} }
NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) { NamedMapsController.prototype.getStaticImageOptions = function () {
var self = this; return function getStaticImageOptionsMiddleware(req, res, next) {
var self = this;
if ([params.zoom, params.lon, params.lat].map(numMapper).every(Number.isFinite)) { const { user, namedMapProvider, zoom, lon, lat, bbox } = res.locals;
return callback(null, {
zoom: params.zoom,
center: {
lng: params.lon,
lat: params.lat
}
});
}
if (params.bbox) { if ([zoom, lon, lat].map(numMapper).every(Number.isFinite)) {
var bbox = params.bbox.split(',').map(numMapper); res.locals.imageOpts = {
if (bbox.length === 4 && bbox.every(Number.isFinite)) { zoom: zoom,
return callback(null, { center: {
bounds: { lng: lon,
west: bbox[0], lat: lat
south: bbox[1],
east: bbox[2],
north: bbox[3]
} }
}); };
return next();
} }
}
step( if (bbox) {
function getTemplate() { var _bbox = bbox.split(',').map(numMapper);
namedMapProvider.getTemplate(this); if (_bbox.length === 4 && _bbox.every(Number.isFinite)) {
}, res.locals.imageOpts = {
function handleTemplateView(err, template) { bounds: {
assert.ifError(err); west: _bbox[0],
south: _bbox[1],
if (template.view) { east: _bbox[2],
var zoomCenter = templateZoomCenter(template.view); north: _bbox[3]
if (zoomCenter) {
if (Number.isFinite(+params.zoom)) {
zoomCenter.zoom = +params.zoom;
} }
return zoomCenter; };
}
var bounds = templateBounds(template.view); return next();
if (bounds) {
return bounds;
}
} }
return false;
},
function estimateBoundsIfNoImageOpts(err, imageOpts) {
if (imageOpts) {
return imageOpts;
}
var next = this;
namedMapProvider.getAffectedTablesAndLastUpdatedTime(function(err, affectedTablesAndLastUpdate) {
if (err) {
return next(null);
}
var affectedTables = affectedTablesAndLastUpdate.tables || [];
if (affectedTables.length === 0) {
return next(null);
}
self.tablesExtentApi.getBounds(cdbUser, affectedTables, function(err, result) {
return next(null, result);
});
});
},
function returnCallback(err, imageOpts) {
return callback(err, imageOpts || DEFAULT_ZOOM_CENTER);
} }
);
step(
function getTemplate() {
namedMapProvider.getTemplate(this);
},
function handleTemplateView(err, template) {
assert.ifError(err);
if (template.view) {
var zoomCenter = templateZoomCenter(template.view);
if (zoomCenter) {
if (Number.isFinite(+zoom)) {
zoomCenter.zoom = +zoom;
}
return zoomCenter;
}
var bounds = templateBounds(template.view);
if (bounds) {
return bounds;
}
}
return false;
},
function estimateBoundsIfNoImageOpts(err, imageOpts) {
if (imageOpts) {
return imageOpts;
}
var _next = this;
namedMapProvider.getAffectedTablesAndLastUpdatedTime(function(err, affectedTablesAndLastUpdate) {
if (err) {
return _next(null);
}
var affectedTables = affectedTablesAndLastUpdate.tables || [];
if (affectedTables.length === 0) {
return _next(null);
}
self.tablesExtentApi.getBounds(user, affectedTables, function (err, result) {
return _next(null, result);
});
});
},
function returnCallback(err, imageOpts) {
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) { function templateZoomCenter(view) {