Middlewarify static-api (bbox/center) endpoints

This commit is contained in:
Daniel García Aubert
2018-03-05 19:26:26 +01:00
parent ca56df5cfe
commit 0f0cde1093
+62 -42
View File
@@ -89,7 +89,8 @@ LayergroupController.prototype.register = function(app) {
userMiddleware,
allowQueryParams(['layer']),
this.prepareContext,
this.center.bind(this)
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.center(this.previewBackend)
);
app.get(
@@ -98,7 +99,8 @@ LayergroupController.prototype.register = function(app) {
userMiddleware,
allowQueryParams(['layer']),
this.prepareContext,
this.bbox.bind(this)
this.getMapStoreMapConfigProvider(this.mapStore, this.userLimitsApi),
this.bbox(this.previewBackend)
);
// Undocumented/non-supported API endpoint methods.
@@ -325,56 +327,74 @@ LayergroupController.prototype.finalizeGetTileOrGrid = function(err, req, res, t
}
};
LayergroupController.prototype.bbox = function(req, res, next) {
this.staticMap(req, res, +req.params.width, +req.params.height, {
west: +req.params.west,
north: +req.params.north,
east: +req.params.east,
south: +req.params.south
}, null, next);
};
LayergroupController.prototype.center = function (previewBackend) {
return function centerMiddleware (req, res, next) {
const width = +req.params.width;
const height = +req.params.height;
const zoom = +req.params.z;
const center = {
lng: +req.params.lng,
lat: +req.params.lat
};
LayergroupController.prototype.center = function(req, res, next) {
this.staticMap(req, res, +req.params.width, +req.params.height, +req.params.z, {
lng: +req.params.lng,
lat: +req.params.lat
}, next);
};
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
LayergroupController.prototype.staticMap = function(req, res, width, height, zoom /* bounds */, center, next) {
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';
// 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';
var self = this;
const { mapConfigProvider } = res.locals;
step(
function getImage() {
if (center) {
self.previewBackend.getImage(
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
format, width, height, zoom, center, this);
} else {
self.previewBackend.getImage(
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
format, width, height, zoom /* bounds */, this);
}
},
function handleImage(err, image, headers, stats) {
previewBackend.getImage(mapConfigProvider, format, width, height, zoom, center, (err, image, headers, stats) => {
req.profiler.done('render-' + format);
req.profiler.add(stats || {});
if (err) {
err.label = 'STATIC_MAP';
next(err);
} else {
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
self.sendResponse(req, res, image, 200);
return next(err);
}
}
);
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
this.sendResponse(req, res, image, 200);
});
}.bind(this);
};
LayergroupController.prototype.bbox = function (previewBackend) {
return function bboxMiddleware (req, res, next) {
const width = +req.params.width;
const height = +req.params.height;
const bounds = {
west: +req.params.west,
north: +req.params.north,
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) => {
req.profiler.done('render-' + format);
req.profiler.add(stats || {});
if (err) {
err.label = 'STATIC_MAP';
return next(err);
}
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
this.sendResponse(req, res, image, 200);
});
}.bind(this);
};
LayergroupController.prototype.sendResponse = function(req, res, body, status, headers) {