From 3eeece6a540f9ab6052a8f07eed02917cfc791fa Mon Sep 17 00:00:00 2001 From: HF Date: Tue, 1 Feb 2022 00:53:28 +0100 Subject: [PATCH] check if emptytile.png exists before sending it --- src/routes/tiles.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/routes/tiles.js b/src/routes/tiles.js index 4594cd4e..bb6dda99 100644 --- a/src/routes/tiles.js +++ b/src/routes/tiles.js @@ -2,11 +2,10 @@ * * Serve zoomlevel tiles * - * @flow */ +import fs from 'fs'; import express from 'express'; -import type { Request, Response } from 'express'; import { TILE_FOLDER } from '../core/config'; import { HOUR } from '../core/constants'; @@ -25,15 +24,25 @@ router.use('/', express.static(TILE_FOLDER, { * catch File Not Found: Send empty tile */ router.use('/:c([0-9]+)/:z([0-9]+)/:x([0-9]+)/:y([0-9]+).png', - async (req: Request, res: Response) => { + async (req, res) => { const { c: paramC } = req.params; const c = parseInt(paramC, 10); + + const filename = `${TILE_FOLDER}/${c}/emptytile.png`; + if (!fs.existsSync(filename)) { + res.set({ + 'Cache-Control': `public, s-maxage=${24 * 3600}, max-age=${24 * 3600}`, + }); + res.status(404).end(); + return; + } + res.set({ 'Cache-Control': `public, s-maxage=${2 * 3600}, max-age=${1 * 3600}`, 'Content-Type': 'image/png', }); res.status(200); - res.sendFile(`${TILE_FOLDER}/${c}/emptytile.png`); + res.sendFile(filename); });