Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

uploads: Move charset-detection before S3 upload, so it appears there.

Alex Vandiver005514739a82
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.py
index 536dc3316fc..428a2d404e1 100644
--- a/zerver/lib/upload/__init__.py
+++ b/zerver/lib/upload/__init__.py
@@ -104,7 +104,6 @@ def create_attachment(
file_size = file_data.size
file_vips_data = file_data.vips_source
- content_type = maybe_add_charset(content_type, file_data)
attachment = Attachment.objects.create(
file_name=file_name,
path_id=path_id,
@@ -207,6 +206,7 @@ def upload_message_attachment(
path_id = upload_backend.generate_message_upload_path(
str(target_realm.id), sanitize_name(uploaded_file_name)
)
+ content_type = maybe_add_charset(content_type, file_data)
with transaction.atomic(durable=True):
upload_backend.upload_message_attachment(
diff --git a/zerver/tests/test_tusd.py b/zerver/tests/test_tusd.py
index c523c4c5a2f..76e06c62fba 100644
--- a/zerver/tests/test_tusd.py
+++ b/zerver/tests/test_tusd.py
@@ -672,6 +672,56 @@ class TusdPreFinishTest(ZulipTestCase):
response = bucket.Object(f"{path_id}.info").get()
self.assertEqual(response["ContentType"], "binary/octet-stream")
+ @use_s3_backend
+ def test_s3_upload_chardet(self) -> None:
+ assert settings.LOCAL_FILES_DIR is None
+ self.login("hamlet")
+ hamlet = self.example_user("hamlet")
+ bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]
+ upload_backend = S3UploadBackend()
+
+ filename = "text-file.txt"
+ path_id = upload_backend.generate_message_upload_path(
+ str(hamlet.realm.id), sanitize_name(filename, strict=True)
+ )
+ content = "zulip!"
+ info = TusUpload(
+ id=path_id,
+ size=len(content),
+ offset=0,
+ size_is_deferred=False,
+ meta_data={
+ "filename": filename,
+ "filetype": "text/plain",
+ "name": filename,
+ "type": "text/plain",
+ },
+ is_final=False,
+ is_partial=False,
+ partial_uploads=None,
+ storage=None,
+ )
+ bucket.Object(path_id).put(
+ Body=content.encode(),
+ ContentType="application/octet-stream",
+ Metadata={k: v.encode("ascii", "replace").decode() for k, v in info.meta_data.items()},
+ )
+ bucket.Object(f"{path_id}.info").put(
+ Body=info.model_dump_json().encode(),
+ )
+ # Post the hook saying the file is in place
+ result = self.client_post(
+ "/api/internal/tusd",
+ self.request(info).model_dump(),
+ content_type="application/json",
+ )
+ self.assertEqual(result.status_code, 200)
+
+ attachment = Attachment.objects.get(path_id=path_id)
+ self.assertEqual(attachment.size, len(content))
+ self.assertEqual(attachment.content_type, 'text/plain; charset="ascii"')
+ self.assertEqual(bucket.Object(path_id).get()["ContentType"], 'text/plain; charset="ascii"')
+
class TusdPreTerminateTest(ZulipTestCase):
def request(self, info: TusUpload) -> TusHook:
diff --git a/zerver/tests/test_upload_s3.py b/zerver/tests/test_upload_s3.py
index 1bfc19b7b80..094090e9d45 100644
--- a/zerver/tests/test_upload_s3.py
+++ b/zerver/tests/test_upload_s3.py
@@ -338,6 +338,26 @@ class S3Test(ZulipTestCase):
key = url.removeprefix("/user_uploads/")
self.assertEqual(b"", bucket.Object(key).get()["Body"].read())
+ @use_s3_backend
+ def test_user_uploads_text_charset(self) -> None:
+ bucket = create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)[0]
+
+ self.login("hamlet")
+ fp = BytesIO("नाम में क्या रक्खा हे".encode())
+ fp.name = "zulip.txt"
+
+ result = self.client_post("/json/user_uploads", {"file": fp})
+ response_dict = self.assert_json_success(result)
+ self.assertIn("url", response_dict)
+ url = response_dict["url"]
+
+ path_id = re.sub(r"/user_uploads/", "", url)
+ s3_obj = bucket.Object(path_id)
+ s3_obj.load()
+ attachment_obj = Attachment.objects.get(path_id=path_id)
+ self.assertEqual(attachment_obj.content_type, 'text/plain; charset="utf-8"')
+ self.assertEqual(s3_obj.content_type, 'text/plain; charset="utf-8"')
+
@use_s3_backend
def test_user_avatars_base(self) -> None:
backend = zerver.lib.upload.upload_backend
diff --git a/zerver/views/tusd.py b/zerver/views/tusd.py
index 309a7145050..055e1ed638e 100644
--- a/zerver/views/tusd.py
+++ b/zerver/views/tusd.py
@@ -23,6 +23,7 @@ from zerver.lib.upload import (
check_upload_within_quota,
create_attachment,
delete_message_attachment,
+ maybe_add_charset,
sanitize_name,
upload_backend,
)
@@ -154,6 +155,8 @@ def handle_upload_pre_finish_hook(
content_type = guess_type(filename)[0]
if content_type is None:
content_type = "application/octet-stream"
+ file_data = attachment_source(path_id)
+ content_type = maybe_add_charset(content_type, file_data)
if settings.LOCAL_UPLOADS_DIR is None:
# We "copy" the file to itself to update the Content-Type,
@@ -200,7 +203,7 @@ def handle_upload_pre_finish_hook(
filename,
path_id,
content_type,
- attachment_source(path_id),
+ file_data,
user_profile,
user_profile.realm,
)