Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
upload: Adjust minimum encoding confidence thresholds.
Alex Vandiver218bbe041e4e
diff --git a/zerver/lib/upload/__init__.py b/zerver/lib/upload/__init__.pyindex 46794f086f5..1dca9ff387e 100644--- a/zerver/lib/upload/__init__.py+++ b/zerver/lib/upload/__init__.py@@ -72,7 +72,16 @@ def maybe_add_charset(content_type: str, file_data: bytes | StreamingSourceWithSdetector.close()reader.close()detected = detector.result- if detected["confidence"] > 0.9 and detected["encoding"]:+ if detected["confidence"] >= 0.90 and detected["encoding"]:+ fake_msg.set_param("charset", detected["encoding"], replace=True)+ elif detected["confidence"] >= 0.73 and detected["encoding"] == "ISO-8859-1":+ # ISO-8859-1 detection maxes out at 73%, so if that's what+ # we're seeing as the best guess, provide it.+ fake_msg.set_param("charset", detected["encoding"], replace=True)+ elif detected["confidence"] >= 0.66 and detected["encoding"] == "utf-8":+ # UTF-8 is far and wide the most common current encoding,+ # so we set a much lower threshold if that's the best guess.+ # https://en.wikipedia.org/wiki/Popularity_of_text_encodingsfake_msg.set_param("charset", detected["encoding"], replace=True)return fake_msg["content-type"]diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex c302372e2c4..220e066852e 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -224,20 +224,37 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):consume_response(result)def test_guess_content_type_charset(self) -> None:- uploaded_file = SimpleUploadedFile(- "somefile.txt", "नाम में क्या रक्खा हे".encode(), content_type="text/plain"- )- result = self.api_post(- self.example_user("hamlet"), "/api/v1/user_uploads", {"file": uploaded_file}- )-+ tests = [+ ("No high bytes in this string", "ascii", "ascii"), # Explicit ASCII encoding+ ("नाम में क्या रक्खा हे", "utf-8", "utf-8"), # Enough to get 99% confidence UTF-8+ ("日本語", "iso2022_jp", "ISO-2022-JP"), # Non-UTF-8 99% confidence+ ("日本語", "utf-8", "utf-8"), # UTF-8 is only 87% confident+ (+ "Min svävare är full av ålar.",+ "iso-8859-1",+ "ISO-8859-1",+ ), # iso-8859-1 maxes out at 73%, but we'll still guess it+ ("Aucune idée", "mac-roman", None), # Short text in obscure formats is left unguessed+ ]self.login("hamlet")- response_dict = self.assert_json_success(result)- url = response_dict["url"]- result = self.client_get(url)- self.assertEqual(result.status_code, 200)- self.assertEqual(result["Content-Type"], 'text/plain; charset="utf-8"')- consume_response(result)+ for test_string, encoded_in, found_encoding in tests:+ with self.subTest(msg=f"Encoding '{test_string}' in {encoded_in}"):+ uploaded_file = SimpleUploadedFile(+ "somefile.txt", test_string.encode(encoded_in), content_type="text/plain"+ )+ result = self.api_post(+ self.example_user("hamlet"), "/api/v1/user_uploads", {"file": uploaded_file}+ )++ response_dict = self.assert_json_success(result)+ url = response_dict["url"]+ result = self.client_get(url)+ self.assertEqual(result.status_code, 200)+ expected = "text/plain"+ if found_encoding is not None:+ expected += f'; charset="{found_encoding}"'+ self.assertEqual(result["Content-Type"], expected)+ consume_response(result)def test_content_type_charset_specified(self) -> None:with tempfile.NamedTemporaryFile() as uploaded_file: