2024-07-24 22:35:18 +02:00
|
|
|
import re
|
|
|
|
|
2021-11-02 15:42:58 +01:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2024-07-24 22:35:18 +02:00
|
|
|
from django.http import HttpRequest, HttpResponseBase, HttpResponseForbidden
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2024-06-28 21:14:00 +02:00
|
|
|
from zerver.lib.typed_endpoint import typed_endpoint
|
2024-07-24 22:35:18 +02:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
from zerver.views.upload import serve_file
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-06-28 21:14:00 +02:00
|
|
|
@typed_endpoint
|
2021-02-12 08:19:30 +01:00
|
|
|
def backend_serve_thumbnail(
|
|
|
|
request: HttpRequest,
|
2024-07-12 02:30:23 +02:00
|
|
|
maybe_user_profile: UserProfile | AnonymousUser,
|
2024-06-28 21:14:00 +02:00
|
|
|
*,
|
|
|
|
url: str,
|
|
|
|
size: str,
|
2024-07-24 22:35:18 +02:00
|
|
|
) -> HttpResponseBase:
|
|
|
|
# This URL used to be passed arbitrary URLs, and pass them through
|
|
|
|
# Camo; we no longer support doing so, and instead return a 403.
|
|
|
|
#
|
|
|
|
# Modern thumbnailing uses URLs of the style
|
|
|
|
# `/user_uploads/thumbnail/.../300x200.webp`; this endpoint is
|
|
|
|
# kept for backward compatibility, and for future extension for
|
|
|
|
# thumbnailing external URLs.
|
|
|
|
upload_path_parts = re.match(r"user_uploads/(\d+)/(.*)", url)
|
|
|
|
if not upload_path_parts:
|
|
|
|
return HttpResponseForbidden()
|
|
|
|
|
|
|
|
realm_id_str = upload_path_parts[1]
|
|
|
|
path_id = upload_path_parts[2]
|
|
|
|
|
|
|
|
# We do not have ImageAttachment rows for historical uploads, so
|
|
|
|
# we cannot serve a "new" thumbnail for these requests; serve the
|
|
|
|
# full-size file.
|
|
|
|
return serve_file(request, maybe_user_profile, realm_id_str, path_id)
|