check if emptytile.png exists before sending it

This commit is contained in:
HF 2022-02-01 00:53:28 +01:00
parent 082cc92b65
commit 3eeece6a54

View File

@ -2,11 +2,10 @@
* *
* Serve zoomlevel tiles * Serve zoomlevel tiles
* *
* @flow
*/ */
import fs from 'fs';
import express from 'express'; import express from 'express';
import type { Request, Response } from 'express';
import { TILE_FOLDER } from '../core/config'; import { TILE_FOLDER } from '../core/config';
import { HOUR } from '../core/constants'; import { HOUR } from '../core/constants';
@ -25,15 +24,25 @@ router.use('/', express.static(TILE_FOLDER, {
* catch File Not Found: Send empty tile * catch File Not Found: Send empty tile
*/ */
router.use('/:c([0-9]+)/:z([0-9]+)/:x([0-9]+)/:y([0-9]+).png', 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: paramC } = req.params;
const c = parseInt(paramC, 10); 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({ res.set({
'Cache-Control': `public, s-maxage=${2 * 3600}, max-age=${1 * 3600}`, 'Cache-Control': `public, s-maxage=${2 * 3600}, max-age=${1 * 3600}`,
'Content-Type': 'image/png', 'Content-Type': 'image/png',
}); });
res.status(200); res.status(200);
res.sendFile(`${TILE_FOLDER}/${c}/emptytile.png`); res.sendFile(filename);
}); });