upload: Rename upload_message_file to use word "attachment".

For consistency with the table, which is named Attachment.
This commit is contained in:
Alex Vandiver 2023-02-28 02:46:41 +00:00 committed by Tim Abbott
parent 96aca49f9f
commit 567d1d54e7
11 changed files with 43 additions and 41 deletions

View File

@ -1100,7 +1100,7 @@ def process_message_files(
def get_attachment_path_and_content(fileinfo: ZerverFieldsT, realm_id: int) -> Tuple[str, str]:
# Should be kept in sync with its equivalent in zerver/lib/uploads in the function
# 'upload_message_file'
# 'upload_message_attachment'
s3_path = "/".join(
[
str(realm_id),

View File

@ -26,7 +26,7 @@ from zerver.lib.queue import queue_json_publish
from zerver.lib.rate_limiter import RateLimitedObject
from zerver.lib.send_email import FromAddress
from zerver.lib.string_validation import is_character_printable
from zerver.lib.upload import upload_message_file
from zerver.lib.upload import upload_message_attachment
from zerver.models import (
Message,
MissedMessageEmailAddress,
@ -337,7 +337,7 @@ def extract_and_upload_attachments(message: EmailMessage, realm: Realm, sender:
if filename:
attachment = part.get_payload(decode=True)
if isinstance(attachment, bytes):
s3_url = upload_message_file(
s3_url = upload_message_attachment(
filename,
len(attachment),
content_type,

View File

@ -103,7 +103,7 @@ def upload_emoji_image(
return upload_backend.upload_emoji_image(emoji_file, emoji_file_name, user_profile)
def upload_message_file(
def upload_message_attachment(
uploaded_file_name: str,
uploaded_file_size: int,
content_type: Optional[str],
@ -111,7 +111,7 @@ def upload_message_file(
user_profile: UserProfile,
target_realm: Optional[Realm] = None,
) -> str:
return upload_backend.upload_message_file(
return upload_backend.upload_message_attachment(
uploaded_file_name,
uploaded_file_size,
content_type,
@ -140,7 +140,7 @@ def upload_message_image_from_request(
user_file: UploadedFile, user_profile: UserProfile, user_file_size: int
) -> str:
uploaded_file_name, content_type = get_file_info(user_file)
return upload_message_file(
return upload_message_attachment(
uploaded_file_name, user_file_size, content_type, user_file.read(), user_profile
)

View File

@ -191,7 +191,7 @@ class ZulipUploadBackend:
def generate_message_upload_path(self, realm_id: str, uploaded_file_name: str) -> str:
raise NotImplementedError
def upload_message_file(
def upload_message_attachment(
self,
uploaded_file_name: str,
uploaded_file_size: int,

View File

@ -78,7 +78,7 @@ class LocalUploadBackend(ZulipUploadBackend):
]
)
def upload_message_file(
def upload_message_attachment(
self,
uploaded_file_name: str,
uploaded_file_size: int,

View File

@ -203,7 +203,7 @@ class S3UploadBackend(ZulipUploadBackend):
]
)
def upload_message_file(
def upload_message_attachment(
self,
uploaded_file_name: str,
uploaded_file_size: int,

View File

@ -18,7 +18,7 @@ from zerver.actions.realm_playgrounds import do_add_realm_playground
from zerver.lib.events import do_events_register
from zerver.lib.initial_password import initial_password
from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.upload import upload_message_file
from zerver.lib.upload import upload_message_attachment
from zerver.lib.users import get_api_key
from zerver.models import Client, Message, UserGroup, UserPresence, get_realm, get_user
@ -345,7 +345,9 @@ def deactivate_own_user() -> Dict[str, object]:
@openapi_param_value_generator(["/attachments/{attachment_id}:delete"])
def remove_attachment() -> Dict[str, object]:
user_profile = helpers.example_user("iago")
url = upload_message_file("dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile)
url = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
attachment_id = url.replace("/user_uploads/", "").split("/")[0]
return {"attachment_id": attachment_id}

View File

@ -601,11 +601,11 @@ class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
incoming_valid_message["Reply-to"] = self.example_email("othello")
with mock.patch(
"zerver.lib.email_mirror.upload_message_file",
"zerver.lib.email_mirror.upload_message_attachment",
return_value="https://test_url",
) as upload_message_file:
) as upload_message_attachment:
process_message(incoming_valid_message)
upload_message_file.assert_called_with(
upload_message_attachment.assert_called_with(
"image.png",
len(image_bytes),
"image/png",
@ -728,11 +728,11 @@ class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
incoming_valid_message["Reply-to"] = self.example_email("othello")
with mock.patch(
"zerver.lib.email_mirror.upload_message_file",
"zerver.lib.email_mirror.upload_message_attachment",
return_value="https://test_url",
) as upload_message_file:
) as upload_message_attachment:
process_message(incoming_valid_message)
upload_message_file.assert_called_with(
upload_message_attachment.assert_called_with(
utf8_filename,
len(image_bytes),
"image/png",
@ -775,11 +775,11 @@ class TestEmailMirrorMessagesWithAttachments(ZulipTestCase):
incoming_valid_message["Reply-to"] = self.example_email("othello")
with mock.patch(
"zerver.lib.email_mirror.upload_message_file",
"zerver.lib.email_mirror.upload_message_attachment",
return_value="https://test_url",
) as upload_message_file:
) as upload_message_attachment:
process_message(incoming_valid_message)
upload_message_file.assert_called_with(
upload_message_attachment.assert_called_with(
"image.png",
len(image_bytes),
"image/png",

View File

@ -48,7 +48,7 @@ from zerver.lib.test_helpers import (
read_test_image_file,
use_s3_backend,
)
from zerver.lib.upload import claim_attachment, upload_avatar_image, upload_message_file
from zerver.lib.upload import claim_attachment, upload_avatar_image, upload_message_attachment
from zerver.lib.user_topics import add_topic_mute
from zerver.lib.utils import assert_is_not_none
from zerver.models import (
@ -154,7 +154,7 @@ class ExportFile(ZulipTestCase):
self, user_profile: UserProfile, *, emoji_name: str = "whatever"
) -> None:
message = most_recent_message(user_profile)
url = upload_message_file(
url = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
attachment_path_id = url.replace("/user_uploads/", "")
@ -355,7 +355,7 @@ class RealmImportExportTest(ExportFile):
# We create an attachment tied to a personal message. That means it shouldn't be
# included in a public export, as it's private data.
personal_message_id = self.send_personal_message(user_profile, self.example_user("othello"))
url = upload_message_file(
url = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
attachment_path_id = url.replace("/user_uploads/", "")

View File

@ -19,7 +19,7 @@ from zerver.lib.transfer import (
transfer_message_files_to_s3,
transfer_uploads_to_s3,
)
from zerver.lib.upload import upload_message_file
from zerver.lib.upload import upload_message_attachment
from zerver.lib.upload.base import resize_emoji
from zerver.models import Attachment, RealmEmoji
@ -67,8 +67,8 @@ class TransferUploadsToS3Test(ZulipTestCase):
hamlet = self.example_user("hamlet")
othello = self.example_user("othello")
upload_message_file("dummy1.txt", len(b"zulip1!"), "text/plain", b"zulip1!", hamlet)
upload_message_file("dummy2.txt", len(b"zulip2!"), "text/plain", b"zulip2!", othello)
upload_message_attachment("dummy1.txt", len(b"zulip1!"), "text/plain", b"zulip1!", hamlet)
upload_message_attachment("dummy2.txt", len(b"zulip2!"), "text/plain", b"zulip2!", othello)
with self.assertLogs(level="INFO"):
transfer_message_files_to_s3(1)

View File

@ -50,7 +50,7 @@ from zerver.lib.upload import (
delete_message_image,
upload_emoji_image,
upload_export_tarball,
upload_message_file,
upload_message_attachment,
)
from zerver.lib.upload.base import (
DEFAULT_AVATAR_SIZE,
@ -1844,9 +1844,9 @@ class RealmNightLogoTest(RealmLogoTest):
class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
def test_file_upload_local(self) -> None:
def test_upload_message_attachment_local(self) -> None:
user_profile = self.example_user("hamlet")
uri = upload_message_file(
uri = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
@ -1861,7 +1861,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
uploaded_file = Attachment.objects.get(owner=user_profile, path_id=path_id)
self.assert_length(b"zulip!", uploaded_file.size)
def test_file_upload_local_cross_realm_path(self) -> None:
def test_upload_message_attachment_local_cross_realm_path(self) -> None:
"""
Verifies that the path of a file uploaded by a cross-realm bot to another
realm is correct.
@ -1872,7 +1872,7 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT, internal_realm.id)
self.assertEqual(user_profile.realm, internal_realm)
uri = upload_message_file(
uri = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile, zulip_realm
)
# Ensure the correct realm id of the target realm is used instead of the bot's realm.
@ -2039,11 +2039,11 @@ class LocalStorageTest(UploadSerializeMixin, ZulipTestCase):
class S3Test(ZulipTestCase):
@use_s3_backend
def test_file_upload_s3(self) -> None:
def test_upload_message_attachment_s3(self) -> None:
bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]
user_profile = self.example_user("hamlet")
uri = upload_message_file(
uri = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
@ -2061,7 +2061,7 @@ class S3Test(ZulipTestCase):
self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test")
@use_s3_backend
def test_file_upload_s3_cross_realm_path(self) -> None:
def test_upload_message_attachment_s3_cross_realm_path(self) -> None:
"""
Verifies that the path of a file uploaded by a cross-realm bot to another
realm is correct.
@ -2073,18 +2073,18 @@ class S3Test(ZulipTestCase):
user_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT, internal_realm.id)
self.assertEqual(user_profile.realm, internal_realm)
uri = upload_message_file(
uri = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile, zulip_realm
)
# Ensure the correct realm id of the target realm is used instead of the bot's realm.
self.assertTrue(uri.startswith(f"/user_uploads/{zulip_realm.id}/"))
@use_s3_backend
def test_file_upload_s3_with_undefined_content_type(self) -> None:
def test_upload_message_attachment_s3_with_undefined_content_type(self) -> None:
bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]
user_profile = self.example_user("hamlet")
uri = upload_message_file("dummy.txt", len(b"zulip!"), None, b"zulip!", user_profile)
uri = upload_message_attachment("dummy.txt", len(b"zulip!"), None, b"zulip!", user_profile)
path_id = re.sub("/user_uploads/", "", uri)
self.assertEqual(b"zulip!", bucket.Object(path_id).get()["Body"].read())
@ -2096,7 +2096,7 @@ class S3Test(ZulipTestCase):
create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)
user_profile = self.example_user("hamlet")
uri = upload_message_file(
uri = upload_message_attachment(
"dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile
)
@ -2115,7 +2115,7 @@ class S3Test(ZulipTestCase):
)
@use_s3_backend
def test_file_upload_authed(self) -> None:
def test_user_uploads_authed(self) -> None:
"""
A call to /json/user_uploads should return a uri and actually create an object.
"""
@ -2507,14 +2507,14 @@ class UploadSpaceTests(UploadSerializeMixin, ZulipTestCase):
self.assertEqual(0, cache_get(get_realm_used_upload_space_cache_key(self.realm))[0])
data = b"zulip!"
upload_message_file("dummy.txt", len(data), "text/plain", data, self.user_profile)
upload_message_attachment("dummy.txt", len(data), "text/plain", data, self.user_profile)
# notify_attachment_update function calls currently_used_upload_space_bytes which
# updates the cache.
self.assert_length(data, cache_get(get_realm_used_upload_space_cache_key(self.realm))[0])
self.assert_length(data, self.realm.currently_used_upload_space_bytes())
data2 = b"more-data!"
upload_message_file("dummy2.txt", len(data2), "text/plain", data2, self.user_profile)
upload_message_attachment("dummy2.txt", len(data2), "text/plain", data2, self.user_profile)
self.assertEqual(
len(data) + len(data2), cache_get(get_realm_used_upload_space_cache_key(self.realm))[0]
)