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.

rocketchat: Fix multi-chunk attachment reassembly order.

Alex Vandiver55c5e4670710
diff --git a/zerver/data_import/rocketchat.py b/zerver/data_import/rocketchat.py
index ebba89cf4b1..9414088d04e 100644
--- a/zerver/data_import/rocketchat.py
+++ b/zerver/data_import/rocketchat.py
@@ -291,7 +291,7 @@ def build_custom_emoji(
for emoji_file in custom_emoji_data["file"]:
if isinstance(emoji_file["_id"], bson.objectid.ObjectId): # nocoverage
object_id_to_filename[str(emoji_file["_id"])] = emoji_file["filename"]
- for emoji_chunk in custom_emoji_data["chunk"]:
+ for emoji_chunk in sorted(custom_emoji_data["chunk"], key=lambda c: c["n"]):
file_id = str(emoji_chunk["files_id"])
emoji_file_data[object_id_to_filename.get(file_id, file_id)].append(emoji_chunk["data"])
@@ -831,7 +831,7 @@ def map_upload_id_to_upload_data(
for upload in upload_data["upload"]:
upload_id_to_upload_data_map[upload["_id"]] = {**upload, "chunk": []}
- for chunk in upload_data["chunk"]:
+ for chunk in sorted(upload_data["chunk"], key=lambda c: c["n"]):
if chunk["files_id"] not in upload_id_to_upload_data_map: # nocoverage
logging.info("Skipping chunk %s without metadata", chunk["files_id"])
# It's unclear why this apparent data corruption in the
diff --git a/zerver/tests/test_rocketchat_importer.py b/zerver/tests/test_rocketchat_importer.py
index 33adb417fbe..717832dd524 100644
--- a/zerver/tests/test_rocketchat_importer.py
+++ b/zerver/tests/test_rocketchat_importer.py
@@ -861,6 +861,17 @@ class RocketChatImporter(ZulipTestCase):
self.assertEqual(upload_id_to_upload_data_map[upload_id]["name"], upload_name)
self.assert_length(upload_id_to_upload_data_map[upload_id]["chunk"], 1)
+ # Verify multi-chunk files have chunks in correct order.
+ # The fixture's PDF (4BoikTP2ZZkE5RsGh) has chunks stored as
+ # n=0, n=2, n=1 in the BSON file; they must be reassembled as
+ # n=0, n=1, n=2.
+ pdf_upload_id = "4BoikTP2ZZkE5RsGh"
+ pdf_chunks = upload_id_to_upload_data_map[pdf_upload_id]["chunk"]
+ self.assert_length(pdf_chunks, 3)
+ self.assertEqual(len(pdf_chunks[0]), 261120)
+ self.assertEqual(len(pdf_chunks[1]), 261120)
+ self.assertEqual(len(pdf_chunks[2]), 47300)
+
def test_build_reactions(self) -> None:
fixture_dir_name = self.fixture_file_name("", "rocketchat_fixtures")
rocketchat_data = rocketchat_data_to_dict(fixture_dir_name)