151 lines
5.2 KiB
JavaScript
151 lines
5.2 KiB
JavaScript
const layergroupToken = require('../middlewares/layergroup-token');
|
|
const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
|
|
const credentials = require('../middlewares/credentials');
|
|
const dbConnSetup = require('../middlewares/db-conn-setup');
|
|
const authorize = require('../middlewares/authorize');
|
|
const rateLimit = require('../middlewares/rate-limit');
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
|
const createMapStoreMapConfigProvider = require('../middlewares/map-store-map-config-provider');
|
|
const cacheControlHeader = require('../middlewares/cache-control-header');
|
|
const cacheChannelHeader = require('../middlewares/cache-channel-header');
|
|
const surrogateKeyHeader = require('../middlewares/surrogate-key-header');
|
|
const lastModifiedHeader = require('../middlewares/last-modified-header');
|
|
|
|
module.exports = class PreviewLayergroupController {
|
|
constructor (
|
|
previewBackend,
|
|
pgConnection,
|
|
mapStore,
|
|
userLimitsApi,
|
|
layergroupAffectedTablesCache,
|
|
authBackend,
|
|
surrogateKeysCache
|
|
) {
|
|
this.previewBackend = previewBackend;
|
|
this.pgConnection = pgConnection;
|
|
this.mapStore = mapStore;
|
|
this.userLimitsApi = userLimitsApi;
|
|
this.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
|
|
this.authBackend = authBackend;
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
}
|
|
|
|
register (mapRouter) {
|
|
const forcedFormat = 'png';
|
|
|
|
mapRouter.get(
|
|
`/static/center/:token/:z/:lat/:lng/:width/:height.:format`,
|
|
layergroupToken(),
|
|
credentials(),
|
|
authorize(this.authBackend),
|
|
dbConnSetup(this.pgConnection),
|
|
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC),
|
|
cleanUpQueryParams(['layer']),
|
|
createMapStoreMapConfigProvider(
|
|
this.mapStore,
|
|
this.userLimitsApi,
|
|
this.pgConnection,
|
|
this.layergroupAffectedTablesCache,
|
|
forcedFormat
|
|
),
|
|
getPreviewImageByCenter(this.previewBackend),
|
|
cacheControlHeader(),
|
|
cacheChannelHeader(),
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
|
lastModifiedHeader()
|
|
);
|
|
|
|
mapRouter.get(
|
|
`/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format`,
|
|
layergroupToken(),
|
|
credentials(),
|
|
authorize(this.authBackend),
|
|
dbConnSetup(this.pgConnection),
|
|
rateLimit(this.userLimitsApi, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC),
|
|
cleanUpQueryParams(['layer']),
|
|
createMapStoreMapConfigProvider(
|
|
this.mapStore,
|
|
this.userLimitsApi,
|
|
this.pgConnection,
|
|
this.layergroupAffectedTablesCache,
|
|
forcedFormat
|
|
),
|
|
getPreviewImageByBoundingBox(this.previewBackend),
|
|
cacheControlHeader(),
|
|
cacheChannelHeader(),
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
|
lastModifiedHeader()
|
|
);
|
|
}
|
|
};
|
|
|
|
function getPreviewImageByCenter (previewBackend) {
|
|
return function getPreviewImageByCenterMiddleware (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
|
|
};
|
|
|
|
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
|
const { mapConfigProvider: provider } = res.locals;
|
|
|
|
previewBackend.getImage(provider, format, width, height, zoom, center, (err, image, headers, stats = {}) => {
|
|
req.profiler.done(`render-${format}`);
|
|
req.profiler.add(stats);
|
|
|
|
if (err) {
|
|
err.label = 'STATIC_MAP';
|
|
return next(err);
|
|
}
|
|
|
|
if (headers) {
|
|
res.set(headers);
|
|
}
|
|
|
|
res.set('Content-Type', headers['Content-Type'] || `image/${format}`);
|
|
|
|
res.body = image;
|
|
|
|
next();
|
|
});
|
|
};
|
|
}
|
|
|
|
function getPreviewImageByBoundingBox (previewBackend) {
|
|
return function getPreviewImageByBoundingBoxMiddleware (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';
|
|
const { mapConfigProvider: provider } = res.locals;
|
|
|
|
previewBackend.getImage(provider, 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);
|
|
}
|
|
|
|
if (headers) {
|
|
res.set(headers);
|
|
}
|
|
|
|
res.set('Content-Type', headers['Content-Type'] || `image/${format}`);
|
|
|
|
res.body = image;
|
|
|
|
next();
|
|
});
|
|
};
|
|
}
|