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.

data_import: Move `get_attachment_path_and_content` to import util.

PieterCK21a6546ef012
diff --git a/zerver/data_import/import_util.py b/zerver/data_import/import_util.py
index 0f3a4c20800..ee6a98c82bc 100644
--- a/zerver/data_import/import_util.py
+++ b/zerver/data_import/import_util.py
@@ -1,6 +1,7 @@
import logging
import os
import random
+import secrets
import shutil
import subprocess
from collections import defaultdict
@@ -24,6 +25,7 @@ from zerver.lib.parallel import run_parallel
from zerver.lib.partial import partial
from zerver.lib.stream_color import STREAM_ASSIGNMENT_COLORS as STREAM_COLORS
from zerver.lib.thumbnail import THUMBNAIL_ACCEPT_IMAGE_TYPES, BadImageError
+from zerver.lib.upload import sanitize_name
from zerver.models import (
Attachment,
DirectMessageGroup,
@@ -878,3 +880,20 @@ def get_data_file(path: str) -> Any:
with open(path, "rb") as fp:
data = orjson.loads(fp.read())
return data
+
+
+def get_attachment_path_and_content(fileinfo: ZerverFieldsT, realm_id: int) -> tuple[str, str]:
+ # Should be kept in sync with its equivalent in zerver/lib/uploads in the function
+ # 'upload_message_attachment'
+ s3_path = "/".join(
+ [
+ str(realm_id),
+ format(random.randint(0, 255), "x"),
+ secrets.token_urlsafe(18),
+ sanitize_name(fileinfo["name"]),
+ ]
+ )
+ attachment_path = f"/user_uploads/{s3_path}"
+ content = "[{}]({})".format(fileinfo["title"], attachment_path)
+
+ return s3_path, content
diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.py
index b757d62f03d..fd1bd7a1874 100644
--- a/zerver/data_import/slack.py
+++ b/zerver/data_import/slack.py
@@ -2,9 +2,7 @@ import itertools
import logging
import os
import posixpath
-import random
import re
-import secrets
import shutil
import time
import zipfile
@@ -38,6 +36,7 @@ from zerver.data_import.import_util import (
build_usermessages,
build_zerver_realm,
create_converted_data_files,
+ get_attachment_path_and_content,
get_data_file,
long_term_idle_helper,
make_subscriber_map,
@@ -59,7 +58,6 @@ from zerver.lib.message import truncate_content
from zerver.lib.mime_types import guess_type
from zerver.lib.storage import static_path
from zerver.lib.thumbnail import THUMBNAIL_ACCEPT_IMAGE_TYPES, resize_realm_icon
-from zerver.lib.upload import sanitize_name
from zerver.models import (
CustomProfileField,
CustomProfileFieldValue,
@@ -1366,23 +1364,6 @@ def process_message_files(
)
-def get_attachment_path_and_content(fileinfo: ZerverFieldsT, realm_id: int) -> tuple[str, str]:
- # Should be kept in sync with its equivalent in zerver/lib/uploads in the function
- # 'upload_message_attachment'
- s3_path = "/".join(
- [
- str(realm_id),
- format(random.randint(0, 255), "x"),
- secrets.token_urlsafe(18),
- sanitize_name(fileinfo["name"]),
- ]
- )
- attachment_path = f"/user_uploads/{s3_path}"
- content = "[{}]({})".format(fileinfo["title"], attachment_path)
-
- return s3_path, content
-
-
def build_reactions(
reaction_list: list[ZerverFieldsT],
reactions: list[ZerverFieldsT],