2015-07-10 17:24:32 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var step = require('step');
|
|
|
|
|
|
|
|
var cors = require('../middleware/cors');
|
2015-09-30 23:17:01 +08:00
|
|
|
var userMiddleware = require('../middleware/user');
|
2017-03-31 02:12:55 +08:00
|
|
|
var allowQueryParams = require('../middleware/allow-query-params');
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2016-03-19 00:22:02 +08:00
|
|
|
var DataviewBackend = require('../backends/dataview');
|
2016-04-14 19:25:56 +08:00
|
|
|
var AnalysisStatusBackend = require('../backends/analysis-status');
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2016-05-23 19:29:34 +08:00
|
|
|
var MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider');
|
2016-02-22 18:40:25 +08:00
|
|
|
|
2016-02-23 02:11:54 +08:00
|
|
|
var QueryTables = require('cartodb-query-tables');
|
2015-07-10 17:24:32 +08:00
|
|
|
|
|
|
|
/**
|
2015-09-16 22:18:26 +08:00
|
|
|
* @param {AuthApi} authApi
|
|
|
|
* @param {PgConnection} pgConnection
|
2015-07-10 17:24:32 +08:00
|
|
|
* @param {MapStore} mapStore
|
|
|
|
* @param {TileBackend} tileBackend
|
|
|
|
* @param {PreviewBackend} previewBackend
|
|
|
|
* @param {AttributesBackend} attributesBackend
|
2015-07-14 19:40:41 +08:00
|
|
|
* @param {SurrogateKeysCache} surrogateKeysCache
|
2015-07-14 17:55:49 +08:00
|
|
|
* @param {UserLimitsApi} userLimitsApi
|
2015-07-14 19:40:41 +08:00
|
|
|
* @param {LayergroupAffectedTables} layergroupAffectedTables
|
2016-04-14 23:09:07 +08:00
|
|
|
* @param {AnalysisBackend} analysisBackend
|
2015-07-10 17:24:32 +08:00
|
|
|
* @constructor
|
|
|
|
*/
|
2017-09-26 01:40:27 +08:00
|
|
|
function LayergroupController(prepareContext, pgConnection, mapStore, tileBackend, previewBackend, attributesBackend,
|
2016-05-27 01:32:58 +08:00
|
|
|
surrogateKeysCache, userLimitsApi, layergroupAffectedTables, analysisBackend) {
|
2016-02-22 18:40:25 +08:00
|
|
|
this.pgConnection = pgConnection;
|
2015-07-10 17:24:32 +08:00
|
|
|
this.mapStore = mapStore;
|
|
|
|
this.tileBackend = tileBackend;
|
|
|
|
this.previewBackend = previewBackend;
|
|
|
|
this.attributesBackend = attributesBackend;
|
2015-07-14 19:40:41 +08:00
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
2015-07-11 01:10:55 +08:00
|
|
|
this.userLimitsApi = userLimitsApi;
|
2015-07-14 19:40:41 +08:00
|
|
|
this.layergroupAffectedTables = layergroupAffectedTables;
|
2016-03-19 00:22:02 +08:00
|
|
|
|
2016-04-14 23:09:07 +08:00
|
|
|
this.dataviewBackend = new DataviewBackend(analysisBackend);
|
2016-04-14 19:25:56 +08:00
|
|
|
this.analysisStatusBackend = new AnalysisStatusBackend();
|
2017-09-21 19:37:32 +08:00
|
|
|
|
2017-09-26 01:40:27 +08:00
|
|
|
this.prepareContext = prepareContext;
|
2015-07-10 17:24:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LayergroupController;
|
|
|
|
|
|
|
|
LayergroupController.prototype.register = function(app) {
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:z/:x/:y@:scale_factor?x.:format',
|
2017-09-22 04:53:31 +08:00
|
|
|
cors(),
|
2017-10-05 18:12:21 +08:00
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:53:31 +08:00
|
|
|
this.tile.bind(this)
|
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:z/:x/:y.:format',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:53:31 +08:00
|
|
|
this.tile.bind(this)
|
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:layer/:z/:x/:y.(:format)',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-26 20:56:20 +08:00
|
|
|
validateLayerRouteMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:53:31 +08:00
|
|
|
this.layer.bind(this)
|
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:layer/attributes/:fid',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:47:08 +08:00
|
|
|
this.attributes.bind(this)
|
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/static/center/:token/:z/:lat/:lng/:width/:height.:format',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 04:43:59 +08:00
|
|
|
allowQueryParams(['layer']),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:43:59 +08:00
|
|
|
this.center.bind(this)
|
|
|
|
);
|
2015-09-30 23:17:01 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 04:43:59 +08:00
|
|
|
allowQueryParams(['layer']),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-22 04:43:59 +08:00
|
|
|
this.bbox.bind(this)
|
|
|
|
);
|
2015-10-07 01:47:44 +08:00
|
|
|
|
2016-01-16 00:02:09 +08:00
|
|
|
// Undocumented/non-supported API endpoint methods.
|
|
|
|
// Use at your own peril.
|
2016-03-23 19:14:17 +08:00
|
|
|
|
2017-06-09 01:22:33 +08:00
|
|
|
var allowedDataviewQueryParams = [
|
|
|
|
'filters', // json
|
|
|
|
'own_filter', // 0, 1
|
|
|
|
'bbox', // w,s,e,n
|
|
|
|
'start', // number
|
|
|
|
'end', // number
|
|
|
|
'column_type', // string
|
|
|
|
'bins', // number
|
|
|
|
'aggregation', //string
|
2017-07-15 00:30:36 +08:00
|
|
|
'offset', // number
|
2017-11-07 23:14:47 +08:00
|
|
|
'q', // widgets search
|
|
|
|
'categories', // number
|
2017-06-09 01:22:33 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/dataview/:dataviewName',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
|
|
|
allowQueryParams(allowedDataviewQueryParams),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-06-09 01:22:33 +08:00
|
|
|
this.dataview.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:layer/widget/:dataviewName',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
|
|
|
allowQueryParams(allowedDataviewQueryParams),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-06-09 01:22:33 +08:00
|
|
|
this.dataview.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/dataview/:dataviewName/search',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
|
|
|
allowQueryParams(allowedDataviewQueryParams),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-06-09 01:22:33 +08:00
|
|
|
this.dataviewSearch.bind(this)
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/:layer/widget/:dataviewName/search',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
|
|
|
allowQueryParams(allowedDataviewQueryParams),
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-06-09 01:22:33 +08:00
|
|
|
this.dataviewSearch.bind(this)
|
|
|
|
);
|
2016-04-12 00:49:56 +08:00
|
|
|
|
2017-10-05 18:12:21 +08:00
|
|
|
app.get(
|
|
|
|
app.base_url_mapconfig + '/:token/analysis/node/:nodeId',
|
|
|
|
cors(),
|
|
|
|
userMiddleware,
|
2017-09-22 23:56:47 +08:00
|
|
|
this.prepareContext,
|
2017-09-21 19:37:32 +08:00
|
|
|
this.analysisNodeStatus.bind(this)
|
|
|
|
);
|
2016-04-12 00:49:56 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.analysisNodeStatus = function(req, res, next) {
|
2016-04-14 16:59:51 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
2017-09-21 19:37:32 +08:00
|
|
|
function retrieveNodeStatus() {
|
2017-10-09 22:29:35 +08:00
|
|
|
self.analysisStatusBackend.getNodeStatus(res.locals, this);
|
2016-04-14 16:59:51 +08:00
|
|
|
},
|
|
|
|
function finish(err, nodeStatus, stats) {
|
|
|
|
req.profiler.add(stats || {});
|
|
|
|
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'GET NODE STATUS';
|
|
|
|
next(err);
|
2016-04-14 16:59:51 +08:00
|
|
|
} else {
|
2016-05-05 23:52:37 +08:00
|
|
|
self.sendResponse(req, res, nodeStatus, 200, {
|
|
|
|
'Cache-Control': 'public,max-age=5',
|
|
|
|
'Last-Modified': new Date().toUTCString()
|
|
|
|
});
|
2016-04-14 16:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2016-03-19 00:22:02 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.dataview = function(req, res, next) {
|
2016-03-19 00:22:02 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
2017-09-21 20:33:32 +08:00
|
|
|
function retrieveDataview() {
|
2016-03-19 00:22:02 +08:00
|
|
|
var mapConfigProvider = new MapStoreMapConfigProvider(
|
2017-10-03 23:47:57 +08:00
|
|
|
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
|
2016-03-19 00:22:02 +08:00
|
|
|
);
|
2017-09-29 20:38:28 +08:00
|
|
|
self.dataviewBackend.getDataview(
|
2017-10-10 17:58:24 +08:00
|
|
|
mapConfigProvider,
|
|
|
|
res.locals.user,
|
|
|
|
res.locals,
|
2017-09-29 20:38:28 +08:00
|
|
|
this
|
2016-03-19 00:22:02 +08:00
|
|
|
);
|
|
|
|
},
|
2016-04-14 01:15:06 +08:00
|
|
|
function finish(err, dataview, stats) {
|
2016-03-19 00:22:02 +08:00
|
|
|
req.profiler.add(stats || {});
|
|
|
|
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'GET DATAVIEW';
|
|
|
|
next(err);
|
2016-03-23 19:14:17 +08:00
|
|
|
} else {
|
2016-04-14 01:15:06 +08:00
|
|
|
self.sendResponse(req, res, dataview, 200);
|
2016-03-23 19:14:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.dataviewSearch = function(req, res, next) {
|
2016-03-23 19:14:17 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
2017-09-21 19:39:58 +08:00
|
|
|
function searchDataview() {
|
2016-03-23 19:14:17 +08:00
|
|
|
var mapConfigProvider = new MapStoreMapConfigProvider(
|
2017-10-03 23:47:57 +08:00
|
|
|
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
|
2016-03-23 19:14:17 +08:00
|
|
|
);
|
2017-10-03 23:47:57 +08:00
|
|
|
self.dataviewBackend.search(mapConfigProvider, res.locals.user, req.params.dataviewName, res.locals, this);
|
2016-03-23 19:14:17 +08:00
|
|
|
},
|
2016-04-14 01:15:06 +08:00
|
|
|
function finish(err, searchResult, stats) {
|
2016-03-23 19:14:17 +08:00
|
|
|
req.profiler.add(stats || {});
|
|
|
|
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'GET DATAVIEW SEARCH';
|
|
|
|
next(err);
|
2016-03-19 00:22:02 +08:00
|
|
|
} else {
|
2016-04-14 01:15:06 +08:00
|
|
|
self.sendResponse(req, res, searchResult, 200);
|
2016-03-19 00:22:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.attributes = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
req.profiler.start('windshaft.maplayer_attribute');
|
|
|
|
|
|
|
|
step(
|
2017-09-22 04:47:08 +08:00
|
|
|
function retrieveFeatureAttributes() {
|
2015-09-15 00:46:22 +08:00
|
|
|
var mapConfigProvider = new MapStoreMapConfigProvider(
|
2017-10-03 23:47:57 +08:00
|
|
|
self.mapStore, res.locals.user, self.userLimitsApi, res.locals
|
2015-09-15 00:46:22 +08:00
|
|
|
);
|
2017-09-29 18:32:46 +08:00
|
|
|
self.attributesBackend.getFeatureAttributes(mapConfigProvider, res.locals, false, this);
|
2015-07-10 17:24:32 +08:00
|
|
|
},
|
|
|
|
function finish(err, tile, stats) {
|
|
|
|
req.profiler.add(stats || {});
|
|
|
|
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'GET ATTRIBUTES';
|
|
|
|
next(err);
|
2015-07-10 17:24:32 +08:00
|
|
|
} else {
|
2015-09-17 08:05:25 +08:00
|
|
|
self.sendResponse(req, res, tile, 200);
|
2015-07-10 17:24:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Gets a tile for a given token and set of tile ZXY coords. (OSM style)
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.tile = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
req.profiler.start('windshaft.map_tile');
|
2017-09-21 17:46:31 +08:00
|
|
|
this.tileOrLayer(req, res, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Gets a tile for a given token, layer set of tile ZXY coords. (OSM style)
|
|
|
|
LayergroupController.prototype.layer = function(req, res, next) {
|
|
|
|
req.profiler.start('windshaft.maplayer_tile');
|
2017-09-21 17:46:31 +08:00
|
|
|
this.tileOrLayer(req, res, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.tileOrLayer = function (req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
2017-09-22 04:53:31 +08:00
|
|
|
function mapController$getTileOrGrid() {
|
2015-07-11 01:10:55 +08:00
|
|
|
self.tileBackend.getTile(
|
2017-10-03 23:47:57 +08:00
|
|
|
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
|
2015-07-11 01:10:55 +08:00
|
|
|
req.params, this
|
|
|
|
);
|
2015-07-10 17:24:32 +08:00
|
|
|
},
|
|
|
|
function mapController$finalize(err, tile, headers, stats) {
|
|
|
|
req.profiler.add(stats);
|
2017-09-21 17:46:31 +08:00
|
|
|
self.finalizeGetTileOrGrid(err, req, res, tile, headers, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-10-06 22:19:00 +08:00
|
|
|
function getStatusCode(tile, format){
|
|
|
|
return tile.length===0 && format==='mvt'? 204:200;
|
|
|
|
}
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
// This function is meant for being called as the very last
|
|
|
|
// step by all endpoints serving tiles or grids
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.finalizeGetTileOrGrid = function(err, req, res, tile, headers, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
var supportedFormats = {
|
|
|
|
grid_json: true,
|
|
|
|
json_torque: true,
|
|
|
|
torque_json: true,
|
2017-05-17 18:04:11 +08:00
|
|
|
png: true,
|
2017-05-17 18:16:16 +08:00
|
|
|
png32: true,
|
2017-05-17 18:04:11 +08:00
|
|
|
mvt: true
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var formatStat = 'invalid';
|
|
|
|
if (req.params.format) {
|
|
|
|
var format = req.params.format.replace('.', '_');
|
|
|
|
if (supportedFormats[format]) {
|
|
|
|
formatStat = format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:36:51 +08:00
|
|
|
if (err) {
|
2015-07-10 17:24:32 +08:00
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/68
|
|
|
|
var errMsg = err.message ? ( '' + err.message ) : ( '' + err );
|
|
|
|
|
|
|
|
// Rewrite mapnik parsing errors to start with layer number
|
|
|
|
var matches = errMsg.match("(.*) in style 'layer([0-9]+)'");
|
|
|
|
if (matches) {
|
|
|
|
errMsg = 'style'+matches[2]+': ' + matches[1];
|
|
|
|
}
|
2015-09-16 07:36:51 +08:00
|
|
|
err.message = errMsg;
|
2015-07-10 17:24:32 +08:00
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'TILE RENDER';
|
|
|
|
next(err);
|
|
|
|
|
2015-07-10 17:24:32 +08:00
|
|
|
global.statsClient.increment('windshaft.tiles.error');
|
|
|
|
global.statsClient.increment('windshaft.tiles.' + formatStat + '.error');
|
|
|
|
} else {
|
2017-10-06 22:19:00 +08:00
|
|
|
this.sendResponse(req, res, tile, getStatusCode(tile, formatStat), headers);
|
2015-07-10 17:24:32 +08:00
|
|
|
global.statsClient.increment('windshaft.tiles.success');
|
|
|
|
global.statsClient.increment('windshaft.tiles.' + formatStat + '.success');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.bbox = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
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
|
2017-10-10 17:58:24 +08:00
|
|
|
}, null, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.center = function(req, res, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
this.staticMap(req, res, +req.params.width, +req.params.height, +req.params.z, {
|
|
|
|
lng: +req.params.lng,
|
|
|
|
lat: +req.params.lat
|
2017-10-05 17:35:49 +08:00
|
|
|
}, next);
|
2015-07-10 17:24:32 +08:00
|
|
|
};
|
|
|
|
|
2017-09-21 17:46:31 +08:00
|
|
|
LayergroupController.prototype.staticMap = function(req, res, width, height, zoom /* bounds */, center, next) {
|
2015-07-10 17:24:32 +08:00
|
|
|
var format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
2017-09-22 04:43:59 +08:00
|
|
|
req.params.format = req.params.format || 'png';
|
2017-10-02 18:07:35 +08:00
|
|
|
res.locals.layer = res.locals.layer || 'all';
|
2015-07-10 17:24:32 +08:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
step(
|
2017-09-22 04:43:59 +08:00
|
|
|
function getImage() {
|
2015-07-10 17:24:32 +08:00
|
|
|
if (center) {
|
2015-07-11 01:10:55 +08:00
|
|
|
self.previewBackend.getImage(
|
2017-10-03 23:47:57 +08:00
|
|
|
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
|
2015-07-10 17:24:32 +08:00
|
|
|
format, width, height, zoom, center, this);
|
|
|
|
} else {
|
2015-07-11 01:10:55 +08:00
|
|
|
self.previewBackend.getImage(
|
2017-10-03 23:47:57 +08:00
|
|
|
new MapStoreMapConfigProvider(self.mapStore, res.locals.user, self.userLimitsApi, res.locals),
|
2015-07-10 17:24:32 +08:00
|
|
|
format, width, height, zoom /* bounds */, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function handleImage(err, image, headers, stats) {
|
|
|
|
req.profiler.done('render-' + format);
|
|
|
|
req.profiler.add(stats || {});
|
|
|
|
|
|
|
|
if (err) {
|
2017-09-21 17:46:31 +08:00
|
|
|
err.label = 'STATIC_MAP';
|
|
|
|
next(err);
|
2015-07-10 17:24:32 +08:00
|
|
|
} else {
|
2015-09-17 08:05:25 +08:00
|
|
|
res.set('Content-Type', headers['Content-Type'] || 'image/' + format);
|
|
|
|
self.sendResponse(req, res, image, 200);
|
2015-07-14 17:55:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-09-17 08:05:25 +08:00
|
|
|
LayergroupController.prototype.sendResponse = function(req, res, body, status, headers) {
|
2017-11-13 19:15:12 +08:00
|
|
|
var self = this;
|
|
|
|
|
2017-02-23 01:38:25 +08:00
|
|
|
req.profiler.done('res');
|
|
|
|
|
2015-09-17 08:03:09 +08:00
|
|
|
res.set('Cache-Control', 'public,max-age=31536000');
|
2015-07-14 17:55:49 +08:00
|
|
|
|
|
|
|
// Set Last-Modified header
|
|
|
|
var lastUpdated;
|
2017-09-29 18:32:46 +08:00
|
|
|
if (res.locals.cache_buster) {
|
2015-07-14 17:55:49 +08:00
|
|
|
// Assuming cache_buster is a timestamp
|
2017-09-29 18:32:46 +08:00
|
|
|
lastUpdated = new Date(parseInt(res.locals.cache_buster));
|
2015-07-14 17:55:49 +08:00
|
|
|
} else {
|
|
|
|
lastUpdated = new Date();
|
|
|
|
}
|
2015-09-17 08:03:09 +08:00
|
|
|
res.set('Last-Modified', lastUpdated.toUTCString());
|
2015-07-14 17:55:49 +08:00
|
|
|
|
2017-11-13 19:15:12 +08:00
|
|
|
var dbName = res.locals.dbname;
|
|
|
|
step(
|
|
|
|
function getAffectedTables() {
|
|
|
|
self.getAffectedTables(res.locals.user, dbName, res.locals.token, this);
|
|
|
|
},
|
|
|
|
function sendResponse(err, affectedTables) {
|
|
|
|
req.profiler.done('affectedTables');
|
|
|
|
if (err) {
|
|
|
|
global.logger.warn('ERROR generating cache channel: ' + err);
|
|
|
|
}
|
|
|
|
if (!!affectedTables) {
|
|
|
|
res.set('X-Cache-Channel', affectedTables.getCacheChannel());
|
|
|
|
self.surrogateKeysCache.tag(res, affectedTables);
|
|
|
|
}
|
2017-10-31 02:28:40 +08:00
|
|
|
|
2017-11-13 19:15:12 +08:00
|
|
|
if (headers) {
|
|
|
|
res.set(headers);
|
|
|
|
}
|
2017-10-31 02:28:40 +08:00
|
|
|
|
2017-11-13 19:15:12 +08:00
|
|
|
res.status(status);
|
2017-10-31 02:28:40 +08:00
|
|
|
|
2017-11-13 19:15:12 +08:00
|
|
|
if (!Buffer.isBuffer(body) && typeof body === 'object') {
|
|
|
|
if (req.query && req.query.callback) {
|
|
|
|
res.jsonp(body);
|
|
|
|
} else {
|
|
|
|
res.json(body);
|
|
|
|
}
|
2017-10-31 02:28:40 +08:00
|
|
|
} else {
|
2017-11-13 19:15:12 +08:00
|
|
|
res.send(body);
|
2017-10-31 02:28:40 +08:00
|
|
|
}
|
2015-07-14 17:55:49 +08:00
|
|
|
}
|
2017-11-13 19:15:12 +08:00
|
|
|
);
|
2015-07-14 17:55:49 +08:00
|
|
|
};
|
|
|
|
|
2015-07-14 19:40:41 +08:00
|
|
|
LayergroupController.prototype.getAffectedTables = function(user, dbName, layergroupId, callback) {
|
2017-11-13 19:15:12 +08:00
|
|
|
|
2015-07-15 02:11:49 +08:00
|
|
|
if (this.layergroupAffectedTables.hasAffectedTables(dbName, layergroupId)) {
|
|
|
|
return callback(null, this.layergroupAffectedTables.get(dbName, layergroupId));
|
|
|
|
}
|
2015-07-14 17:55:49 +08:00
|
|
|
|
2015-07-15 02:11:49 +08:00
|
|
|
var self = this;
|
|
|
|
step(
|
|
|
|
function extractSQL() {
|
2015-07-14 17:55:49 +08:00
|
|
|
step(
|
|
|
|
function loadFromStore() {
|
2015-07-14 19:40:41 +08:00
|
|
|
self.mapStore.load(layergroupId, this);
|
2015-07-14 17:55:49 +08:00
|
|
|
},
|
|
|
|
function getSQL(err, mapConfig) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
2017-03-14 00:28:29 +08:00
|
|
|
var queries = [];
|
2017-03-14 01:36:13 +08:00
|
|
|
mapConfig.getLayers().forEach(function(layer) {
|
2017-03-14 00:28:29 +08:00
|
|
|
queries.push(layer.options.sql);
|
|
|
|
if (layer.options.affected_tables) {
|
|
|
|
layer.options.affected_tables.map(function(table) {
|
|
|
|
queries.push('SELECT * FROM ' + table + ' LIMIT 0');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-14 17:55:49 +08:00
|
|
|
|
|
|
|
return queries.length ? queries.join(';') : null;
|
|
|
|
},
|
|
|
|
this
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function findAffectedTables(err, sql) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
if ( ! sql ) {
|
|
|
|
throw new Error("this request doesn't need an X-Cache-Channel generated");
|
2015-07-10 17:24:32 +08:00
|
|
|
}
|
2015-07-14 17:55:49 +08:00
|
|
|
|
2016-02-22 18:40:25 +08:00
|
|
|
step(
|
|
|
|
function getConnection() {
|
|
|
|
self.pgConnection.getConnection(user, this);
|
|
|
|
},
|
|
|
|
function getAffectedTables(err, connection) {
|
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
QueryTables.getAffectedTablesFromQuery(connection, sql, this);
|
|
|
|
},
|
|
|
|
this
|
|
|
|
);
|
2015-07-14 17:55:49 +08:00
|
|
|
},
|
2016-02-10 02:06:34 +08:00
|
|
|
function buildCacheChannel(err, tables) {
|
2015-07-14 17:55:49 +08:00
|
|
|
assert.ifError(err);
|
2016-02-22 18:40:25 +08:00
|
|
|
self.layergroupAffectedTables.set(dbName, layergroupId, tables);
|
2015-07-14 17:55:49 +08:00
|
|
|
|
2016-02-10 02:06:34 +08:00
|
|
|
return tables;
|
2015-07-14 17:55:49 +08:00
|
|
|
},
|
2016-03-04 02:25:32 +08:00
|
|
|
callback
|
2015-07-10 17:24:32 +08:00
|
|
|
);
|
|
|
|
};
|
2017-09-26 20:56:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
function validateLayerRouteMiddleware(req, res, next) {
|
|
|
|
if (req.params.token === 'static') {
|
|
|
|
return next('route');
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|