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.

import_util: Use `response.iter_content` to write file stream.

PieterCK07ee7777cd63
diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.py
index 738731b959e..9124682f73a 100644
--- a/zerver/data_import/import_util.py
+++ b/zerver/data_import/import_util.py
@@ -625,8 +625,7 @@ def get_avatar(avatar_dir: str, size_url_suffix: str, avatar_upload_item: list[s
avatar_url += size_url_suffix
response = request_file_stream(avatar_url)
- with open(image_path, "wb") as image_file:
- shutil.copyfileobj(response.raw, image_file)
+ write_response_file_stream_to_path(response, image_path)
shutil.copy(image_path, original_image_path)
@@ -721,6 +720,18 @@ def request_file_stream(
return response
+WRITE_RESPONSE_FILE_STREAM_CHUNK_SIZE = 64 * 1024
+
+
+def write_response_file_stream_to_path(
+ response: requests.Response,
+ file_path: str,
+ chunk_size: int = WRITE_RESPONSE_FILE_STREAM_CHUNK_SIZE,
+) -> None:
+ with open(file_path, "wb") as file_destination:
+ file_destination.writelines(response.iter_content(chunk_size=chunk_size))
+
+
def download_and_export_upload_file(
output_dir: str, upload_file_request: UploadFileRequest
) -> None:
@@ -734,8 +745,7 @@ def download_and_export_upload_file(
)
os.makedirs(os.path.dirname(file_output_path), exist_ok=True)
- with open(file_output_path, "wb") as upload_file:
- shutil.copyfileobj(response.raw, upload_file)
+ write_response_file_stream_to_path(response, file_output_path)
def build_realm_emoji(realm_id: int, name: str, id: int, file_name: str) -> ZerverFieldsT:
@@ -786,8 +796,7 @@ def get_emojis(
upload_emoji_path = os.path.join(emoji_dir, emoji_path)
os.makedirs(os.path.dirname(upload_emoji_path), exist_ok=True)
- with open(upload_emoji_path, "wb") as emoji_file:
- shutil.copyfileobj(response.raw, emoji_file)
+ write_response_file_stream_to_path(response, upload_emoji_path)
return GetEmojiResult(path_id=emoji_path, filename=emoji_file_name)
diff --git a/zerver/data_import/microsoft_teams.py b/zerver/data_import/microsoft_teams.py
index 2ee732f0d47..659f09068bc 100644
--- a/zerver/data_import/microsoft_teams.py
+++ b/zerver/data_import/microsoft_teams.py
@@ -1,7 +1,6 @@
import logging
import os
import re
-import shutil
from collections import defaultdict
from collections.abc import Callable, Iterable
from dataclasses import dataclass
@@ -37,6 +36,7 @@ from zerver.data_import.import_util import (
make_subscriber_map,
request_file_stream,
validate_user_emails_for_import,
+ write_response_file_stream_to_path,
)
from zerver.data_import.sequencer import NEXT_ID
from zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZE, do_common_export_processes
@@ -461,8 +461,7 @@ def download_and_export_microsoft_teams_upload_file(
)
os.makedirs(os.path.dirname(file_output_path), exist_ok=True)
- with open(file_output_path, "wb") as upload_file:
- shutil.copyfileobj(response.raw, upload_file)
+ write_response_file_stream_to_path(response, file_output_path)
raw_content_type = response.headers.get("Content-Type")
assert raw_content_type is not None
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py
index 0d910dcbe5a..bdb38ebea56 100644
--- a/zerver/data_import/slack.py
+++ b/zerver/data_import/slack.py
@@ -47,6 +47,7 @@ from zerver.data_import.import_util import (
process_emojis,
request_file_stream,
validate_user_emails_for_import,
+ write_response_file_stream_to_path,
)
from zerver.data_import.sequencer import NEXT_ID
from zerver.data_import.slack_message_conversion import (
@@ -1601,14 +1602,13 @@ def fetch_team_icons(
return []
response = request_file_stream(icon_url)
- response_raw = response.raw
realm_id = zerver_realm["id"]
os.makedirs(os.path.join(output_dir, str(realm_id)), exist_ok=True)
original_icon_output_path = os.path.join(output_dir, str(realm_id), "icon.original")
- with open(original_icon_output_path, "wb") as output_file:
- shutil.copyfileobj(response_raw, output_file)
+ write_response_file_stream_to_path(response, original_icon_output_path)
+
records.append(
{
"realm_id": realm_id,