s3: Allow setting a CloudFront URL prefix for avatar and emoji images.

This commit is contained in:
Alex Vandiver 2024-06-18 17:19:17 +00:00 committed by Tim Abbott
parent 08b24484d1
commit 5cd10ce51d
4 changed files with 30 additions and 0 deletions

View File

@ -154,6 +154,15 @@ class S3UploadBackend(ZulipUploadBackend):
# hundreds of avatar URLs in single `GET /messages` request, # hundreds of avatar URLs in single `GET /messages` request,
# we instead back-compute the URL pattern here. # we instead back-compute the URL pattern here.
# The S3_AVATAR_PUBLIC_URL_PREFIX setting is used to override
# this prefix, for instance if a CloudFront distribution is
# used.
if settings.S3_AVATAR_PUBLIC_URL_PREFIX is not None:
prefix = settings.S3_AVATAR_PUBLIC_URL_PREFIX
if not prefix.endswith("/"):
prefix += "/"
return prefix
DUMMY_KEY = "dummy_key_ignored" DUMMY_KEY = "dummy_key_ignored"
# We do not access self.avatar_bucket.meta.client directly, # We do not access self.avatar_bucket.meta.client directly,

View File

@ -247,6 +247,25 @@ class S3Test(ZulipTestCase):
body = f"First message ...[zulip.txt](http://{hamlet.realm.host}" + url + ")" body = f"First message ...[zulip.txt](http://{hamlet.realm.host}" + url + ")"
self.send_stream_message(hamlet, "Denmark", body, "test") self.send_stream_message(hamlet, "Denmark", body, "test")
@use_s3_backend
def test_user_avatars_base(self) -> None:
backend = zerver.lib.upload.upload_backend
assert isinstance(backend, S3UploadBackend)
self.assertEqual(
backend.construct_public_upload_url_base(),
f"https://{settings.S3_AVATAR_BUCKET}.s3.amazonaws.com/",
)
with self.settings(S3_AVATAR_PUBLIC_URL_PREFIX="https://avatars.example.com"):
self.assertEqual(
backend.construct_public_upload_url_base(), "https://avatars.example.com/"
)
with self.settings(S3_AVATAR_PUBLIC_URL_PREFIX="https://avatars.example.com/"):
self.assertEqual(
backend.construct_public_upload_url_base(), "https://avatars.example.com/"
)
@use_s3_backend @use_s3_backend
def test_user_avatars_redirect(self) -> None: def test_user_avatars_redirect(self) -> None:
create_s3_buckets(settings.S3_AVATAR_BUCKET)[0] create_s3_buckets(settings.S3_AVATAR_BUCKET)[0]

View File

@ -161,6 +161,7 @@ S3_UPLOADS_STORAGE_CLASS: Literal[
"STANDARD", "STANDARD",
"STANDARD_IA", "STANDARD_IA",
] = "STANDARD" ] = "STANDARD"
S3_AVATAR_PUBLIC_URL_PREFIX: Optional[str] = None
LOCAL_UPLOADS_DIR: Optional[str] = None LOCAL_UPLOADS_DIR: Optional[str] = None
LOCAL_AVATARS_DIR: Optional[str] = None LOCAL_AVATARS_DIR: Optional[str] = None
LOCAL_FILES_DIR: Optional[str] = None LOCAL_FILES_DIR: Optional[str] = None

View File

@ -790,6 +790,7 @@ LOCAL_UPLOADS_DIR = "/home/zulip/uploads"
# S3_AVATAR_BUCKET = "" # S3_AVATAR_BUCKET = ""
# S3_REGION = None # S3_REGION = None
# S3_ENDPOINT_URL = None # S3_ENDPOINT_URL = None
# S3_AVATAR_PUBLIC_URL_PREFIX = None
# S3_ADDRESSING_STYLE = "auto" # S3_ADDRESSING_STYLE = "auto"
# S3_SKIP_PROXY = True # S3_SKIP_PROXY = True
# S3_UPLOADS_STORAGE_CLASS = "STANDARD" # S3_UPLOADS_STORAGE_CLASS = "STANDARD"