2019-02-14 13:42:04 +01:00
|
|
|
import logging
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2022-07-29 09:14:09 +02:00
|
|
|
from concurrent.futures import ProcessPoolExecutor, as_completed
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2022-07-19 00:14:23 +02:00
|
|
|
import bmemcached
|
2019-02-14 13:42:04 +01:00
|
|
|
from django.conf import settings
|
2020-10-01 00:20:02 +02:00
|
|
|
from django.core.cache import cache
|
2019-02-14 13:42:04 +01:00
|
|
|
from django.db import connection
|
|
|
|
|
|
|
|
from zerver.lib.avatar_hash import user_avatar_path
|
2024-06-18 23:41:37 +02:00
|
|
|
from zerver.lib.mime_types import guess_type
|
2024-06-25 21:03:49 +02:00
|
|
|
from zerver.lib.upload import upload_avatar_image, upload_emoji_image
|
2022-12-14 21:51:37 +01:00
|
|
|
from zerver.lib.upload.s3 import S3UploadBackend, upload_image_to_s3
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Attachment, RealmEmoji, UserProfile
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
s3backend = S3UploadBackend()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-02-14 13:42:04 +01:00
|
|
|
def transfer_uploads_to_s3(processes: int) -> None:
|
|
|
|
# TODO: Eventually, we'll want to add realm icon and logo
|
|
|
|
transfer_avatars_to_s3(processes)
|
|
|
|
transfer_message_files_to_s3(processes)
|
|
|
|
transfer_emoji_to_s3(processes)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def _transfer_avatar_to_s3(user: UserProfile) -> None:
|
|
|
|
avatar_path = user_avatar_path(user)
|
2022-05-31 00:59:29 +02:00
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
2022-12-12 22:02:25 +01:00
|
|
|
assert settings.LOCAL_AVATARS_DIR is not None
|
2024-06-25 21:03:49 +02:00
|
|
|
file_path = os.path.join(settings.LOCAL_AVATARS_DIR, avatar_path)
|
2019-07-28 01:22:28 +02:00
|
|
|
try:
|
2024-06-25 21:03:49 +02:00
|
|
|
with open(file_path + ".original", "rb") as f:
|
2024-06-25 23:27:26 +02:00
|
|
|
upload_avatar_image(f, user, backend=s3backend)
|
2019-07-28 01:22:28 +02:00
|
|
|
logging.info("Uploaded avatar for %s in realm %s", user.id, user.realm.name)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def transfer_avatars_to_s3(processes: int) -> None:
|
2019-02-14 13:42:04 +01:00
|
|
|
users = list(UserProfile.objects.all())
|
|
|
|
if processes == 1:
|
|
|
|
for user in users:
|
|
|
|
_transfer_avatar_to_s3(user)
|
|
|
|
else: # nocoverage
|
|
|
|
connection.close()
|
2022-10-08 06:10:17 +02:00
|
|
|
_cache = cache._cache # type: ignore[attr-defined] # not in stubs
|
2022-07-19 00:14:23 +02:00
|
|
|
assert isinstance(_cache, bmemcached.Client)
|
|
|
|
_cache.disconnect_all()
|
2022-07-29 09:14:09 +02:00
|
|
|
with ProcessPoolExecutor(max_workers=processes) as executor:
|
|
|
|
for future in as_completed(
|
|
|
|
executor.submit(_transfer_avatar_to_s3, user) for user in users
|
|
|
|
):
|
|
|
|
future.result()
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def _transfer_message_files_to_s3(attachment: Attachment) -> None:
|
2022-05-31 00:59:29 +02:00
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
2022-12-12 22:02:25 +01:00
|
|
|
assert settings.LOCAL_FILES_DIR is not None
|
|
|
|
file_path = os.path.join(settings.LOCAL_FILES_DIR, attachment.path_id)
|
2019-07-28 01:22:28 +02:00
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(file_path, "rb") as f:
|
2019-07-28 01:22:28 +02:00
|
|
|
guessed_type = guess_type(attachment.file_name)[0]
|
2021-02-12 08:19:30 +01:00
|
|
|
upload_image_to_s3(
|
|
|
|
s3backend.uploads_bucket,
|
|
|
|
attachment.path_id,
|
|
|
|
guessed_type,
|
|
|
|
attachment.owner,
|
|
|
|
f.read(),
|
2023-07-19 04:27:03 +02:00
|
|
|
settings.S3_UPLOADS_STORAGE_CLASS,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2019-07-28 01:22:28 +02:00
|
|
|
logging.info("Uploaded message file in path %s", file_path)
|
|
|
|
except FileNotFoundError: # nocoverage
|
|
|
|
pass
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def transfer_message_files_to_s3(processes: int) -> None:
|
2019-02-14 13:42:04 +01:00
|
|
|
attachments = list(Attachment.objects.all())
|
|
|
|
if processes == 1:
|
|
|
|
for attachment in attachments:
|
|
|
|
_transfer_message_files_to_s3(attachment)
|
|
|
|
else: # nocoverage
|
|
|
|
connection.close()
|
2022-10-08 06:10:17 +02:00
|
|
|
_cache = cache._cache # type: ignore[attr-defined] # not in stubs
|
2022-07-19 00:14:23 +02:00
|
|
|
assert isinstance(_cache, bmemcached.Client)
|
|
|
|
_cache.disconnect_all()
|
2022-07-29 09:14:09 +02:00
|
|
|
with ProcessPoolExecutor(max_workers=processes) as executor:
|
|
|
|
for future in as_completed(
|
|
|
|
executor.submit(_transfer_message_files_to_s3, attachment)
|
|
|
|
for attachment in attachments
|
|
|
|
):
|
|
|
|
future.result()
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def _transfer_emoji_to_s3(realm_emoji: RealmEmoji) -> None:
|
|
|
|
if not realm_emoji.file_name or not realm_emoji.author:
|
|
|
|
return # nocoverage
|
|
|
|
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=realm_emoji.realm.id,
|
|
|
|
emoji_file_name=realm_emoji.file_name,
|
|
|
|
)
|
2022-05-31 00:59:29 +02:00
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
2022-12-12 22:02:25 +01:00
|
|
|
assert settings.LOCAL_AVATARS_DIR is not None
|
|
|
|
emoji_path = os.path.join(settings.LOCAL_AVATARS_DIR, emoji_path) + ".original"
|
2019-07-28 01:22:28 +02:00
|
|
|
try:
|
2021-02-12 08:20:45 +01:00
|
|
|
with open(emoji_path, "rb") as f:
|
2024-06-24 22:25:16 +02:00
|
|
|
upload_emoji_image(f, realm_emoji.file_name, realm_emoji.author, backend=s3backend)
|
2019-07-28 01:22:28 +02:00
|
|
|
logging.info("Uploaded emoji file in path %s", emoji_path)
|
|
|
|
except FileNotFoundError: # nocoverage
|
|
|
|
pass
|
2019-02-14 13:42:04 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-07-28 01:22:28 +02:00
|
|
|
def transfer_emoji_to_s3(processes: int) -> None:
|
2019-02-14 13:42:04 +01:00
|
|
|
realm_emojis = list(RealmEmoji.objects.filter())
|
|
|
|
if processes == 1:
|
|
|
|
for realm_emoji in realm_emojis:
|
|
|
|
_transfer_emoji_to_s3(realm_emoji)
|
|
|
|
else: # nocoverage
|
|
|
|
connection.close()
|
2022-10-08 06:10:17 +02:00
|
|
|
_cache = cache._cache # type: ignore[attr-defined] # not in stubs
|
2022-07-19 00:14:23 +02:00
|
|
|
assert isinstance(_cache, bmemcached.Client)
|
|
|
|
_cache.disconnect_all()
|
2022-07-29 09:14:09 +02:00
|
|
|
with ProcessPoolExecutor(max_workers=processes) as executor:
|
|
|
|
for future in as_completed(
|
|
|
|
executor.submit(_transfer_emoji_to_s3, realm_emoji) for realm_emoji in realm_emojis
|
|
|
|
):
|
|
|
|
future.result()
|