2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-31 20:57:34 +08:00
|
|
|
const BaseMapConfigProvider = require('./base-mapconfig-adapter');
|
2018-05-30 23:22:02 +08:00
|
|
|
const dot = require('dot');
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
// Configure bases for cache keys suitable for string interpolation
|
2019-10-22 01:07:24 +08:00
|
|
|
const baseKey = '{{=it.dbname}}:{{=it.token}}';
|
2018-05-31 02:08:35 +08:00
|
|
|
const rendererKey = baseKey + ':{{=it.dbuser}}:{{=it.format}}:{{=it.layer}}:{{=it.scale_factor}}';
|
|
|
|
|
|
|
|
const baseKeyTpl = dot.template(baseKey);
|
|
|
|
const rendererKeyTpl = dot.template(rendererKey);
|
2018-03-21 21:11:54 +08:00
|
|
|
|
2018-05-31 20:57:34 +08:00
|
|
|
module.exports = class MapStoreMapConfigProvider extends BaseMapConfigProvider {
|
2018-05-31 02:08:35 +08:00
|
|
|
/**
|
|
|
|
* @param {MapStore} mapStore
|
|
|
|
* @param {String} user
|
|
|
|
* @param {UserLimitsBackend} userLimitsBackend
|
|
|
|
* @param {Object} params
|
|
|
|
* @constructor
|
|
|
|
* @type {MapStoreMapConfigProvider}
|
|
|
|
*/
|
|
|
|
constructor (mapStore, user, userLimitsBackend, pgConnection, affectedTablesCache, params) {
|
2018-05-31 20:57:34 +08:00
|
|
|
super();
|
2018-05-31 02:08:35 +08:00
|
|
|
this.mapStore = mapStore;
|
|
|
|
this.user = user;
|
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.affectedTablesCache = affectedTablesCache;
|
2018-05-31 20:57:34 +08:00
|
|
|
this.params = params;
|
2018-05-31 02:08:35 +08:00
|
|
|
this.token = params.token;
|
|
|
|
this.cacheBuster = params.cache_buster || 0;
|
|
|
|
this.mapConfig = null;
|
|
|
|
this.context = null;
|
|
|
|
}
|
2018-03-21 21:11:54 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
getMapConfig (callback) {
|
|
|
|
if (this.mapConfig !== null) {
|
|
|
|
return callback(null, this.mapConfig, this.params, this.context);
|
2015-07-11 01:10:55 +08:00
|
|
|
}
|
2018-05-30 23:09:01 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
const context = {};
|
2018-05-30 23:09:01 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
this.userLimitsBackend.getRenderLimits(this.user, this.params.api_key, (err, renderLimits) => {
|
2018-05-30 23:09:01 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
context.limits = renderLimits;
|
2018-05-30 23:09:01 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
this.mapStore.load(this.token, (err, mapConfig) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
this.mapConfig = mapConfig;
|
|
|
|
this.context = context;
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
return callback(null, mapConfig, this.params, context);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
getKey () {
|
|
|
|
return this.createKey(false);
|
|
|
|
}
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
getCacheBuster () {
|
|
|
|
return this.cacheBuster;
|
|
|
|
}
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
filter (key) {
|
|
|
|
const regex = new RegExp('^' + this.createKey(true) + '.*');
|
|
|
|
return key && key.match(regex);
|
|
|
|
}
|
2015-07-10 18:31:56 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
createKey (base) {
|
2018-10-19 17:57:54 +08:00
|
|
|
const { dbname = '', token = '', dbuser = '', format = '', layer = '', scale_factor = 1 } = this.params;
|
|
|
|
const tplValues = { dbname, token, dbuser, format, layer, scale_factor };
|
2018-03-21 21:11:54 +08:00
|
|
|
|
2018-05-31 02:08:35 +08:00
|
|
|
return (base) ? baseKeyTpl(tplValues) : rendererKeyTpl(tplValues);
|
|
|
|
}
|
2018-04-06 18:59:53 +08:00
|
|
|
};
|