fix: add suggestions

Signed-off-by: acalcutt <acalcutt@techidiots.net>
This commit is contained in:
acalcutt 2024-04-23 01:48:32 -04:00
parent ee77371d8c
commit c848107eb8
2 changed files with 44 additions and 90 deletions

View file

@ -1046,7 +1046,6 @@ export const serve_rendered = {
} }
if (styleJSON.sprite) { if (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)) {
spriteItem.url = spriteItem.url =
@ -1062,21 +1061,6 @@ export const serve_rendered = {
); );
} }
}); });
} else {
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 (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) { if (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) {

View file

@ -10,8 +10,15 @@ import { validateStyleMin } from '@maplibre/maplibre-gl-style-spec';
import { getPublicUrl } from './utils.js'; import { getPublicUrl } from './utils.js';
const httpTester = /^https?:\/\//i; const httpTester = /^https?:\/\//i;
const allowedSpriteScales = allowedOptions(['', '@2x', '@3x'], '');
const allowedSpriteFormats = allowedOptions(['png', 'json']);
const fixUrl = (req, url, publicUrl) => { function allowedOptions(opts, { defaultValue } = {}) {
const values = Object.fromEntries(opts.map(key => [key, key]));
return (value) => values[value] || defaultValue;
}
function fixUrl(req, url, publicUrl) {
if (!url || typeof url !== 'string' || url.indexOf('local://') !== 0) { if (!url || typeof url !== 'string' || url.indexOf('local://') !== 0) {
return url; return url;
} }
@ -59,43 +66,15 @@ export const serve_style = {
app.get( app.get(
'/:id/sprite(/:spriteID)?:scale(@[23]x)?.:format([\\w]+)', '/:id/sprite(/:spriteID)?:scale(@[23]x)?.:format([\\w]+)',
(req, res, next) => { (req, res, next) => {
const spriteID = req.params.spriteID || 'default'; const { spriteID = 'default', id } = req.params;
const scale = req.params.scale || ''; const scale = allowedSpriteScales(req.params.scale) || '';
const format = req.params.format; const format = allowedSpriteFormats(req.params.format);
const item = repo[req.params.id];
let spritePath; if (format) {
if (item && item.spritePaths) { const item = repo[id];
for (const sprite of item.spritePaths) { const sprite = item.spritePaths.find(sprite => sprite.id === spriteID);
if (sprite.id === spriteID) { if (sprite) {
spritePath = sprite.path; const filename = `${sprite.path + scale}.${format}`;
}
}
if (!spritePath) {
return res.sendStatus(404);
}
} else {
return res.sendStatus(404);
}
let spriteScale;
const allowedScales = ['', '@2x', '@3x'];
for (const as of allowedScales) {
if (as === scale) {
spriteScale = as;
}
}
let spriteFormat;
const allowedFormats = ['png', 'json'];
for (const af of allowedFormats) {
if (af === format) {
spriteFormat = af;
}
}
if (spriteFormat) {
const filename = `${spritePath + spriteScale}.${spriteFormat}`;
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);
@ -108,7 +87,10 @@ export const serve_style = {
} }
}); });
} else { } else {
return res.sendStatus(400); return res.status(400).send('Bad Sprite ID or Scale');
}
} else {
return res.status(400).send('Bad Sprite Format');
} }
}, },
); );
@ -180,8 +162,11 @@ export const serve_style = {
let spritePaths = []; let spritePaths = [];
if (styleJSON.sprite) { if (styleJSON.sprite) {
if (Array.isArray(styleJSON.sprite)) { if (!Array.isArray(styleJSON.sprite)) {
styleJSON.sprite.forEach((spriteItem) => { styleJSON.sprite = [{id: 'default', url: styleJSON.sprite }];
}
for (let spriteItem of styleJSON.sprite) {
if (!httpTester.test(spriteItem.url)) { if (!httpTester.test(spriteItem.url)) {
let spritePath = path.join( let spritePath = path.join(
options.paths.sprites, options.paths.sprites,
@ -195,21 +180,6 @@ export const serve_style = {
spriteItem.url = `local://styles/${id}/sprite/` + spriteItem.id; spriteItem.url = `local://styles/${id}/sprite/` + spriteItem.id;
spritePaths.push({ id: spriteItem.id, path: spritePath }); spritePaths.push({ id: spriteItem.id, path: spritePath });
} }
});
} else {
if (!httpTester.test(styleJSON.sprite)) {
let 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`;
spritePaths.push({ id: 'default', path: spritePath });
}
} }
} }