2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
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');
|
2018-04-09 22:18:30 +08:00
|
|
|
const namedMapProvider = require('../middlewares/named-map-provider');
|
2018-04-07 00:20:33 +08:00
|
|
|
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');
|
2018-05-11 23:42:28 +08:00
|
|
|
const checkStaticImageFormat = require('../middlewares/check-static-image-format');
|
2018-04-07 00:20:33 +08:00
|
|
|
const rateLimit = require('../middlewares/rate-limit');
|
2018-02-27 23:52:27 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
2015-09-16 22:18:26 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
const DEFAULT_ZOOM_CENTER = {
|
|
|
|
zoom: 1,
|
|
|
|
center: {
|
|
|
|
lng: 0,
|
|
|
|
lat: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function numMapper(n) {
|
|
|
|
return +n;
|
|
|
|
}
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2018-05-11 21:34:10 +08:00
|
|
|
module.exports = class PreviewTemplateController {
|
|
|
|
constructor (
|
|
|
|
namedMapProviderCache,
|
|
|
|
previewBackend,
|
|
|
|
surrogateKeysCache,
|
|
|
|
tablesExtentBackend,
|
|
|
|
metadataBackend,
|
|
|
|
pgConnection,
|
|
|
|
authBackend,
|
|
|
|
userLimitsBackend
|
|
|
|
) {
|
|
|
|
this.namedMapProviderCache = namedMapProviderCache;
|
|
|
|
this.previewBackend = previewBackend;
|
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
this.tablesExtentBackend = tablesExtentBackend;
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.authBackend = authBackend;
|
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
|
|
|
}
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (mapRouter) {
|
2018-05-11 21:34:10 +08:00
|
|
|
mapRouter.get('/static/named/:template_id/:width/:height.:format', this.middlewares());
|
|
|
|
}
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2018-05-11 21:34:10 +08:00
|
|
|
middlewares () {
|
|
|
|
return [
|
|
|
|
credentials(),
|
|
|
|
authorize(this.authBackend),
|
|
|
|
dbConnSetup(this.pgConnection),
|
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC_NAMED),
|
|
|
|
cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
|
2018-05-16 20:40:59 +08:00
|
|
|
checkStaticImageFormat(),
|
2018-05-11 21:34:10 +08:00
|
|
|
namedMapProvider({
|
|
|
|
namedMapProviderCache: this.namedMapProviderCache,
|
|
|
|
label: 'STATIC_VIZ_MAP', forcedFormat: 'png'
|
|
|
|
}),
|
|
|
|
getTemplate({ label: 'STATIC_VIZ_MAP' }),
|
|
|
|
prepareLayerFilterFromPreviewLayers({
|
|
|
|
namedMapProviderCache: this.namedMapProviderCache,
|
|
|
|
label: 'STATIC_VIZ_MAP'
|
|
|
|
}),
|
|
|
|
getStaticImageOptions({ tablesExtentBackend: this.tablesExtentBackend }),
|
|
|
|
getImage({ previewBackend: this.previewBackend, label: 'STATIC_VIZ_MAP' }),
|
|
|
|
setContentTypeHeader(),
|
|
|
|
incrementMapViews({ metadataBackend: this.metadataBackend }),
|
|
|
|
cacheControlHeader(),
|
|
|
|
cacheChannelHeader(),
|
|
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
|
|
|
lastModifiedHeader()
|
|
|
|
];
|
|
|
|
}
|
2015-07-09 19:37:00 +08:00
|
|
|
};
|
|
|
|
|
2018-03-14 20:02:49 +08:00
|
|
|
function getTemplate ({ label }) {
|
2018-01-01 23:54:35 +08:00
|
|
|
return function getTemplateMiddleware (req, res, next) {
|
2018-03-21 21:11:54 +08:00
|
|
|
const { mapConfigProvider } = res.locals;
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
mapConfigProvider.getTemplate((err, template) => {
|
2017-12-30 21:13:23 +08:00
|
|
|
if (err) {
|
2017-12-30 23:08:46 +08:00
|
|
|
err.label = label;
|
2017-12-30 21:13:23 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
res.locals.template = template;
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
2018-03-14 20:10:24 +08:00
|
|
|
}
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-03-14 20:05:05 +08:00
|
|
|
function prepareLayerFilterFromPreviewLayers ({ namedMapProviderCache, label }) {
|
2018-01-01 23:54:35 +08:00
|
|
|
return function prepareLayerFilterFromPreviewLayersMiddleware (req, res, next) {
|
2018-03-23 18:23:19 +08:00
|
|
|
const { template } = res.locals;
|
2018-01-01 23:54:35 +08:00
|
|
|
const { config, auth_token } = req.query;
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
if (!template || !template.view || !template.view.preview_layers) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
var previewLayers = template.view.preview_layers;
|
|
|
|
var layerVisibilityFilter = [];
|
|
|
|
|
2018-01-09 18:20:20 +08:00
|
|
|
template.layergroup.layers.forEach((layer, index) => {
|
2018-01-01 23:54:35 +08:00
|
|
|
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
|
|
|
|
layerVisibilityFilter.push(''+index);
|
2017-12-30 21:13:23 +08:00
|
|
|
}
|
2018-01-01 23:54:35 +08:00
|
|
|
});
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
if (!layerVisibilityFilter.length) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-03-23 18:23:19 +08:00
|
|
|
const { user, token, cache_buster, api_key } = res.locals;
|
|
|
|
const { dbuser, dbname, dbpassword, dbhost, dbport } = res.locals;
|
|
|
|
const { template_id, format } = req.params;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
user, token, cache_buster, api_key,
|
|
|
|
dbuser, dbname, dbpassword, dbhost, dbport,
|
|
|
|
template_id, format
|
|
|
|
};
|
2018-01-16 00:08:54 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
// overwrites 'all' default filter
|
2018-01-16 00:08:54 +08:00
|
|
|
params.layer = layerVisibilityFilter.join(',');
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
// recreates the provider
|
2018-03-14 20:05:05 +08:00
|
|
|
namedMapProviderCache.get(user, template_id, config, auth_token, params, (err, provider) => {
|
2018-01-01 23:54:35 +08:00
|
|
|
if (err) {
|
|
|
|
err.label = label;
|
|
|
|
return next(err);
|
|
|
|
}
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
res.locals.mapConfigProvider = provider;
|
2018-01-01 23:54:35 +08:00
|
|
|
|
|
|
|
next();
|
2017-12-30 21:13:23 +08:00
|
|
|
});
|
2018-03-14 20:05:05 +08:00
|
|
|
};
|
2018-03-14 20:10:24 +08:00
|
|
|
}
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-04-10 15:40:09 +08:00
|
|
|
function getStaticImageOptions ({ tablesExtentBackend }) {
|
2017-12-30 22:21:20 +08:00
|
|
|
return function getStaticImageOptionsMiddleware(req, res, next) {
|
2018-03-21 21:11:54 +08:00
|
|
|
const { user, mapConfigProvider, template } = res.locals;
|
2018-03-23 23:38:55 +08:00
|
|
|
const { zoom, lon, lat, bbox } = req.query;
|
|
|
|
const params = { zoom, lon, lat, bbox };
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-03-23 23:38:55 +08:00
|
|
|
const imageOpts = getImageOptions(params, template);
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-01-02 01:06:56 +08:00
|
|
|
if (imageOpts) {
|
|
|
|
res.locals.imageOpts = imageOpts;
|
|
|
|
return next();
|
2018-01-01 23:54:35 +08:00
|
|
|
}
|
2015-09-23 19:04:46 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
res.locals.imageOpts = DEFAULT_ZOOM_CENTER;
|
2017-12-31 01:02:48 +08:00
|
|
|
|
2018-04-06 19:00:12 +08:00
|
|
|
mapConfigProvider.createAffectedTables((err, affectedTables) => {
|
2018-01-01 23:54:35 +08:00
|
|
|
if (err) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
var tables = affectedTables.tables || [];
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
if (tables.length === 0) {
|
2018-01-01 23:54:35 +08:00
|
|
|
return next();
|
|
|
|
}
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-04-10 15:40:09 +08:00
|
|
|
tablesExtentBackend.getBounds(user, tables, (err, bounds) => {
|
2018-01-01 23:54:35 +08:00
|
|
|
if (err) {
|
2017-12-31 01:02:48 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
res.locals.imageOpts = bounds;
|
2018-01-02 01:06:56 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
return next();
|
2017-12-31 01:02:48 +08:00
|
|
|
});
|
|
|
|
});
|
2018-03-14 20:10:24 +08:00
|
|
|
};
|
|
|
|
}
|
2015-09-23 19:04:46 +08:00
|
|
|
|
2018-01-02 01:06:56 +08:00
|
|
|
function getImageOptions (params, template) {
|
|
|
|
const { zoom, lon, lat, bbox } = params;
|
|
|
|
|
|
|
|
let imageOpts = getImageOptionsFromCoordinates(zoom, lon, lat);
|
|
|
|
if (imageOpts) {
|
|
|
|
return imageOpts;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageOpts = getImageOptionsFromBoundingBox(bbox);
|
|
|
|
if (imageOpts) {
|
|
|
|
return imageOpts;
|
|
|
|
}
|
|
|
|
|
|
|
|
imageOpts = getImageOptionsFromTemplate(template, zoom);
|
|
|
|
if (imageOpts) {
|
|
|
|
return imageOpts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getImageOptionsFromCoordinates (zoom, lon, lat) {
|
|
|
|
if ([zoom, lon, lat].map(numMapper).every(Number.isFinite)) {
|
|
|
|
return {
|
|
|
|
zoom: zoom,
|
|
|
|
center: {
|
|
|
|
lng: lon,
|
|
|
|
lat: lat
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getImageOptionsFromTemplate (template, zoom) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 17:56:45 +08:00
|
|
|
function getImageOptionsFromBoundingBox (bbox = '') {
|
|
|
|
var _bbox = bbox.split(',').map(numMapper);
|
|
|
|
|
|
|
|
if (_bbox.length === 4 && _bbox.every(Number.isFinite)) {
|
|
|
|
return {
|
2019-06-17 16:43:30 +08:00
|
|
|
bbox: {
|
2018-01-02 17:56:45 +08:00
|
|
|
west: _bbox[0],
|
|
|
|
south: _bbox[1],
|
|
|
|
east: _bbox[2],
|
|
|
|
north: _bbox[3]
|
|
|
|
}
|
|
|
|
};
|
2018-01-02 01:06:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:15:38 +08:00
|
|
|
function getImage({ previewBackend, label }) {
|
2017-12-30 22:21:20 +08:00
|
|
|
return function getImageMiddleware (req, res, next) {
|
2018-03-21 21:11:54 +08:00
|
|
|
const { imageOpts, mapConfigProvider } = res.locals;
|
2019-06-17 16:43:30 +08:00
|
|
|
const { zoom, center, bbox } = imageOpts;
|
2017-12-30 22:21:20 +08:00
|
|
|
|
|
|
|
let { width, height } = req.params;
|
2015-09-23 19:04:46 +08:00
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
width = +width;
|
|
|
|
height = +height;
|
2015-09-23 19:04:46 +08:00
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
2018-05-11 23:47:47 +08:00
|
|
|
|
2017-12-31 01:18:37 +08:00
|
|
|
if (zoom !== undefined && center) {
|
2019-06-17 16:43:30 +08:00
|
|
|
const options = { mapConfigProvider, format, width, height, zoom, center };
|
|
|
|
|
|
|
|
return previewBackend.getImage(options, (err, image, stats) => {
|
2018-03-17 00:13:48 +08:00
|
|
|
req.profiler.add(stats);
|
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
if (err) {
|
2017-12-30 23:08:46 +08:00
|
|
|
err.label = label;
|
2017-12-30 22:21:20 +08:00
|
|
|
return next(err);
|
2015-09-23 19:04:46 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-17 00:13:48 +08:00
|
|
|
res.body = image;
|
2015-09-23 19:04:46 +08:00
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
next();
|
|
|
|
});
|
2015-07-08 21:50:59 +08:00
|
|
|
}
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2019-06-17 16:43:30 +08:00
|
|
|
const options = { mapConfigProvider, format, width, height, bbox };
|
|
|
|
|
|
|
|
previewBackend.getImage(options, (err, image, stats) => {
|
2018-03-17 00:13:48 +08:00
|
|
|
req.profiler.add(stats);
|
2018-03-20 02:48:14 +08:00
|
|
|
req.profiler.done('render-' + format);
|
2018-03-17 00:13:48 +08:00
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
if (err) {
|
2017-12-30 23:08:46 +08:00
|
|
|
err.label = label;
|
2017-12-30 22:21:20 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-17 00:13:48 +08:00
|
|
|
res.body = image;
|
2017-12-30 22:21:20 +08:00
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
2018-03-14 20:15:38 +08:00
|
|
|
};
|
|
|
|
}
|
2015-07-08 21:50:59 +08:00
|
|
|
|
2018-03-22 18:38:33 +08:00
|
|
|
function setContentTypeHeader () {
|
|
|
|
return function setContentTypeHeaderMiddleware(req, res, next) {
|
2019-06-10 19:16:05 +08:00
|
|
|
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
|
|
|
|
|
|
|
res.set('Content-Type', `image/${format}`);
|
2018-03-22 18:38:33 +08:00
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-09 18:17:07 +08:00
|
|
|
function incrementMapViewsError (ctx) {
|
|
|
|
return `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;
|
|
|
|
}
|
2017-12-30 23:04:24 +08:00
|
|
|
|
2018-03-14 20:18:37 +08:00
|
|
|
function incrementMapViews ({ metadataBackend }) {
|
2017-12-30 23:04:24 +08:00
|
|
|
return function incrementMapViewsMiddleware(req, res, next) {
|
2018-03-21 21:11:54 +08:00
|
|
|
const { user, mapConfigProvider } = res.locals;
|
2017-12-30 23:04:24 +08:00
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
mapConfigProvider.getMapConfig((err, mapConfig) => {
|
2017-12-30 23:04:24 +08:00
|
|
|
if (err) {
|
|
|
|
global.logger.log(incrementMapViewsError({ user, err }));
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const statTag = mapConfig.obj().stat_tag;
|
|
|
|
|
2018-03-14 20:18:37 +08:00
|
|
|
metadataBackend.incMapviewCount(user, statTag, (err) => {
|
2017-12-30 23:04:24 +08:00
|
|
|
if (err) {
|
|
|
|
global.logger.log(incrementMapViewsError({ user, err }));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
2018-03-14 20:18:37 +08:00
|
|
|
};
|
|
|
|
}
|
2017-12-30 23:04:24 +08:00
|
|
|
|
2015-07-08 21:50:59 +08:00
|
|
|
function templateZoomCenter(view) {
|
2017-12-31 01:18:37 +08:00
|
|
|
if (view.zoom !== undefined && view.center) {
|
2015-07-08 21:50:59 +08:00
|
|
|
return {
|
|
|
|
zoom: view.zoom,
|
|
|
|
center: view.center
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function templateBounds(view) {
|
|
|
|
if (view.bounds) {
|
2018-01-09 18:20:20 +08:00
|
|
|
var hasAllBounds = ['west', 'south', 'east', 'north'].every(prop => Number.isFinite(view.bounds[prop]));
|
2017-12-31 01:18:37 +08:00
|
|
|
|
2015-07-08 21:50:59 +08:00
|
|
|
if (hasAllBounds) {
|
|
|
|
return {
|
2019-06-17 16:43:30 +08:00
|
|
|
bbox: {
|
2015-07-08 21:50:59 +08:00
|
|
|
west: view.bounds.west,
|
|
|
|
south: view.bounds.south,
|
|
|
|
east: view.bounds.east,
|
|
|
|
north: view.bounds.north
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|