From b1e1d72f256187a17a740a9ca03323c2eb0743dc Mon Sep 17 00:00:00 2001 From: acalcutt Date: Sun, 5 Jan 2025 00:50:13 -0500 Subject: [PATCH] Update serve_style.js --- src/serve_style.js | 68 ++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/src/serve_style.js b/src/serve_style.js index 5dcdbe5..2d43415 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -91,36 +91,56 @@ export const serve_style = { }); /** - * Handles requests for a font file. - * @param {object} req - Express request object. - * @param {object} res - Express response object. - * @param {string} req.params.fontstack - Name of the font stack. - * @param {string} req.params.range - The range of the font (e.g. 0-255). + * Handles GET requests for sprite images and JSON files. + * @param {express.Request} req - Express request object. + * @param {express.Response} res - Express response object. + * @param {express.NextFunction} next - Express next function. + * @param {string} req.params.id - ID of the sprite. + * @param {string} [req.params.spriteID='default'] - ID of the specific sprite image, defaults to 'default'. + * @param {string} [req.params.scale] - Scale of the sprite image, defaults to ''. + * @param {string} req.params.format - Format of the sprite file, 'png' or 'json'. * @returns {Promise} */ - app.get('/fonts/:fontstack/:range.pbf', async (req, res) => { + app.get(`/:id/sprite{/:spriteID}{@:scale}{.:format}`, (req, res, next) => { if (verbose) { - console.log(req.params); - } - const fontstack = decodeURI(req.params.fontstack); - const range = req.params.range; + const { spriteID = 'default', id, format } = req.params; + const spriteScale = allowedSpriteScales(req.params.scale); - try { - const concatenated = await getFontsPbf( - options.serveAllFonts ? null : allowedFonts, - fontPath, - fontstack, - range, - existingFonts, + console.log( + `Handling sprite request for: /styles/%s/sprite/%s%s%s`, + String(id).replace(/\n|\r/g, ''), + String(spriteID).replace(/\n|\r/g, ''), + String(spriteScale).replace(/\n|\r/g, ''), + String(format).replace(/\n|\r/g, ''),, ); - - res.header('Content-type', 'application/x-protobuf'); - res.header('Last-Modified', lastModified); - return res.send(concatenated); - } catch (err) { - console.error('Error serving font:', err); - return res.status(400).header('Content-Type', 'text/plain').send(err); } + + const item = repo[id]; + if (!item || !allowedSpriteFormats(format)) { + return res.sendStatus(404); + } + + const sprite = item.spritePaths.find((sprite) => sprite.id === spriteID); + if (!sprite) { + return res.status(400).send('Bad Sprite ID or Scale'); + } + + const filename = `${sprite.path}${spriteScale}.${format}`; + + // eslint-disable-next-line security/detect-non-literal-fs-filename + fs.readFile(filename, (err, data) => { + if (err) { + console.error('Sprite load error: %s, Error: %s', filename, err); + return res.sendStatus(404); + } + + if (format === 'json') { + res.header('Content-type', 'application/json'); + } else if (format === 'png') { + res.header('Content-type', 'image/png'); + } + return res.send(data); + }); }); return app;