all tests passing

This commit is contained in:
acalcutt 2025-01-03 03:48:17 -05:00
parent 8bdd14a5e9
commit 32606113c0
2 changed files with 47 additions and 31 deletions

View file

@ -453,6 +453,8 @@ const respondImage = async (
overlay = null, overlay = null,
mode = 'tile', mode = 'tile',
) => { ) => {
console.log(lat);
console.log(lon);
if ( if (
Math.abs(lon) > 180 || Math.abs(lon) > 180 ||
Math.abs(lat) > 85.06 || Math.abs(lat) > 85.06 ||
@ -724,6 +726,7 @@ async function handleTileRequest(
* @param {string} req.params.format - The format of the image. * @param {string} req.params.format - The format of the image.
* @param {Function} next - Express next middleware function. * @param {Function} next - Express next middleware function.
* @param {number} maxScaleFactor - The maximum scale factor allowed. * @param {number} maxScaleFactor - The maximum scale factor allowed.
* @param verbose
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async function handleStaticRequest( async function handleStaticRequest(
@ -733,6 +736,7 @@ async function handleStaticRequest(
res, res,
next, next,
maxScaleFactor, maxScaleFactor,
verbose,
) { ) {
const { const {
id, id,
@ -742,6 +746,13 @@ async function handleStaticRequest(
scale: scaleParam, scale: scaleParam,
format, format,
} = req.params; } = req.params;
if (verbose) {
console.log(
`Handling static request for: /styles/${id}/static/${raw ? raw + '/' : ''}${staticType}${widthAndHeight ? '/' + widthAndHeight : ''}${
scaleParam ? '@' + scaleParam : ''
}.${format}`,
);
}
console.log(req.params); console.log(req.params);
const item = repo[id]; const item = repo[id];
@ -752,7 +763,6 @@ async function handleStaticRequest(
if (sizeMatch) { if (sizeMatch) {
const width = parseInt(sizeMatch[1], 10); const width = parseInt(sizeMatch[1], 10);
const height = parseInt(sizeMatch[2], 10); const height = parseInt(sizeMatch[2], 10);
if ( if (
isNaN(width) || isNaN(width) ||
isNaN(height) || isNaN(height) ||
@ -775,22 +785,22 @@ async function handleStaticRequest(
.status(400) .status(400)
.send('Invalid width or height provided in size parameter'); .send('Invalid width or height provided in size parameter');
} }
const scale = parseScale(scaleParam, maxScaleFactor); const scale = parseScale(scaleParam, maxScaleFactor);
let isRaw = raw === 'raw'; let isRaw = raw === 'raw';
if (!item || !staticType || !format || !scale) { const staticTypeMatch = staticType.match(staticTypeRegex);
if (!item || !format || !scale || !staticTypeMatch?.groups) {
return res.sendStatus(404); return res.sendStatus(404);
} }
const staticTypeMatch = staticType.match(staticTypeRegex);
if (staticTypeMatch.groups.lon) { if (staticTypeMatch.groups.lon) {
// Center Based Static Image // Center Based Static Image
const z = staticTypeMatch.groups.zoom; const z = parseFloat(staticTypeMatch.groups.zoom) || 0;
let x = staticTypeMatch.groups.lon; let x = parseFloat(staticTypeMatch.groups.lon) || 0;
let y = staticTypeMatch.groups.lat; let y = parseFloat(staticTypeMatch.groups.lat) || 0;
const bearing = staticTypeMatch.groups.bearing; const bearing = parseFloat(staticTypeMatch.groups.bearing) || 0;
const pitch = staticTypeMatch.groups.pitch; const pitch = parseInt(staticTypeMatch.groups.pitch) || 0;
if (z < 0) { if (z < 0) {
return res.status(404).send('Invalid zoom'); return res.status(404).send('Invalid zoom');
} }
@ -807,7 +817,6 @@ async function handleStaticRequest(
const paths = extractPathsFromQuery(req.query, transformer); const paths = extractPathsFromQuery(req.query, transformer);
const markers = extractMarkersFromQuery(req.query, options, transformer); const markers = extractMarkersFromQuery(req.query, options, transformer);
// prettier-ignore // prettier-ignore
const overlay = await renderOverlay( const overlay = await renderOverlay(
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
@ -819,12 +828,16 @@ async function handleStaticRequest(
); );
} else if (staticTypeMatch.groups.minx) { } else if (staticTypeMatch.groups.minx) {
// Area Based Static Image // Area Based Static Image
const bbox = [ const minx = parseFloat(staticTypeMatch.groups.minx) || 0;
+staticTypeMatch.groups.minx, const miny = parseFloat(staticTypeMatch.groups.miny) || 0;
+staticTypeMatch.groups.miny, const maxx = parseFloat(staticTypeMatch.groups.maxx) || 0;
+staticTypeMatch.groups.maxx, const maxy = parseFloat(staticTypeMatch.groups.maxy) || 0;
+staticTypeMatch.groups.maxx, if (isNaN(minx) || isNaN(miny) || isNaN(maxx) || isNaN(maxy)) {
]; return res
.status(400)
.send('Invalid bounding box provided in staticType parameter');
}
const bbox = [minx, miny, maxx, maxy];
let center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]; let center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
const transformer = isRaw const transformer = isRaw
@ -841,14 +854,18 @@ async function handleStaticRequest(
center = transformer(center); center = transformer(center);
} }
if (Math.abs(center[0]) > 180 || Math.abs(center[1]) > 85.06) {
return res.status(400).send('Invalid center');
}
const z = calcZForBBox(bbox, parsedWidth, parsedHeight, req.query); const z = calcZForBBox(bbox, parsedWidth, parsedHeight, req.query);
const x = center[0]; const x = center[0];
const y = center[1]; const y = center[1];
const bearing = 0; const bearing = 0;
const pitch = 0; const pitch = 0;
const paths = extractPathsFromQuery(req.query, transformer); const paths = extractPathsFromQuery(req.query, transformer);
const markers = extractMarkersFromQuery(req.query, options, transformer); const markers = extractMarkersFromQuery(req.query, options, transformer);
// prettier-ignore // prettier-ignore
const overlay = await renderOverlay( const overlay = await renderOverlay(
z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query, z, x, y, bearing, pitch, parsedWidth, parsedHeight, scale, paths, markers, req.query,
@ -921,7 +938,6 @@ async function handleStaticRequest(
return res.sendStatus(404); return res.sendStatus(404);
} }
} }
const existingFonts = {}; const existingFonts = {};
let maxScaleFactor = 2; let maxScaleFactor = 2;
@ -980,7 +996,7 @@ export const serve_rendered = {
res, res,
next, next,
maxScaleFactor, maxScaleFactor,
defailtTileSize, verbose,
); );
} }
return res.sendStatus(404); return res.sendStatus(404);

View file

@ -78,7 +78,7 @@ describe('Static endpoints', function () {
testStatic(prefix, '0,0,0/256x256', 'png', 404, 1); testStatic(prefix, '0,0,0/256x256', 'png', 404, 1);
testStatic(prefix, '0,0,-1/256x256', 'png', 404); testStatic(prefix, '0,0,-1/256x256', 'png', 404);
testStatic(prefix, '0,0,0/256.5x256.5', 'png', 404); testStatic(prefix, '0,0,0/256.5x256.5', 'png', 400);
testStatic(prefix, '0,0,0,/256x256', 'png', 404); testStatic(prefix, '0,0,0,/256x256', 'png', 404);
testStatic(prefix, '0,0,0,0,/256x256', 'png', 404); testStatic(prefix, '0,0,0,0,/256x256', 'png', 404);