fix: use sprite id instead of name

Signed-off-by: acalcutt <acalcutt@techidiots.net>
This commit is contained in:
acalcutt 2024-04-22 23:33:51 -04:00
parent ae933d5ae6
commit ee77371d8c
2 changed files with 7 additions and 15 deletions

View file

@ -8,7 +8,7 @@ Styles
====== ======
* Styles are served at ``/styles/{id}/style.json`` (+ array at ``/styles.json``) * Styles are served at ``/styles/{id}/style.json`` (+ array at ``/styles.json``)
* Sprites at ``/styles/{id}/sprite[/name][@2x].{format}`` * Sprites at ``/styles/{id}/sprite[/spriteID][@2x].{format}``
* Fonts at ``/fonts/{fontstack}/{start}-{end}.pbf`` * Fonts at ``/fonts/{fontstack}/{start}-{end}.pbf``
Rendered tiles Rendered tiles

View file

@ -57,9 +57,9 @@ export const serve_style = {
}); });
app.get( app.get(
'/:id/sprite(/:name)?:scale(@[23]x)?.:format([\\w]+)', '/:id/sprite(/:spriteID)?:scale(@[23]x)?.:format([\\w]+)',
(req, res, next) => { (req, res, next) => {
const name = req.params.name || 'sprite'; const spriteID = req.params.spriteID || 'default';
const scale = req.params.scale || ''; const scale = req.params.scale || '';
const format = req.params.format; const format = req.params.format;
const item = repo[req.params.id]; const item = repo[req.params.id];
@ -67,7 +67,7 @@ export const serve_style = {
let spritePath; let spritePath;
if (item && item.spritePaths) { if (item && item.spritePaths) {
for (const sprite of item.spritePaths) { for (const sprite of item.spritePaths) {
if (sprite.name === name) { if (sprite.id === spriteID) {
spritePath = sprite.path; spritePath = sprite.path;
} }
} }
@ -96,7 +96,6 @@ export const serve_style = {
if (spriteFormat) { if (spriteFormat) {
const filename = `${spritePath + spriteScale}.${spriteFormat}`; const filename = `${spritePath + spriteScale}.${spriteFormat}`;
// eslint-disable-next-line security/detect-non-literal-fs-filename
return fs.readFile(filename, (err, data) => { return fs.readFile(filename, (err, data) => {
if (err) { if (err) {
console.log('Sprite load error:', filename); console.log('Sprite load error:', filename);
@ -184,9 +183,6 @@ export const serve_style = {
if (Array.isArray(styleJSON.sprite)) { if (Array.isArray(styleJSON.sprite)) {
styleJSON.sprite.forEach((spriteItem) => { styleJSON.sprite.forEach((spriteItem) => {
if (!httpTester.test(spriteItem.url)) { if (!httpTester.test(spriteItem.url)) {
let spriteName = spriteItem.url.substring(
spriteItem.url.lastIndexOf('/') + 1,
);
let spritePath = path.join( let spritePath = path.join(
options.paths.sprites, options.paths.sprites,
spriteItem.url spriteItem.url
@ -196,12 +192,8 @@ export const serve_style = {
path.relative(options.paths.sprites, path.dirname(styleFile)), path.relative(options.paths.sprites, path.dirname(styleFile)),
), ),
); );
spriteItem.url = `local://styles/${id}/sprite/` + spriteName; spriteItem.url = `local://styles/${id}/sprite/` + spriteItem.id;
spritePaths.push({ spritePaths.push({ id: spriteItem.id, path: spritePath });
id: spriteItem.id,
name: spriteName,
path: spritePath,
});
} }
}); });
} else { } else {
@ -216,7 +208,7 @@ export const serve_style = {
), ),
); );
styleJSON.sprite = `local://styles/${id}/sprite`; styleJSON.sprite = `local://styles/${id}/sprite`;
spritePaths.push({ id: 'default', name: 'sprite', path: spritePath }); spritePaths.push({ id: 'default', path: spritePath });
} }
} }
} }