Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
uploads: Pass target_realm down, so S3 can set correct metadata.
Alex Vandiver6bd89be5e1b9
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.pyindex 391779c7f51..7a3ada2ebd5 100644--- a/zerver/lib/upload/__init__.py+++ b/zerver/lib/upload/__init__.py@@ -215,6 +215,7 @@ def upload_message_attachment(content_type,file_data,user_profile,+ target_realm,)create_attachment(uploaded_file_name,diff --git a/zerver/lib/upload/base.py b/zerver/lib/upload/base.pyindex 7bf22450718..9e2970f602c 100644--- a/zerver/lib/upload/base.py+++ b/zerver/lib/upload/base.py@@ -37,6 +37,7 @@ class ZulipUploadBackend:content_type: str,file_data: bytes,user_profile: UserProfile | None,+ target_realm: Realm | None,) -> None:raise NotImplementedErrordiff --git a/zerver/lib/upload/local.py b/zerver/lib/upload/local.pyindex 6ab554d3505..7c0ae2070d9 100644--- a/zerver/lib/upload/local.py+++ b/zerver/lib/upload/local.py@@ -95,6 +95,7 @@ class LocalUploadBackend(ZulipUploadBackend):content_type: str,file_data: bytes,user_profile: UserProfile | None,+ target_realm: Realm | None,) -> None:write_local_file("files", path_id, file_data)diff --git a/zerver/lib/upload/s3.py b/zerver/lib/upload/s3.pyindex 953bad5ce66..c4d8f81fc97 100644--- a/zerver/lib/upload/s3.py+++ b/zerver/lib/upload/s3.py@@ -91,6 +91,7 @@ def upload_content_to_s3(cache_control: str | None = None,extra_metadata: dict[str, str] | None = None,filename: str | None = None,+ target_realm: Realm | None = None,) -> None:# Note that these steps are also replicated in# handle_upload_pre_finish_hook in zerver.views.tus, to update@@ -101,6 +102,8 @@ def upload_content_to_s3(if user_profile:metadata["user_profile_id"] = str(user_profile.id)metadata["realm_id"] = str(user_profile.realm_id)+ if target_realm:+ metadata["realm_id"] = str(target_realm.id)if extra_metadata is not None:metadata.update(extra_metadata)@@ -256,6 +259,7 @@ class S3UploadBackend(ZulipUploadBackend):content_type: str,file_data: bytes,user_profile: UserProfile | None,+ target_realm: Realm | None,) -> None:upload_content_to_s3(self.uploads_bucket,@@ -265,6 +269,7 @@ class S3UploadBackend(ZulipUploadBackend):file_data,storage_class=settings.S3_UPLOADS_STORAGE_CLASS,filename=filename,+ target_realm=target_realm,)@overridediff --git a/zerver/tests/test_thumbnail.py b/zerver/tests/test_thumbnail.pyindex e39998dbca3..b699448a074 100644--- a/zerver/tests/test_thumbnail.py+++ b/zerver/tests/test_thumbnail.py@@ -744,7 +744,7 @@ class TestStoreThumbnail(ZulipTestCase):hamlet = self.example_user("hamlet")path_id = upload_backend.generate_message_upload_path(str(hamlet.realm.id), "img.png")upload_backend.upload_message_attachment(- path_id, "img.png", "image/png", read_test_image_file("img.png"), hamlet+ path_id, "img.png", "image/png", read_test_image_file("img.png"), hamlet, hamlet.realm)source = attachment_source(path_id)create_attachment("img.png", path_id, "image/png", source, hamlet, hamlet.realm)diff --git a/zerver/tests/test_tusd.py b/zerver/tests/test_tusd.pyindex c8b5e9557c7..c523c4c5a2f 100644--- a/zerver/tests/test_tusd.py+++ b/zerver/tests/test_tusd.py@@ -282,9 +282,9 @@ class TusdPreCreateTest(ZulipTestCase):realm.custom_upload_quota_gb = 1realm.save(update_fields=["custom_upload_quota_gb"])- path_id = upload_message_attachment("zulip.txt", "text/plain", b"zulip!", hamlet)[- 0- ].removeprefix("/user_uploads/")+ path_id = upload_message_attachment(+ "zulip.txt", "text/plain", b"zulip!", hamlet, hamlet.realm+ )[0].removeprefix("/user_uploads/")attachment = Attachment.objects.get(path_id=path_id)attachment.size = assert_is_not_none(realm.upload_quota_bytes()) - 10attachment.save(update_fields=["size"])@@ -480,7 +480,12 @@ class TusdPreFinishTest(ZulipTestCase):str(hamlet.realm.id), sanitize_name("zulip.txt"))upload_backend.upload_message_attachment(- path_id, "zulip.txt", "text/plain", b"zulip!", hamlet+ path_id,+ "zulip.txt",+ "text/plain",+ b"zulip!",+ hamlet,+ hamlet.realm,)info = TusUpload(@@ -505,6 +510,7 @@ class TusdPreFinishTest(ZulipTestCase):"application/octet-stream",info.model_dump_json().encode(),hamlet,+ hamlet.realm,)# Post the hook saying the file is in place@@ -542,7 +548,9 @@ class TusdPreFinishTest(ZulipTestCase):path_id = upload_backend.generate_message_upload_path(str(hamlet.realm.id), sanitize_name(""))- upload_backend.upload_message_attachment(path_id, "", "ignored", b"zulip!", hamlet)+ upload_backend.upload_message_attachment(+ path_id, "", "ignored", b"zulip!", hamlet, hamlet.realm+ )info = TusUpload(id=path_id,@@ -561,6 +569,7 @@ class TusdPreFinishTest(ZulipTestCase):"ignored",info.model_dump_json().encode(),hamlet,+ hamlet.realm,)# Post the hook saying the file is in place@@ -688,7 +697,7 @@ class TusdPreTerminateTest(ZulipTestCase):str(hamlet.realm.id), sanitize_name("zulip.txt"))upload_backend.upload_message_attachment(- path_id, "zulip.txt", "text/plain", b"zulip!", hamlet+ path_id, "zulip.txt", "text/plain", b"zulip!", hamlet, hamlet.realm)info = TusUpload(diff --git a/zerver/tests/test_upload_s3.py b/zerver/tests/test_upload_s3.pyindex 73204cbc752..1bfc19b7b80 100644--- a/zerver/tests/test_upload_s3.py+++ b/zerver/tests/test_upload_s3.py@@ -130,7 +130,7 @@ class S3Test(ZulipTestCase):Verifies that the path of a file uploaded by a cross-realm bot to anotherrealm is correct."""- create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)+ bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]internal_realm = get_realm(settings.SYSTEM_BOT_REALM)zulip_realm = get_realm("zulip")@@ -143,6 +143,11 @@ class S3Test(ZulipTestCase):# Ensure the correct realm id of the target realm is used instead of the bot's realm.self.assertTrue(url.startswith(f"/user_uploads/{zulip_realm.id}/"))+ path_id = re.sub(r"/user_uploads/", "", url)+ s3_obj = bucket.Object(path_id)+ s3_obj.load()+ self.assertEqual(s3_obj.metadata["realm_id"], str(zulip_realm.id))+@use_s3_backenddef test_delete_message_attachment(self) -> None:bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]diff --git a/zerver/worker/thumbnail.py b/zerver/worker/thumbnail.pyindex 7101258240a..8fcda75d3bc 100644--- a/zerver/worker/thumbnail.py+++ b/zerver/worker/thumbnail.py@@ -116,6 +116,7 @@ def ensure_thumbnails(image_attachment: ImageAttachment) -> int:content_type,thumbnailed_bytes,None,+ None,)height = resized.get("page-height") if thumbnail_format.animated else resized.heightimage_attachment.thumbnail_metadata.append(