From 74326f67de6d9c3ed9a235d4d31cc863e510d8b1 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 18 May 2023 00:14:32 +0200 Subject: usertile: return empty tile instead of 404 --- fietsboek/views/profile.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/fietsboek/views/profile.py b/fietsboek/views/profile.py index fcb08ee..c7f932d 100644 --- a/fietsboek/views/profile.py +++ b/fietsboek/views/profile.py @@ -16,6 +16,31 @@ from .. import models, util from ..data import UserDataDir from ..models.track import TrackType, TrackWithMetadata +# A well-made transparent tile is actually pretty small (only 116 bytes), which +# is even smaller than our HTTP 404 page. So not only is it more efficient +# (bandwidth-wise) to transfer the transparent PNG, it also means that we can +# set cache headers, which the client will honor. +# +# Since the tile is so small, we've embedded it right here in the source. +# +# The tile is taken and adapted from hittekaart, which in turn took inspiration +# from https://www.mjt.me.uk/posts/smallest-png/ and +# http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html +# fmt: off +EMPTY_TILE = bytes([ + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, + 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, + 0x01, 0x03, 0x00, 0x00, 0x00, 0x66, 0xbc, 0x3a, 0x25, 0x00, 0x00, 0x00, + 0x03, 0x50, 0x4c, 0x54, 0x45, 0x00, 0xff, 0x00, 0x34, 0x5e, 0xc0, 0xa8, + 0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8, + 0x66, 0x00, 0x00, 0x00, 0x1f, 0x49, 0x44, 0x41, 0x54, 0x68, 0x81, 0xed, + 0xc1, 0x01, 0x0d, 0x00, 0x00, 0x00, 0xc2, 0xa0, 0xf7, 0x4f, 0x6d, 0x0e, + 0x37, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x0d, + 0x21, 0x00, 0x00, 0x01, 0x9a, 0x60, 0xe1, 0xd5, 0x00, 0x00, 0x00, 0x00, + 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, +]) +# fmt: on + @dataclass class CumulativeStats: @@ -199,7 +224,7 @@ def user_tile(request: Request) -> Response: ) result = result.fetchone() if result is None: - return HTTPNotFound() + return Response(EMPTY_TILE, content_type="image/png") return Response(result[0], content_type="image/png") -- cgit v1.2.3