2019-02-14 13:42:04 +01:00
|
|
|
import logging
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
|
|
|
from mimetypes import guess_type
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection
|
|
|
|
|
|
|
|
from zerver.lib.avatar_hash import user_avatar_path
|
|
|
|
from zerver.lib.parallel import run_parallel
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.upload import S3UploadBackend, upload_image_to_s3
|
|
|
|
from zerver.models import Attachment, RealmEmoji, UserProfile
|
2019-02-14 13:42:04 +01:00
|
|
|
|
|
|
|
s3backend = S3UploadBackend()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def transfer_avatars_to_s3(processes: int) -> None:
|
|
|
|
def _transfer_avatar_to_s3(user: UserProfile) -> int:
|
|
|
|
avatar_path = user_avatar_path(user)
|
|
|
|
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", avatar_path) + ".original"
|
|
|
|
try:
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
s3backend.upload_avatar_image(f, user, user)
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Uploaded avatar for %s in realm %s", user.id, user.realm.name)
|
2019-02-14 13:42:04 +01:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
return 0
|
|
|
|
|
|
|
|
users = list(UserProfile.objects.all())
|
|
|
|
if processes == 1:
|
|
|
|
for user in users:
|
|
|
|
_transfer_avatar_to_s3(user)
|
|
|
|
else: # nocoverage
|
|
|
|
output = []
|
|
|
|
connection.close()
|
|
|
|
for (status, job) in run_parallel(_transfer_avatar_to_s3, users, processes):
|
|
|
|
output.append(job)
|
|
|
|
|
|
|
|
def transfer_message_files_to_s3(processes: int) -> None:
|
|
|
|
def _transfer_message_files_to_s3(attachment: Attachment) -> int:
|
|
|
|
file_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "files", attachment.path_id)
|
|
|
|
try:
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
guessed_type = guess_type(attachment.file_name)[0]
|
2020-06-19 01:09:20 +02:00
|
|
|
upload_image_to_s3(s3backend.uploads_bucket, attachment.path_id, guessed_type, attachment.owner, f.read())
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Uploaded message file in path %s", file_path)
|
2019-02-14 13:42:04 +01:00
|
|
|
except FileNotFoundError: # nocoverage
|
|
|
|
pass
|
|
|
|
return 0
|
|
|
|
|
|
|
|
attachments = list(Attachment.objects.all())
|
|
|
|
if processes == 1:
|
|
|
|
for attachment in attachments:
|
|
|
|
_transfer_message_files_to_s3(attachment)
|
|
|
|
else: # nocoverage
|
|
|
|
output = []
|
|
|
|
connection.close()
|
|
|
|
for status, job in run_parallel(_transfer_message_files_to_s3, attachments, processes):
|
|
|
|
output.append(job)
|
|
|
|
|
|
|
|
def transfer_emoji_to_s3(processes: int) -> None:
|
|
|
|
def _transfer_emoji_to_s3(realm_emoji: RealmEmoji) -> int:
|
|
|
|
if not realm_emoji.file_name or not realm_emoji.author:
|
|
|
|
return 0 # nocoverage
|
|
|
|
emoji_path = RealmEmoji.PATH_ID_TEMPLATE.format(
|
|
|
|
realm_id=realm_emoji.realm.id,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
emoji_file_name=realm_emoji.file_name,
|
2019-02-14 13:42:04 +01:00
|
|
|
)
|
|
|
|
emoji_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", emoji_path) + ".original"
|
|
|
|
try:
|
|
|
|
with open(emoji_path, 'rb') as f:
|
|
|
|
s3backend.upload_emoji_image(f, realm_emoji.file_name, realm_emoji.author)
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.info("Uploaded emoji file in path %s", emoji_path)
|
2019-02-14 13:42:04 +01:00
|
|
|
except FileNotFoundError: # nocoverage
|
|
|
|
pass
|
|
|
|
return 0
|
|
|
|
|
|
|
|
realm_emojis = list(RealmEmoji.objects.filter())
|
|
|
|
if processes == 1:
|
|
|
|
for realm_emoji in realm_emojis:
|
|
|
|
_transfer_emoji_to_s3(realm_emoji)
|
|
|
|
else: # nocoverage
|
|
|
|
output = []
|
|
|
|
connection.close()
|
|
|
|
for status, job in run_parallel(_transfer_emoji_to_s3, realm_emojis, processes):
|
|
|
|
output.append(job)
|