Windshaft-cartodb/lib/cartodb/controllers/named_maps.js

528 lines
15 KiB
JavaScript
Raw Normal View History

const NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
const cors = require('../middleware/cors');
2018-03-16 23:28:50 +08:00
const user = require('../middleware/user');
const locals = require('../middleware/locals');
const cleanUpQueryParams = require('../middleware/clean-up-query-params');
const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize');
const vectorError = require('../middleware/vector-error');
const DEFAULT_ZOOM_CENTER = {
zoom: 1,
center: {
lng: 0,
lat: 0
}
};
function numMapper(n) {
return +n;
}
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;
}
function NamedMapsController(namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi, metadataBackend, pgConnection, authApi) {
this.namedMapProviderCache = namedMapProviderCache;
this.tileBackend = tileBackend;
this.previewBackend = previewBackend;
2015-01-23 23:37:38 +08:00
this.surrogateKeysCache = surrogateKeysCache;
this.tablesExtentApi = tablesExtentApi;
this.metadataBackend = metadataBackend;
this.pgConnection = pgConnection;
this.authApi = authApi;
}
module.exports = NamedMapsController;
NamedMapsController.prototype.register = function(app) {
2018-03-16 20:04:42 +08:00
const { base_url_mapconfig: mapconfigBasePath, base_url_templated: templateBasePath } = app;
2018-03-15 00:33:54 +08:00
app.get(
2018-03-16 20:04:42 +08:00
`${templateBasePath}/:template_id/:layer/:z/:x/:y.(:format)`,
cors(),
cleanUpQueryParams(),
locals(),
2018-03-16 23:28:50 +08:00
user(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
2018-03-15 00:08:04 +08:00
getNamedMapProvider({
namedMapProviderCache: this.namedMapProviderCache,
label: 'NAMED_MAP_TILE'
}),
getAffectedTables(),
2018-03-15 00:08:04 +08:00
getTile({
tileBackend: this.tileBackend,
label: 'NAMED_MAP_TILE'
}),
setSurrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
setCacheChannelHeader(),
setLastModifiedHeader(),
setCacheControlHeader(),
setContentTypeHeader(),
sendResponse(),
vectorError()
);
app.get(
2018-03-16 20:04:42 +08:00
`${mapconfigBasePath}/static/named/:template_id/:width/:height.:format`,
cors(),
cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
locals(),
2018-03-16 23:28:50 +08:00
user(),
layergroupToken(),
credentials(),
authorize(this.authApi),
dbConnSetup(this.pgConnection),
2018-03-15 00:15:50 +08:00
getNamedMapProvider({
namedMapProviderCache: this.namedMapProviderCache,
label: 'STATIC_VIZ_MAP', forcedFormat: 'png'
}),
getAffectedTables(),
2018-03-15 00:08:04 +08:00
getTemplate({ label: 'STATIC_VIZ_MAP' }),
prepareLayerFilterFromPreviewLayers({
namedMapProviderCache: this.namedMapProviderCache,
label: 'STATIC_VIZ_MAP'
}),
getStaticImageOptions({ tablesExtentApi: this.tablesExtentApi }),
2018-03-15 00:08:04 +08:00
getImage({ previewBackend: this.previewBackend, label: 'STATIC_VIZ_MAP' }),
incrementMapViews({ metadataBackend: this.metadataBackend }),
setSurrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
setCacheChannelHeader(),
setLastModifiedHeader(),
setCacheControlHeader(),
setContentTypeHeader(),
sendResponse()
);
};
function getNamedMapProvider ({ namedMapProviderCache, label, forcedFormat = null }) {
return function getNamedMapProviderMiddleware (req, res, next) {
const { user } = res.locals;
const { config, auth_token } = req.query;
const { template_id } = req.params;
if (forcedFormat) {
res.locals.format = forcedFormat;
res.locals.layer = res.locals.layer || 'all';
}
2018-01-16 01:09:54 +08:00
const params = getRequestParams(res.locals);
namedMapProviderCache.get(user, template_id, config, auth_token, params, (err, namedMapProvider) => {
if (err) {
2017-12-30 23:08:46 +08:00
err.label = label;
return next(err);
}
res.locals.namedMapProvider = namedMapProvider;
next();
});
};
}
function getAffectedTables () {
2018-01-01 23:21:22 +08:00
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();
});
};
}
2018-01-01 23:21:22 +08:00
function getTemplate ({ 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);
}
res.locals.template = template;
2017-12-30 21:13:23 +08:00
next();
});
};
}
2017-12-30 21:13:23 +08:00
function prepareLayerFilterFromPreviewLayers ({ namedMapProviderCache, 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
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) => {
if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) {
layerVisibilityFilter.push(''+index);
2017-12-30 21:13:23 +08:00
}
});
2017-12-30 21:13:23 +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);
// overwrites 'all' default filter
params.layer = layerVisibilityFilter.join(',');
2017-12-30 21:13:23 +08:00
// recreates the provider
namedMapProviderCache.get(user, template_id, config, auth_token, params, (err, provider) => {
if (err) {
err.label = label;
return next(err);
}
2017-12-30 21:13:23 +08:00
res.locals.namedMapProvider = provider;
next();
2017-12-30 21:13:23 +08:00
});
};
}
2017-12-30 21:13:23 +08:00
function getTile ({ tileBackend, label }) {
2017-12-31 00:18:12 +08:00
return function getTileMiddleware (req, res, next) {
const { namedMapProvider } = res.locals;
tileBackend.getTile(namedMapProvider, req.params, (err, tile, headers, stats) => {
2017-12-31 00:18:12 +08:00
req.profiler.add(stats);
if (err) {
2018-01-17 00:55:09 +08:00
err.label = label;
2017-12-31 00:18:12 +08:00
return next(err);
}
if (headers) {
res.set(headers);
}
res.body = tile;
2017-12-30 02:33:49 +08:00
2017-12-31 00:18:12 +08:00
next();
});
};
}
2015-06-29 22:39:35 +08:00
function getStaticImageOptions ({ tablesExtentApi }) {
return function getStaticImageOptionsMiddleware(req, res, next) {
2018-01-02 01:06:56 +08:00
const { user, namedMapProvider, template } = res.locals;
2018-01-02 01:06:56 +08:00
const imageOpts = getImageOptions(res.locals, template);
2018-01-02 01:06:56 +08:00
if (imageOpts) {
res.locals.imageOpts = imageOpts;
return next();
}
res.locals.imageOpts = DEFAULT_ZOOM_CENTER;
2017-12-31 01:02:48 +08:00
namedMapProvider.getAffectedTablesAndLastUpdatedTime((err, affectedTablesAndLastUpdate) => {
if (err) {
return next();
}
var affectedTables = affectedTablesAndLastUpdate.tables || [];
if (affectedTables.length === 0) {
return next();
}
tablesExtentApi.getBounds(user, affectedTables, (err, bounds) => {
if (err) {
2017-12-31 01:02:48 +08:00
return next();
}
res.locals.imageOpts = bounds;
2018-01-02 01:06:56 +08:00
return next();
2017-12-31 01:02:48 +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
}
}
function getImage({ previewBackend, label }) {
return function getImageMiddleware (req, res, next) {
const { imageOpts, namedMapProvider } = res.locals;
const { zoom, center, bounds } = imageOpts;
let { width, height } = req.params;
width = +width;
height = +height;
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
2017-12-31 01:18:37 +08:00
if (zoom !== undefined && center) {
return previewBackend.getImage(namedMapProvider, format, width, height, zoom, center,
(err, image, headers, stats) => {
req.profiler.add(stats);
if (err) {
2017-12-30 23:08:46 +08:00
err.label = label;
return next(err);
}
if (headers) {
res.set(headers);
}
res.body = image;
next();
});
}
previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
req.profiler.add(stats);
if (err) {
2017-12-30 23:08:46 +08:00
err.label = label;
return next(err);
}
if (headers) {
res.set(headers);
}
res.body = image;
next();
});
};
}
function incrementMapViewsError (ctx) {
return `ERROR: failed to increment mapview count for user '${ctx.user}': ${ctx.err}`;
}
function incrementMapViews ({ metadataBackend }) {
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;
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
global.logger.log(incrementMapViewsError({ user, err }));
}
next();
});
});
};
}
function templateZoomCenter(view) {
2017-12-31 01:18:37 +08:00
if (view.zoom !== undefined && view.center) {
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
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
function setSurrogateKeyHeader ({ surrogateKeysCache }) {
return function setSurrogateKeyHeaderMiddleware(req, res, next) {
const { user, namedMapProvider, affectedTablesAndLastUpdate } = res.locals;
2017-12-31 00:18:12 +08:00
surrogateKeysCache.tag(res, new NamedMapsCacheEntry(user, namedMapProvider.getTemplateName()));
2018-01-01 23:21:22 +08:00
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
if (affectedTablesAndLastUpdate.tables.length > 0) {
surrogateKeysCache.tag(res, affectedTablesAndLastUpdate);
}
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();
};
}
2018-01-01 23:21:22 +08:00
function setCacheChannelHeader () {
return function setCacheChannelHeaderMiddleware (req, res, next) {
const { affectedTablesAndLastUpdate } = res.locals;
2018-01-01 23:21:22 +08:00
if (!affectedTablesAndLastUpdate || !!affectedTablesAndLastUpdate.tables) {
res.set('X-Cache-Channel', affectedTablesAndLastUpdate.getCacheChannel());
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();
};
}
2017-12-31 00:18:12 +08:00
function setLastModifiedHeader () {
2018-01-01 23:21:22 +08:00
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();
};
}
2018-01-01 23:21:22 +08:00
function setCacheControlHeader () {
2018-01-01 23:21:22 +08:00
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();
};
}
2018-01-01 23:21:22 +08:00
function setContentTypeHeader () {
2018-01-01 23:21:22 +08:00
return function setContentTypeHeaderMiddleware(req, res, next) {
res.set('Content-Type', res.get('content-type') || res.get('Content-Type') || 'image/png');
2018-01-01 23:21:22 +08:00
next();
};
}
2017-12-31 00:18:12 +08:00
function sendResponse () {
return function sendResponseMiddleware (req, res) {
const { format } = res.locals;
2017-12-31 00:18:12 +08:00
req.profiler.done('render-' + format);
res.status(200);
res.send(res.body);
2017-12-31 00:18:12 +08:00
};
}