mirror of
https://git.linux-kernel.at/oliver/ivatar.git
synced 2025-11-14 12:08:04 +00:00
We actually need to implement this via Middleware, as the Locale Middleware comes later in the process and hinders us from removing the header. Anyway, it's cleaner, since we're not duplicating code
This commit is contained in:
@@ -31,7 +31,7 @@ INSTALLED_APPS.extend(
|
|||||||
|
|
||||||
MIDDLEWARE.extend(
|
MIDDLEWARE.extend(
|
||||||
[
|
[
|
||||||
"django.middleware.locale.LocaleMiddleware",
|
"ivatar.middleware.RemoveVaryForImagesMiddleware",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
MIDDLEWARE.insert(
|
MIDDLEWARE.insert(
|
||||||
|
|||||||
@@ -4,6 +4,26 @@ Middleware classes
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from django.utils.deprecation import MiddlewareMixin
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
|
from django.middleware.locale import LocaleMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
class RemoveVaryForImagesMiddleware(LocaleMiddleware):
|
||||||
|
"""
|
||||||
|
Middleware that extends LocaleMiddleware to skip Vary header processing for image URLs
|
||||||
|
"""
|
||||||
|
|
||||||
|
def process_response(self, request, response):
|
||||||
|
# Check if this is an image-related URL
|
||||||
|
path = request.path
|
||||||
|
if any(
|
||||||
|
path.startswith(prefix)
|
||||||
|
for prefix in ["/avatar/", "/gravatarproxy/", "/blueskyproxy/"]
|
||||||
|
):
|
||||||
|
# Skip the parent's process_response to avoid adding Accept-Language to Vary
|
||||||
|
return response
|
||||||
|
|
||||||
|
# For all other URLs, use the parent's behavior
|
||||||
|
return super().process_response(request, response)
|
||||||
|
|
||||||
|
|
||||||
class MultipleProxyMiddleware(
|
class MultipleProxyMiddleware(
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ MIDDLEWARE = [
|
|||||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"django.middleware.locale.LocaleMiddleware",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = "ivatar.urls"
|
ROOT_URLCONF = "ivatar.urls"
|
||||||
|
|||||||
@@ -125,8 +125,6 @@ class AvatarImageView(TemplateView):
|
|||||||
reason=centry["reason"],
|
reason=centry["reason"],
|
||||||
charset=centry["charset"],
|
charset=centry["charset"],
|
||||||
)
|
)
|
||||||
# Remove Vary header for images since language doesn't matter
|
|
||||||
response["Vary"] = ""
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
# In case no digest at all is provided, return to home page
|
# In case no digest at all is provided, return to home page
|
||||||
@@ -297,8 +295,6 @@ class AvatarImageView(TemplateView):
|
|||||||
imgformat = "jpeg"
|
imgformat = "jpeg"
|
||||||
response = CachingHttpResponse(uri, data, content_type=f"image/{imgformat}")
|
response = CachingHttpResponse(uri, data, content_type=f"image/{imgformat}")
|
||||||
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
||||||
# Remove Vary header for images since language doesn't matter
|
|
||||||
response["Vary"] = ""
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _redirect_static_w_size(self, arg0, size):
|
def _redirect_static_w_size(self, arg0, size):
|
||||||
@@ -317,8 +313,6 @@ class AvatarImageView(TemplateView):
|
|||||||
data.seek(0)
|
data.seek(0)
|
||||||
response = CachingHttpResponse(uri, data, content_type="image/png")
|
response = CachingHttpResponse(uri, data, content_type="image/png")
|
||||||
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
||||||
# Remove Vary header for images since language doesn't matter
|
|
||||||
response["Vary"] = ""
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _return_cached_png(self, arg0, data, uri):
|
def _return_cached_png(self, arg0, data, uri):
|
||||||
@@ -414,8 +408,6 @@ class GravatarProxyView(View):
|
|||||||
data.read(), content_type=f"image/{file_format(img.format)}"
|
data.read(), content_type=f"image/{file_format(img.format)}"
|
||||||
)
|
)
|
||||||
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
||||||
# Remove Vary header for images since language doesn't matter
|
|
||||||
response["Vary"] = ""
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
@@ -535,8 +527,6 @@ class BlueskyProxyView(View):
|
|||||||
data.read(), content_type=f"image/{file_format(format)}"
|
data.read(), content_type=f"image/{file_format(format)}"
|
||||||
)
|
)
|
||||||
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
response["Cache-Control"] = "max-age=%i" % CACHE_IMAGES_MAX_AGE
|
||||||
# Remove Vary header for images since language doesn't matter
|
|
||||||
response["Vary"] = ""
|
|
||||||
return response
|
return response
|
||||||
except ValueError as exc:
|
except ValueError as exc:
|
||||||
print(f"Value error: {exc}")
|
print(f"Value error: {exc}")
|
||||||
|
|||||||
Reference in New Issue
Block a user