From 433968cbb69852e603dbc12345d87576ef081530 Mon Sep 17 00:00:00 2001 From: PieterCK Date: Tue, 22 Oct 2024 17:14:45 +0700 Subject: [PATCH] storage: Reformat hashed medium static avatar files. This commit reformats hashed medium static avatar files("-medium.png" files). Unlike user-uploaded avatar files, avatars served alongside static files are hashed by Django. To implement the URL patterns for finding medium-size avatar URLs that are hardcoded into current versions of the mobile apps, the medium avatar files need to be adjusted to include the "-medium" string just before the file type. For example, the URL format will now be: Before: welcome-bot-medium.123123321.png After: welcome-bot.123123123-medium.png --- zerver/lib/storage.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/zerver/lib/storage.py b/zerver/lib/storage.py index abd7b1d06f..0465ae69a8 100644 --- a/zerver/lib/storage.py +++ b/zerver/lib/storage.py @@ -24,6 +24,25 @@ else: return os.path.join(settings.STATIC_ROOT, path) +def reformat_medium_filename(hashed_name: str) -> str: + """ + Because the protocol for getting medium-size avatar URLs + was never fully documented, the mobile apps use a + substitution of the form s/.png/-medium.png/ to get the + medium-size avatar URLs. Thus, we must ensure the hashed + filenames for system bot avatars follow this naming convention. + """ + + name_parts = hashed_name.rsplit(".", 1) + base_name = name_parts[0] + + if len(name_parts) != 2 or "-medium" not in base_name: + return hashed_name + extension = name_parts[1].replace("png", "medium.png") + base_name = base_name.replace("-medium", "") + return f"{base_name}-{extension}" + + class IgnoreBundlesManifestStaticFilesStorage(ManifestStaticFilesStorage): @override def hashed_name( @@ -45,7 +64,11 @@ class IgnoreBundlesManifestStaticFilesStorage(ManifestStaticFilesStorage): # so they can hit our Nginx caching block for static files. # We don't need to worry about stale caches since these are # only used by the system bots. - return super().hashed_name(name, content, filename) + if not name.endswith("-medium.png"): + return super().hashed_name(name, content, filename) + + hashed_medium_file = super().hashed_name(name, content, filename) + return reformat_medium_filename(hashed_medium_file) if name == "generated/emoji/emoji_api.json": # Unlike most .json files, we do want to hash this file; # its hashed URL is returned as part of the API. See