aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-05-18 00:14:32 +0200
committerDaniel Schadt <kingdread@gmx.de>2023-05-18 00:14:32 +0200
commit74326f67de6d9c3ed9a235d4d31cc863e510d8b1 (patch)
tree404e1e998490572f7a3e9bfaf013701ce640778d
parent5aedaf987162072ce4ce1a68d80b89f151520ed5 (diff)
downloadfietsboek-74326f67de6d9c3ed9a235d4d31cc863e510d8b1.tar.gz
fietsboek-74326f67de6d9c3ed9a235d4d31cc863e510d8b1.tar.bz2
fietsboek-74326f67de6d9c3ed9a235d4d31cc863e510d8b1.zip
usertile: return empty tile instead of 404
-rw-r--r--fietsboek/views/profile.py27
1 files changed, 26 insertions, 1 deletions
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")