Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
uploads: Add a method to copy attachment contents out.
Alex Vandivere408f069feaf
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.pyindex bb613939d54..8380c3d5588 100644--- a/zerver/lib/upload/__init__.py+++ b/zerver/lib/upload/__init__.py@@ -3,7 +3,7 @@ import loggingimport urllibfrom datetime import datetimefrom mimetypes import guess_type-from typing import IO, Any, Callable, Iterator, List, Optional, Tuple+from typing import IO, Any, BinaryIO, Callable, Iterator, List, Optional, Tuplefrom urllib.parse import urljoinfrom django.conf import settings@@ -107,6 +107,10 @@ def upload_message_attachment_from_request()+def save_attachment_contents(path_id: str, filehandle: BinaryIO) -> None:+ return upload_backend.save_attachment_contents(path_id, filehandle)++def delete_message_attachment(path_id: str) -> bool:return upload_backend.delete_message_attachment(path_id)diff --git a/zerver/lib/upload/base.py b/zerver/lib/upload/base.pyindex 77c33833523..8ff8f5d969f 100644--- a/zerver/lib/upload/base.py+++ b/zerver/lib/upload/base.py@@ -3,7 +3,7 @@ import osimport reimport unicodedatafrom datetime import datetime-from typing import IO, Any, Callable, Iterator, List, Optional, Tuple+from typing import IO, Any, BinaryIO, Callable, Iterator, List, Optional, Tuplefrom django.utils.translation import gettext as _from PIL import GifImagePlugin, Image, ImageOps, PngImagePlugin@@ -198,6 +198,9 @@ class ZulipUploadBackend:) -> str:raise NotImplementedError+ def save_attachment_contents(self, path_id: str, filehandle: BinaryIO) -> None:+ raise NotImplementedError+def delete_message_attachment(self, path_id: str) -> bool:raise NotImplementedErrordiff --git a/zerver/lib/upload/local.py b/zerver/lib/upload/local.pyindex 2d2e638ae92..d5b0cbb5967 100644--- a/zerver/lib/upload/local.py+++ b/zerver/lib/upload/local.py@@ -4,7 +4,7 @@ import randomimport secretsimport shutilfrom datetime import datetime-from typing import IO, Any, Callable, Iterator, Literal, Optional, Tuple+from typing import IO, Any, BinaryIO, Callable, Iterator, Literal, Optional, Tuplefrom django.conf import settings@@ -98,6 +98,9 @@ class LocalUploadBackend(ZulipUploadBackend):create_attachment(uploaded_file_name, path, user_profile, target_realm, uploaded_file_size)return "/user_uploads/" + path+ def save_attachment_contents(self, path_id: str, filehandle: BinaryIO) -> None:+ filehandle.write(read_local_file("files", path_id))+def delete_message_attachment(self, path_id: str) -> bool:return delete_local_file("files", path_id)diff --git a/zerver/lib/upload/s3.py b/zerver/lib/upload/s3.pyindex 7fc0c686fd8..af9e4848637 100644--- a/zerver/lib/upload/s3.py+++ b/zerver/lib/upload/s3.py@@ -4,7 +4,7 @@ import secretsimport urllibfrom datetime import datetimefrom mimetypes import guess_type-from typing import IO, Any, Callable, Iterator, List, Optional, Tuple+from typing import IO, Any, BinaryIO, Callable, Iterator, List, Optional, Tupleimport boto3import botocore@@ -231,6 +231,10 @@ class S3UploadBackend(ZulipUploadBackend):)return url+ def save_attachment_contents(self, path_id: str, filehandle: BinaryIO) -> None:+ for chunk in self.uploads_bucket.Object(path_id).get()["Body"]:+ filehandle.write(chunk)+def delete_message_attachment(self, path_id: str) -> bool:return self.delete_file_from_s3(path_id, self.uploads_bucket)diff --git a/zerver/tests/test_upload_local.py b/zerver/tests/test_upload_local.pyindex 2272cb8dd99..4fcb5cbb01e 100644--- a/zerver/tests/test_upload_local.py+++ b/zerver/tests/test_upload_local.py@@ -1,7 +1,7 @@import osimport reimport urllib-from io import StringIO+from io import BytesIO, StringIOfrom urllib.parse import urlparsefrom django.conf import settings@@ -20,6 +20,7 @@ from zerver.lib.upload import (delete_export_tarball,delete_message_attachment,delete_message_attachments,+ save_attachment_contents,upload_emoji_image,upload_export_tarball,upload_message_attachment,@@ -56,6 +57,17 @@ 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_save_attachment_contents(self) -> None:+ user_profile = self.example_user("hamlet")+ uri = upload_message_attachment(+ "dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile+ )++ path_id = re.sub("/user_uploads/", "", uri)+ output = BytesIO()+ save_attachment_contents(path_id, output)+ self.assertEqual(output.getvalue(), b"zulip!")+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 anotherdiff --git a/zerver/tests/test_upload_s3.py b/zerver/tests/test_upload_s3.pyindex 4b46aefd57e..a38ce5aaa38 100644--- a/zerver/tests/test_upload_s3.py+++ b/zerver/tests/test_upload_s3.py@@ -2,7 +2,7 @@ import ioimport osimport reimport urllib-from io import StringIO+from io import BytesIO, StringIOfrom unittest.mock import patchimport botocore.exceptions@@ -25,6 +25,7 @@ from zerver.lib.upload import (delete_export_tarball,delete_message_attachment,delete_message_attachments,+ save_attachment_contents,upload_export_tarball,upload_message_attachment,)@@ -67,6 +68,19 @@ class S3Test(ZulipTestCase):body = f"First message ...[zulip.txt](http://{user_profile.realm.host}{uri})"self.send_stream_message(self.example_user("hamlet"), "Denmark", body, "test")+ @use_s3_backend+ def test_save_attachment_contents(self) -> None:+ create_s3_buckets(settings.S3_AUTH_UPLOADS_BUCKET)+ user_profile = self.example_user("hamlet")+ uri = upload_message_attachment(+ "dummy.txt", len(b"zulip!"), "text/plain", b"zulip!", user_profile+ )++ path_id = re.sub("/user_uploads/", "", uri)+ output = BytesIO()+ save_attachment_contents(path_id, output)+ self.assertEqual(output.getvalue(), b"zulip!")+@use_s3_backenddef test_upload_message_attachment_s3_cross_realm_path(self) -> None:"""