Fix sprite and glyph loading from remote URLs

This commit is contained in:
Petr Sloup 2016-12-08 18:22:11 +01:00
parent 442baee1ce
commit bbc14abb4a
2 changed files with 33 additions and 8 deletions

View file

@ -144,9 +144,19 @@ module.exports = function(options, repo, params, id, dataResolver) {
});
};
styleJSON = clone(require(path.join(options.paths.styles, styleFile)));
styleJSON.sprite = 'sprites://' + path.basename(styleFile, '.json');
styleJSON.glyphs = 'fonts://{fontstack}/{range}.pbf';
var styleJSONPath = path.join(options.paths.styles, styleFile);
styleJSON = clone(require(styleJSONPath));
var httpTester = /^(http(s)?:)?\/\//;
if (!httpTester.test(styleJSON.sprite)) {
styleJSON.sprite = 'sprites://' +
styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json'))
.replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleJSONPath)));
}
if (!httpTester.test(styleJSON.glyphs)) {
styleJSON.glyphs = 'fonts://' + styleJSON.glyphs;
}
var tileJSON = {
'tilejson': '2.0.0',

View file

@ -43,11 +43,20 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
};
styleJSON.layers.forEach(findFontReferences);
var spritePath = path.join(options.paths.sprites,
path.basename(styleFile, '.json'));
var spritePath;
styleJSON.sprite = 'local://styles/' + id + '/sprite';
styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf';
var httpTester = /^(http(s)?:)?\/\//;
if (!httpTester.test(styleJSON.sprite)) {
spritePath = path.join(options.paths.sprites,
styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json'))
.replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile)))
);
styleJSON.sprite = 'local://styles/' + id + '/sprite';
}
if (!httpTester.test(styleJSON.glyphs)) {
styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf';
}
repo[id] = styleJSON;
@ -57,7 +66,10 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
if (!opt_nokey && req.query.key) {
queryParams.unshift('key=' + req.query.key);
}
var query = '?' + queryParams.join('&');
var query = '';
if (!opt_nokey) {
query = '?' + queryParams.join('&');
}
return url.replace(
'local://', req.protocol + '://' + req.headers.host + '/') + query;
};
@ -75,6 +87,9 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
app.get('/' + id + '/sprite:scale(@[23]x)?\.:format([\\w]+)',
function(req, res, next) {
if (!spritePath) {
return res.status(404).send('File not found');
}
var scale = req.params.scale,
format = req.params.format;
var filename = spritePath + (scale || '') + '.' + format;