2018-01-01 23:54:35 +08:00
|
|
|
const NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
|
|
|
|
const cors = require('../middleware/cors');
|
|
|
|
const userMiddleware = require('../middleware/user');
|
|
|
|
const allowQueryParams = require('../middleware/allow-query-params');
|
|
|
|
const vectorError = require('../middleware/vector-error');
|
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-01-16 01:09:54 +08:00
|
|
|
function getRequestParams(locals) {
|
|
|
|
const params = Object.assign({}, locals);
|
|
|
|
|
|
|
|
delete params.template;
|
|
|
|
delete params.affectedTablesAndLastUpdate;
|
|
|
|
delete params.namedMapProvider;
|
|
|
|
delete params.allowedQueryParams;
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:40:27 +08:00
|
|
|
function NamedMapsController(prepareContext, namedMapProviderCache, tileBackend, previewBackend,
|
2016-09-01 22:31:30 +08:00
|
|
|
surrogateKeysCache, tablesExtentApi, metadataBackend) {
|
2015-07-15 02:53:06 +08:00
|
|
|
this.namedMapProviderCache = namedMapProviderCache;
|
2015-07-09 19:37:00 +08:00
|
|
|
this.tileBackend = tileBackend;
|
|
|
|
this.previewBackend = previewBackend;
|
2015-01-23 23:37:38 +08:00
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
2015-07-08 21:50:59 +08:00
|
|
|
this.tablesExtentApi = tablesExtentApi;
|
2015-09-29 18:21:11 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
2017-09-26 01:40:27 +08:00
|
|
|
this.prepareContext = prepareContext;
|
2015-01-20 23:56:06 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
module.exports = NamedMapsController;
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.register = function(app) {
|
2017-09-22 06:48:44 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_templated + '/:template_id/:layer/:z/:x/:y.(:format)',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2018-01-17 00:55:09 +08:00
|
|
|
this.getNamedMapProvider('NAMED_MAP_TILE'),
|
2018-01-01 23:21:22 +08:00
|
|
|
this.getAffectedTables(),
|
2018-01-17 00:55:09 +08:00
|
|
|
this.getTile('NAMED_MAP_TILE'),
|
2018-01-01 23:21:22 +08:00
|
|
|
this.setSurrogateKey(),
|
|
|
|
this.setCacheChannelHeader(),
|
|
|
|
this.setLastModifiedHeader(),
|
|
|
|
this.setCacheControlHeader(),
|
|
|
|
this.setContentTypeHeader(),
|
2017-12-31 00:18:12 +08:00
|
|
|
this.respond(),
|
2017-12-12 20:20:22 +08:00
|
|
|
vectorError()
|
2017-09-22 06:48:44 +08:00
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-09-22 06:48:44 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/static/named/:template_id/:width/:height.:format',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-08-29 21:05:03 +08:00
|
|
|
allowQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-12-30 23:08:46 +08:00
|
|
|
this.getNamedMapProvider('STATIC_VIZ_MAP'),
|
2018-01-01 23:21:22 +08:00
|
|
|
this.getAffectedTables(),
|
2018-01-01 23:54:35 +08:00
|
|
|
this.getTemplate('STATIC_VIZ_MAP'),
|
2017-12-30 23:08:46 +08:00
|
|
|
this.prepareLayerFilterFromPreviewLayers('STATIC_VIZ_MAP'),
|
2017-12-30 22:21:20 +08:00
|
|
|
this.getStaticImageOptions(),
|
2017-12-30 23:08:46 +08:00
|
|
|
this.getImage('STATIC_VIZ_MAP'),
|
2017-12-30 23:04:24 +08:00
|
|
|
this.incrementMapViews(),
|
2018-01-01 23:21:22 +08:00
|
|
|
this.setSurrogateKey(),
|
|
|
|
this.setCacheChannelHeader(),
|
|
|
|
this.setLastModifiedHeader(),
|
|
|
|
this.setCacheControlHeader(),
|
|
|
|
this.setContentTypeHeader(),
|
2017-12-31 00:18:12 +08:00
|
|
|
this.respond()
|
2017-09-22 06:48:44 +08:00
|
|
|
);
|
2015-07-09 19:37:00 +08:00
|
|
|
};
|
|
|
|
|
2017-12-30 23:08:46 +08:00
|
|
|
NamedMapsController.prototype.getNamedMapProvider = function (label) {
|
2017-12-30 02:31:02 +08:00
|
|
|
return function getNamedMapProviderMiddleware (req, res, next) {
|
|
|
|
const { user } = res.locals;
|
|
|
|
const { config, auth_token } = req.query;
|
|
|
|
const { template_id } = req.params;
|
2018-01-17 00:57:22 +08:00
|
|
|
|
|
|
|
// 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';
|
|
|
|
|
2018-01-16 01:09:54 +08:00
|
|
|
const params = getRequestParams(res.locals);
|
2017-12-30 02:31:02 +08:00
|
|
|
|
2018-01-16 00:08:54 +08:00
|
|
|
this.namedMapProviderCache.get(user, template_id, config, auth_token, params, (err, namedMapProvider) => {
|
2017-12-30 02:31:02 +08:00
|
|
|
if (err) {
|
2017-12-30 23:08:46 +08:00
|
|
|
err.label = label;
|
2017-12-30 02:31:02 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.namedMapProvider = namedMapProvider;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
NamedMapsController.prototype.getAffectedTables = function () {
|
|
|
|
return function getAffectedTables (req, res, next) {
|
|
|
|
const { namedMapProvider } = res.locals;
|
|
|
|
|
|
|
|
namedMapProvider.getAffectedTablesAndLastUpdatedTime((err, affectedTablesAndLastUpdate) => {
|
|
|
|
req.profiler.done('affectedTables');
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.affectedTablesAndLastUpdate = affectedTablesAndLastUpdate;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
NamedMapsController.prototype.getTemplate = function (label) {
|
|
|
|
return function getTemplateMiddleware (req, res, next) {
|
|
|
|
const { namedMapProvider } = res.locals;
|
2017-12-30 21:13:23 +08:00
|
|
|
|
|
|
|
namedMapProvider.getTemplate((err, template) => {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2017-12-30 21:13:23 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
NamedMapsController.prototype.prepareLayerFilterFromPreviewLayers = function (label) {
|
|
|
|
return function prepareLayerFilterFromPreviewLayersMiddleware (req, res, next) {
|
|
|
|
const { user, template } = res.locals;
|
|
|
|
const { template_id } = req.params;
|
|
|
|
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-01-16 01:09:54 +08:00
|
|
|
const params = getRequestParams(res.locals);
|
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-01-16 00:08:54 +08:00
|
|
|
this.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-01-01 23:54:35 +08:00
|
|
|
res.locals.namedMapProvider = provider;
|
|
|
|
|
|
|
|
next();
|
2017-12-30 21:13:23 +08:00
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
2018-01-17 00:55:09 +08:00
|
|
|
NamedMapsController.prototype.getTile = function (label) {
|
2017-12-31 00:18:12 +08:00
|
|
|
return function getTileMiddleware (req, res, next) {
|
|
|
|
const { namedMapProvider } = res.locals;
|
2015-07-15 02:10:55 +08:00
|
|
|
|
2017-12-31 00:18:12 +08:00
|
|
|
this.tileBackend.getTile(namedMapProvider, req.params, (err, tile, headers, stats) => {
|
|
|
|
req.profiler.add(stats);
|
2015-07-15 02:10:55 +08:00
|
|
|
|
|
|
|
if (err) {
|
2018-01-17 00:55:09 +08:00
|
|
|
err.label = label;
|
2017-12-31 00:18:12 +08:00
|
|
|
return next(err);
|
2015-07-15 02:10:55 +08:00
|
|
|
}
|
|
|
|
|
2017-12-31 00:18:12 +08:00
|
|
|
res.locals.body = tile;
|
|
|
|
res.locals.headers = headers;
|
|
|
|
res.locals.stats = stats;
|
2017-12-30 02:33:49 +08:00
|
|
|
|
2017-12-31 00:18:12 +08:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}.bind(this);
|
2015-06-29 22:39:35 +08:00
|
|
|
};
|
|
|
|
|
2017-12-30 22:21:20 +08:00
|
|
|
NamedMapsController.prototype.getStaticImageOptions = function () {
|
|
|
|
return function getStaticImageOptionsMiddleware(req, res, next) {
|
2018-01-02 01:06:56 +08:00
|
|
|
const { user, namedMapProvider, template } = res.locals;
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-01-02 01:06:56 +08:00
|
|
|
const imageOpts = getImageOptions(res.locals, 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-01-01 23:54:35 +08:00
|
|
|
namedMapProvider.getAffectedTablesAndLastUpdatedTime((err, affectedTablesAndLastUpdate) => {
|
|
|
|
if (err) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
var affectedTables = affectedTablesAndLastUpdate.tables || [];
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
if (affectedTables.length === 0) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-12-30 22:21:20 +08:00
|
|
|
|
2018-01-01 23:54:35 +08:00
|
|
|
this.tablesExtentApi.getBounds(user, affectedTables, (err, bounds) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
2017-12-30 22:21:20 +08:00
|
|
|
}.bind(this);
|
|
|
|
};
|
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 {
|
|
|
|
bounds: {
|
|
|
|
west: _bbox[0],
|
|
|
|
south: _bbox[1],
|
|
|
|
east: _bbox[2],
|
|
|
|
north: _bbox[3]
|
|
|
|
}
|
|
|
|
};
|
2018-01-02 01:06:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-30 23:08:46 +08:00
|
|
|
NamedMapsController.prototype.getImage = function (label) {
|
2017-12-30 22:21:20 +08:00
|
|
|
return function getImageMiddleware (req, res, next) {
|
|
|
|
const { imageOpts, namedMapProvider } = res.locals;
|
|
|
|
const { zoom, center, bounds } = imageOpts;
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
2017-12-31 01:18:37 +08:00
|
|
|
if (zoom !== undefined && center) {
|
2017-12-30 22:21:20 +08:00
|
|
|
return this.previewBackend.getImage(namedMapProvider, format, width, height, zoom, center,
|
|
|
|
(err, image, headers, stats) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-31 00:18:12 +08:00
|
|
|
res.locals.body = image;
|
2017-12-30 22:21:20 +08:00
|
|
|
res.locals.headers = headers;
|
|
|
|
res.locals.stats = stats;
|
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
|
|
|
|
|
|
|
this.previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
|
|
|
|
if (err) {
|
2017-12-30 23:08:46 +08:00
|
|
|
err.label = label;
|
2017-12-30 22:21:20 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2017-12-31 00:18:12 +08:00
|
|
|
res.locals.body = image;
|
2017-12-30 22:21:20 +08:00
|
|
|
res.locals.headers = headers;
|
|
|
|
res.locals.stats = stats;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}.bind(this);
|
2015-09-23 19:04:46 +08:00
|
|
|
};
|
2015-07-08 21:50:59 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
NamedMapsController.prototype.incrementMapViews = function () {
|
|
|
|
return function incrementMapViewsMiddleware(req, res, next) {
|
|
|
|
const { user, namedMapProvider } = res.locals;
|
|
|
|
|
|
|
|
namedMapProvider.getMapConfig((err, mapConfig) => {
|
|
|
|
if (err) {
|
|
|
|
global.logger.log(incrementMapViewsError({ user, err }));
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const statTag = mapConfig.obj().stat_tag;
|
|
|
|
|
|
|
|
this.metadataBackend.incMapviewCount(user, statTag, (err) => {
|
|
|
|
if (err) {
|
|
|
|
global.logger.log(incrementMapViewsError({ user, err }));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
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 {
|
|
|
|
bounds: {
|
|
|
|
west: view.bounds.west,
|
|
|
|
south: view.bounds.south,
|
|
|
|
east: view.bounds.east,
|
|
|
|
north: view.bounds.north
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
NamedMapsController.prototype.setCacheChannelHeader = function () {
|
|
|
|
return function setCacheChannelHeaderMiddleware (req, res, next) {
|
|
|
|
const { affectedTablesAndLastUpdate } = res.locals;
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
|
|
|
|
res.set('X-Cache-Channel', affectedTablesAndLastUpdate.getCacheChannel());
|
|
|
|
}
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
NamedMapsController.prototype.setSurrogateKey = function () {
|
|
|
|
return function setSurrogateKeyMiddleware(req, res, next) {
|
|
|
|
const { user, namedMapProvider, affectedTablesAndLastUpdate } = res.locals;
|
|
|
|
|
|
|
|
this.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, namedMapProvider.getTemplateName()));
|
|
|
|
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
|
|
|
|
if (affectedTablesAndLastUpdate.tables.length > 0) {
|
|
|
|
this.surrogateKeysCache.tag(res, affectedTablesAndLastUpdate);
|
2017-12-31 00:18:12 +08:00
|
|
|
}
|
2018-01-01 23:21:22 +08:00
|
|
|
}
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
next();
|
|
|
|
}.bind(this);
|
|
|
|
};
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
NamedMapsController.prototype.setLastModifiedHeader = function () {
|
|
|
|
return function setLastModifiedHeaderMiddleware(req, res, next) {
|
|
|
|
const { affectedTablesAndLastUpdate } = res.locals;
|
2017-12-31 00:18:12 +08:00
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
|
|
|
|
var lastModifiedDate;
|
|
|
|
if (Number.isFinite(affectedTablesAndLastUpdate.lastUpdatedTime)) {
|
|
|
|
lastModifiedDate = new Date(affectedTablesAndLastUpdate.getLastUpdatedAt());
|
|
|
|
} else {
|
|
|
|
lastModifiedDate = new Date();
|
2017-12-31 00:18:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-01 23:21:22 +08:00
|
|
|
res.set('Last-Modified', lastModifiedDate.toUTCString());
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
NamedMapsController.prototype.setCacheControlHeader = function () {
|
|
|
|
return function setCacheControlHeaderMiddleware(req, res, next) {
|
|
|
|
const { affectedTablesAndLastUpdate } = res.locals;
|
|
|
|
|
|
|
|
res.set('Cache-Control', 'public,max-age=7200,must-revalidate');
|
|
|
|
|
|
|
|
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
|
|
|
|
// we increase cache control as we can invalidate it
|
|
|
|
res.set('Cache-Control', 'public,max-age=31536000');
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
NamedMapsController.prototype.setContentTypeHeader = function () {
|
|
|
|
return function setContentTypeHeaderMiddleware(req, res, next) {
|
|
|
|
const { headers = {} } = res.locals;
|
|
|
|
|
|
|
|
res.set('Content-Type', headers['content-type'] || headers['Content-Type'] || 'image/png');
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
2017-12-31 00:18:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NamedMapsController.prototype.respond = function () {
|
|
|
|
return function respondMiddleware (req, res) {
|
|
|
|
const { body, stats = {}, format } = res.locals;
|
|
|
|
|
|
|
|
req.profiler.done('render-' + format);
|
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
res.status(200);
|
|
|
|
res.send(body);
|
|
|
|
};
|
|
|
|
};
|