diff --git a/zerver/lib/upload.py b/zerver/lib/upload.py index 1f249b19e7..d60c3769ac 100644 --- a/zerver/lib/upload.py +++ b/zerver/lib/upload.py @@ -284,8 +284,6 @@ def check_upload_within_quota(realm: Realm, uploaded_file_size: int) -> None: def get_file_info(request: HttpRequest, user_file: File) -> Tuple[str, int, Optional[str]]: uploaded_file_name = user_file.name - assert isinstance(uploaded_file_name, str) - content_type = request.GET.get('mimetype') if content_type is None: guessed_type = guess_type(uploaded_file_name)[0] diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.py index a247381d16..e25f247f82 100644 --- a/zerver/tests/test_upload.py +++ b/zerver/tests/test_upload.py @@ -39,7 +39,6 @@ from zerver.lib.actions import ( from zerver.lib.cache import get_realm_used_upload_space_cache_key, cache_get from zerver.lib.create_user import copy_user_settings from zerver.lib.users import get_api_key -from zerver.views.upload import upload_file_backend import urllib import ujson @@ -131,43 +130,6 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase): uri = result.json()["uri"] self.assertTrue(uri.endswith("pasted_file.png")) - def test_filename_encoding(self) -> None: - """ - In Python 2, we need to encode unicode filenames (which converts them to - str) before they can be rendered correctly. However, in Python 3, the - separate unicode type does not exist, and we don't need to perform this - encoding. This test ensures that we handle filename encodings properly, - and does so in a way that preserves 100% test coverage for Python 3. - """ - - user_profile = self.example_user('hamlet') - - mock_file = mock.Mock() - mock_file._get_size = mock.Mock(return_value=1024) - - mock_files = mock.Mock() - mock_files.__len__ = mock.Mock(return_value=1) - mock_files.values = mock.Mock(return_value=[mock_file]) - - mock_request = mock.Mock() - mock_request.FILES = mock_files - - # str filenames should not be encoded. - mock_filename = mock.Mock(spec=str) - mock_file.name = mock_filename - with mock.patch('zerver.views.upload.upload_message_image_from_request'): - result = upload_file_backend(mock_request, user_profile) - self.assert_json_success(result) - mock_filename.encode.assert_not_called() - - # Non-str filenames should be encoded. - mock_filename = mock.Mock(spec=None) # None is not str - mock_file.name = mock_filename - with mock.patch('zerver.views.upload.upload_message_image_from_request'): - result = upload_file_backend(mock_request, user_profile) - self.assert_json_success(result) - mock_filename.encode.assert_called_once_with('ascii') - def test_file_too_big_failure(self) -> None: """ Attempting to upload big files should fail. diff --git a/zerver/views/upload.py b/zerver/views/upload.py index affc06d437..59146bcdfc 100644 --- a/zerver/views/upload.py +++ b/zerver/views/upload.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from django.http import HttpRequest, HttpResponse, HttpResponseForbidden, \ HttpResponseNotFound from django.shortcuts import redirect @@ -73,30 +71,5 @@ def upload_file_backend(request: HttpRequest, user_profile: UserProfile) -> Http settings.MAX_FILE_UPLOAD_SIZE)) check_upload_within_quota(user_profile.realm, file_size) - if not isinstance(user_file.name, str): - # It seems that in Python 2 unicode strings containing bytes are - # rendered differently than ascii strings containing same bytes. - # - # Example: - # >>> print('\xd3\x92') - # Ӓ - # >>> print(u'\xd3\x92') - # Ó - # - # This is the cause of the problem as user_file.name variable - # is received as a unicode which is converted into unicode - # strings containing bytes and is rendered incorrectly. - # - # Example: - # >>> import urllib.parse - # >>> name = u'%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D0%B5%D0%B8%CC%86%D1%82%D0%B5.txt' - # >>> print(urllib.parse.unquote(name)) - # Здравейте # This is wrong - # - # >>> name = '%D0%97%D0%B4%D1%80%D0%B0%D0%B2%D0%B5%D0%B8%CC%86%D1%82%D0%B5.txt' - # >>> print(urllib.parse.unquote(name)) - # Здравейте.txt # This is correct - user_file.name = user_file.name.encode('ascii') - uri = upload_message_image_from_request(request, user_file, user_profile) return json_success({'uri': uri})