Merge pull request #955 from CartoDB/fix-image-format-png

Unsupported static image format
This commit is contained in:
Simon Martín 2018-05-17 12:07:54 +02:00 committed by GitHub
commit 38c69de01b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 103 additions and 0 deletions

View File

@ -12,6 +12,7 @@ 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');
const checkStaticImageFormat = require('../middlewares/check-static-image-format');
module.exports = class PreviewLayergroupController {
constructor (
@ -65,6 +66,7 @@ module.exports = class PreviewLayergroupController {
dbConnSetup(this.pgConnection),
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC),
cleanUpQueryParams(['layer']),
checkStaticImageFormat(),
createMapStoreMapConfigProvider(
this.mapStore,
this.userLimitsBackend,

View File

@ -7,6 +7,7 @@ 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');
const checkStaticImageFormat = require('../middlewares/check-static-image-format');
const rateLimit = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit;
@ -54,6 +55,7 @@ module.exports = class PreviewTemplateController {
dbConnSetup(this.pgConnection),
rateLimit(this.userLimitsBackend, RATE_LIMIT_ENDPOINTS_GROUPS.STATIC_NAMED),
cleanUpQueryParams(['layer', 'zoom', 'lon', 'lat', 'bbox']),
checkStaticImageFormat(),
namedMapProvider({
namedMapProviderCache: this.namedMapProviderCache,
label: 'STATIC_VIZ_MAP', forcedFormat: 'png'

View File

@ -0,0 +1,11 @@
const VALID_IMAGE_FORMATS = ['png', 'jpg'];
module.exports = function checkStaticImageFormat () {
return function checkStaticImageFormatMiddleware (req, res, next) {
if(!VALID_IMAGE_FORMATS.includes(req.params.format)) {
return next(new Error(`Unsupported image format "${req.params.format}"`));
}
next();
};
};

View File

@ -261,6 +261,41 @@ describe('named-maps analysis', function() {
);
});
it('should return and an error requesting unsupported image format', function(done) {
assert.response(
server,
{
url: '/api/v1/map/static/center/' + layergroupid + '/4/42/-3/320/240.gif',
method: 'GET',
encoding: 'binary',
headers: {
host: username
}
},
{
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function(res, err) {
assert.ifError(err);
assert.deepEqual(
JSON.parse(res.body),
{
errors:['Unsupported image format \"gif\"'],
errors_with_context:[{
type: 'unknown',
message: 'Unsupported image format \"gif\"'
}]
}
);
done();
}
);
});
});
describe('auto-instantiation', function() {

View File

@ -282,4 +282,57 @@ describe('named maps static view', function() {
});
});
it('should return an error requesting unsupported image format', function (done) {
var view = {
zoom: 4,
center: {
lng: 40,
lat: 20
}
};
templateMaps.addTemplate(username, createTemplate(view), function (err) {
if (err) {
return done(err);
}
var url = `/api/v1/map/static/named/${templateName}/640/480.gif`;
var requestOptions = {
url: url,
method: 'GET',
headers: {
host: username
},
encoding: 'binary'
};
var expectedResponse = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
// this could be removed once named maps are invalidated, otherwise you hits the cache
var server = new CartodbWindshaft(serverOptions);
assert.response(server, requestOptions, expectedResponse, function (res, err) {
assert.ifError(err);
assert.deepEqual(
JSON.parse(res.body),
{
errors:['Unsupported image format \"gif\"'],
errors_with_context:[{
type: 'unknown',
message: 'Unsupported image format \"gif\"'
}]
}
);
done();
});
});
});
});