import_realm: Use generate_message_upload_path() to get upload file path.

This way, we no longer have to manually keep the upload path code in
sync with the upload path code in zerver/lib/upload.py.
This was originally suggested in
https://github.com/zulip/zulip/pull/19478#issuecomment-911479530.

This change fixes a bug when importing into a server using the local
file uploads backend, where the `import_realm.py` copy wasn't using
our standard 256-directory approach to avoid putting too many files in
a single directory.
This commit is contained in:
rht 2021-09-02 08:28:21 -04:00 committed by Tim Abbott
parent 6ff659d199
commit a13f3d4386
1 changed files with 9 additions and 10 deletions

View File

@ -2,7 +2,6 @@ import datetime
import logging
import multiprocessing
import os
import secrets
import shutil
from mimetypes import guess_type
from typing import Any, Dict, Iterable, List, Optional, Tuple
@ -32,7 +31,7 @@ from zerver.lib.message import get_last_message_id
from zerver.lib.server_initialization import create_internal_realm, server_initialized
from zerver.lib.streams import render_stream_description
from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.upload import BadImageError, get_bucket, sanitize_name
from zerver.lib.upload import BadImageError, get_bucket, sanitize_name, upload_backend
from zerver.lib.utils import generate_api_key, process_list_in_batches
from zerver.models import (
AlertWord,
@ -672,6 +671,12 @@ def fix_subscriptions_is_user_active_column(
def process_avatars(record: Dict[str, Any]) -> None:
# We need to re-import upload_backend here, because in the
# import-export unit tests, the Zulip settings are overridden for
# specific tests to control the choice of upload backend, and this
# reimport ensures that we use the right choice for the current
# test. Outside the test suite, settings never change after the
# server is started, so this import will have no effect in production.
from zerver.lib.upload import upload_backend
if record["s3_path"].endswith(".original"):
@ -773,17 +778,11 @@ def import_uploads(
relative_path = os.path.join(str(record["realm_id"]), "realm", icon_name)
record["last_modified"] = timestamp
else:
# Should be kept in sync with its equivalent in zerver/lib/uploads in the
# function 'upload_message_file'.
# This relative_path is basically the new location of the file,
# which will later be copied from its original location as
# specified in record["s3_path"].
relative_path = "/".join(
[
str(record["realm_id"]),
secrets.token_urlsafe(18),
sanitize_name(os.path.basename(record["path"])),
]
relative_path = upload_backend.generate_message_upload_path(
str(record["realm_id"]), sanitize_name(os.path.basename(record["path"]))
)
path_maps["attachment_path"][record["s3_path"]] = relative_path