Extract getImage middleware from controller's context

This commit is contained in:
Daniel García Aubert
2018-03-14 13:15:38 +01:00
parent c3ddb933bb
commit 3b3e0c0acd

View File

@@ -65,6 +65,7 @@ NamedMapsController.prototype.register = function(app) {
);
const staticOptions = {
previewBackend: this.previewBackend,
namedMapProviderCache: this.namedMapProviderCache,
forcedFormat: 'png',
label: 'STATIC_VIZ_MAP'
@@ -81,7 +82,7 @@ NamedMapsController.prototype.register = function(app) {
getTemplate(staticOptions),
prepareLayerFilterFromPreviewLayers(staticOptions),
getStaticImageOptions({ tablesExtentApi: this.tablesExtentApi }),
this.getImage(staticOptions),
getImage(staticOptions),
this.incrementMapViews(),
this.setSurrogateKey(),
this.setCacheChannelHeader(),
@@ -318,7 +319,7 @@ function getImageOptionsFromBoundingBox (bbox = '') {
}
}
NamedMapsController.prototype.getImage = function ({ label }) {
function getImage({ previewBackend, label }) {
return function getImageMiddleware (req, res, next) {
const { imageOpts, namedMapProvider } = res.locals;
const { zoom, center, bounds } = imageOpts;
@@ -331,7 +332,7 @@ NamedMapsController.prototype.getImage = function ({ label }) {
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
if (zoom !== undefined && center) {
return this.previewBackend.getImage(namedMapProvider, format, width, height, zoom, center,
return previewBackend.getImage(namedMapProvider, format, width, height, zoom, center,
(err, image, headers, stats) => {
if (err) {
err.label = label;
@@ -346,7 +347,7 @@ NamedMapsController.prototype.getImage = function ({ label }) {
});
}
this.previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
if (err) {
err.label = label;
return next(err);
@@ -358,8 +359,8 @@ NamedMapsController.prototype.getImage = function ({ label }) {
next();
});
}.bind(this);
};
};
}
function incrementMapViewsError (ctx) {
return `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;