2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
const layergroupToken = require('../middlewares/layergroup-token');
|
2018-04-17 22:07:47 +08:00
|
|
|
const coordinates = require('../middlewares/coordinates');
|
2018-04-07 00:20:33 +08:00
|
|
|
const cleanUpQueryParams = require('../middlewares/clean-up-query-params');
|
|
|
|
const credentials = require('../middlewares/credentials');
|
2018-05-11 20:05:41 +08:00
|
|
|
const noop = require('../middlewares/noop');
|
2018-04-07 00:20:33 +08:00
|
|
|
const dbConnSetup = require('../middlewares/db-conn-setup');
|
|
|
|
const authorize = require('../middlewares/authorize');
|
|
|
|
const rateLimit = require('../middlewares/rate-limit');
|
2018-03-24 04:20:37 +08:00
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
|
2018-04-09 22:18:30 +08:00
|
|
|
const createMapStoreMapConfigProvider = require('../middlewares/map-store-map-config-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-03-24 04:20:37 +08:00
|
|
|
|
2018-03-29 01:37:31 +08:00
|
|
|
module.exports = class PreviewLayergroupController {
|
2018-03-24 04:20:37 +08:00
|
|
|
constructor (
|
|
|
|
previewBackend,
|
|
|
|
pgConnection,
|
|
|
|
mapStore,
|
2018-04-10 16:16:07 +08:00
|
|
|
userLimitsBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
layergroupAffectedTablesCache,
|
2018-04-10 00:08:56 +08:00
|
|
|
authBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
surrogateKeysCache
|
|
|
|
) {
|
|
|
|
this.previewBackend = previewBackend;
|
|
|
|
this.pgConnection = pgConnection;
|
|
|
|
this.mapStore = mapStore;
|
2018-04-10 16:16:07 +08:00
|
|
|
this.userLimitsBackend = userLimitsBackend;
|
2018-03-24 04:20:37 +08:00
|
|
|
this.layergroupAffectedTablesCache = layergroupAffectedTablesCache;
|
2018-04-10 00:08:56 +08:00
|
|
|
this.authBackend = authBackend;
|
2018-03-24 04:20:37 +08:00
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:22:23 +08:00
|
|
|
route (mapRouter) {
|
2018-05-11 20:05:41 +08:00
|
|
|
mapRouter.get('/static/center/:token/:z/:lat/:lng/:width/:height.:format', this.middlewares({
|
|
|
|
validateZoom: true,
|
|
|
|
previewType: 'centered'
|
|
|
|
}));
|
|
|
|
|
|
|
|
mapRouter.get('/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format', this.middlewares({
|
|
|
|
validateZoom: false,
|
|
|
|
previewType: 'bbox'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
middlewares ({ validateZoom, previewType }) {
|
2018-03-24 04:20:37 +08:00
|
|
|
const forcedFormat = 'png';
|
|
|
|
|
2018-05-11 20:05:41 +08:00
|
|
|
let getPreviewImage;
|
|
|
|
|
|
|
|
if (previewType === 'centered') {
|
|
|
|
getPreviewImage = getPreviewImageByCenter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previewType === 'bbox') {
|
|
|
|
getPreviewImage = getPreviewImageByBoundingBox;
|
|
|
|
}
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2018-05-11 20:05:41 +08:00
|
|
|
return [
|
2018-03-24 04:20:37 +08:00
|
|
|
layergroupToken(),
|
2018-05-11 20:05:41 +08:00
|
|
|
validateZoom ? coordinates({ z: true, x: false, y: false }) : noop(),
|
2018-03-24 04:20:37 +08:00
|
|
|
credentials(),
|
2018-04-10 00:08:56 +08:00
|
|
|
authorize(this.authBackend),
|
2018-03-24 04:20:37 +08:00
|
|
|
dbConnSetup(this.pgConnection),
|
2018-04-10 16:16:07 +08:00
|
|
|
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC),
|
2018-03-24 04:20:37 +08:00
|
|
|
cleanUpQueryParams(['layer']),
|
2018-05-11 23:45:17 +08:00
|
|
|
checkStaticImageFormat(),
|
2018-03-24 04:20:37 +08:00
|
|
|
createMapStoreMapConfigProvider(
|
|
|
|
this.mapStore,
|
2018-04-10 16:16:07 +08:00
|
|
|
this.userLimitsBackend,
|
2018-03-24 04:20:37 +08:00
|
|
|
this.pgConnection,
|
|
|
|
this.layergroupAffectedTablesCache,
|
|
|
|
forcedFormat
|
|
|
|
),
|
2018-05-11 20:05:41 +08:00
|
|
|
getPreviewImage(this.previewBackend),
|
2018-03-24 04:20:37 +08:00
|
|
|
cacheControlHeader(),
|
|
|
|
cacheChannelHeader(),
|
|
|
|
surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }),
|
2018-04-05 01:15:51 +08:00
|
|
|
lastModifiedHeader()
|
2018-05-11 20:05:41 +08:00
|
|
|
];
|
2018-03-24 04:20:37 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function getPreviewImageByCenter (previewBackend) {
|
|
|
|
return function getPreviewImageByCenterMiddleware (req, res, next) {
|
|
|
|
const width = +req.params.width;
|
|
|
|
const height = +req.params.height;
|
|
|
|
const zoom = +req.params.z;
|
|
|
|
const center = {
|
|
|
|
lng: +req.params.lng,
|
|
|
|
lat: +req.params.lat
|
|
|
|
};
|
|
|
|
|
|
|
|
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
2019-06-17 16:43:30 +08:00
|
|
|
const { mapConfigProvider } = res.locals;
|
|
|
|
const options = { mapConfigProvider, format, width, height, zoom, center };
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2019-06-17 16:43:30 +08:00
|
|
|
previewBackend.getImage(options, (err, image, stats = {}) => {
|
2018-03-24 04:20:37 +08:00
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = 'STATIC_MAP';
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:49:56 +08:00
|
|
|
res.set('Content-Type', `image/${format}`);
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-24 04:20:37 +08:00
|
|
|
res.body = image;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPreviewImageByBoundingBox (previewBackend) {
|
|
|
|
return function getPreviewImageByBoundingBoxMiddleware (req, res, next) {
|
|
|
|
const width = +req.params.width;
|
|
|
|
const height = +req.params.height;
|
2019-06-17 16:43:30 +08:00
|
|
|
const bbox = {
|
2018-03-24 04:20:37 +08:00
|
|
|
west: +req.params.west,
|
|
|
|
north: +req.params.north,
|
|
|
|
east: +req.params.east,
|
|
|
|
south: +req.params.south
|
|
|
|
};
|
|
|
|
const format = req.params.format === 'jpg' ? 'jpeg' : 'png';
|
2019-06-17 16:43:30 +08:00
|
|
|
const { mapConfigProvider } = res.locals;
|
|
|
|
const options = { mapConfigProvider, format, width, height, bbox };
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2019-06-17 16:43:30 +08:00
|
|
|
previewBackend.getImage(options, (err, image, stats = {}) => {
|
2018-03-24 04:20:37 +08:00
|
|
|
req.profiler.add(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
err.label = 'STATIC_MAP';
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2019-06-10 18:49:56 +08:00
|
|
|
res.set('Content-Type', `image/${format}`);
|
2018-03-24 04:20:37 +08:00
|
|
|
|
2018-05-09 21:00:18 +08:00
|
|
|
res.statusCode = 200;
|
2018-03-24 04:20:37 +08:00
|
|
|
res.body = image;
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|