fix: lint

Signed-off-by: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
Andrew Calcutt 2024-04-20 19:06:59 -04:00 committed by acalcutt
parent 92b528498d
commit 80953f49d7
2 changed files with 67 additions and 51 deletions

View file

@ -1050,25 +1050,31 @@ export const serve_rendered = {
styleJSON.sprite.forEach((spriteItem) => { styleJSON.sprite.forEach((spriteItem) => {
if (!httpTester.test(spriteItem.url)) { if (!httpTester.test(spriteItem.url)) {
spriteItem.url = spriteItem.url =
'sprites://' + 'sprites://' +
spriteItem.url spriteItem.url
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace( .replace(
'{styleJsonFolder}', '{styleJsonFolder}',
path.relative(options.paths.sprites, path.dirname(styleJSONPath)), path.relative(
); options.paths.sprites,
path.dirname(styleJSONPath),
),
);
} }
}); });
} else { } else {
if (!httpTester.test(styleJSON.sprite)) { if (!httpTester.test(styleJSON.sprite)) {
styleJSON.sprite = styleJSON.sprite =
'sprites://' + 'sprites://' +
styleJSON.sprite styleJSON.sprite
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace( .replace(
'{styleJsonFolder}', '{styleJsonFolder}',
path.relative(options.paths.sprites, path.dirname(styleJSONPath)), path.relative(
); options.paths.sprites,
path.dirname(styleJSONPath),
),
);
} }
} }
} }

View file

@ -56,39 +56,43 @@ export const serve_style = {
return res.send(styleJSON_); return res.send(styleJSON_);
}); });
app.get('/:id/sprite(/:name)?:scale(@[23]x)?.:format([\\w]+)', (req, res, next) => { app.get(
const item = repo[req.params.id]; '/:id/sprite(/:name)?:scale(@[23]x)?.:format([\\w]+)',
const spriteName = req.params.name || 'sprite'; (req, res, next) => {
const item = repo[req.params.id];
const spriteName = req.params.name || 'sprite';
if (!item || !item.spritePaths) { if (!item || !item.spritePaths) {
return res.sendStatus(404);
}
let spritePath
for (const sprite of item.spritePaths) {
if (sprite.name === spriteName) {
spritePath = sprite.path;
}
}
if (!spritePath) {
return res.sendStatus(404);
}
const scale = req.params.scale;
const format = req.params.format;
const filename = `${spritePath + (scale || '')}.${format}`;
return fs.readFile(filename, (err, data) => {
if (err) {
console.log('Sprite load error:', filename);
return res.sendStatus(404); return res.sendStatus(404);
} else {
if (format === 'json') res.header('Content-type', 'application/json');
if (format === 'png') res.header('Content-type', 'image/png');
return res.send(data);
} }
});
}); let spritePath;
for (const sprite of item.spritePaths) {
if (sprite.name === spriteName) {
spritePath = sprite.path;
}
}
if (!spritePath) {
return res.sendStatus(404);
}
const scale = req.params.scale;
const format = req.params.format;
const filename = `${spritePath + (scale || '')}.${format}`;
return fs.readFile(filename, (err, data) => {
if (err) {
console.log('Sprite load error:', filename);
return res.sendStatus(404);
} else {
if (format === 'json')
res.header('Content-type', 'application/json');
if (format === 'png') res.header('Content-type', 'image/png');
return res.send(data);
}
});
},
);
return app; return app;
}, },
@ -160,18 +164,24 @@ 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 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
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace( .replace(
'{styleJsonFolder}', '{styleJsonFolder}',
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/` + spriteName;
spritePaths.push({id: spriteItem.id, name: spriteName, path: spritePath}); spritePaths.push({
id: spriteItem.id,
name: spriteName,
path: spritePath,
});
} }
}); });
} else { } else {
@ -182,11 +192,11 @@ export const serve_style = {
.replace('{style}', path.basename(styleFile, '.json')) .replace('{style}', path.basename(styleFile, '.json'))
.replace( .replace(
'{styleJsonFolder}', '{styleJsonFolder}',
path.relative(options.paths.sprites, path.dirname(styleFile)) path.relative(options.paths.sprites, path.dirname(styleFile)),
) ),
); );
styleJSON.sprite = `local://styles/${id}/sprite`; styleJSON.sprite = `local://styles/${id}/sprite`;
spritePaths.push({id: 'default', name: 'sprite', path: spritePath}); spritePaths.push({ id: 'default', name: 'sprite', path: spritePath });
} }
} }
} }