2018-03-11 18:55:20 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2022-07-30 07:09:07 +02:00
|
|
|
import boto3.session
|
2018-03-11 18:55:20 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import migrations, models
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2018-03-11 18:55:20 +01:00
|
|
|
from django.db.migrations.state import StateApps
|
2021-08-10 02:11:16 +02:00
|
|
|
from mypy_boto3_s3.type_defs import CopySourceTypeDef
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
class Uploader:
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.old_orig_image_path_template = "{realm_id}/emoji/{emoji_file_name}.original"
|
|
|
|
self.old_path_template = "{realm_id}/emoji/{emoji_file_name}"
|
|
|
|
self.new_orig_image_path_template = "{realm_id}/emoji/images/{emoji_file_name}.original"
|
|
|
|
self.new_path_template = "{realm_id}/emoji/images/{emoji_file_name}"
|
|
|
|
|
|
|
|
def copy_files(self, src_path: str, dst_path: str) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def ensure_emoji_images(self, realm_id: int, old_filename: str, new_filename: str) -> None:
|
|
|
|
# Copy original image file.
|
2021-02-12 08:19:30 +01:00
|
|
|
old_file_path = self.old_orig_image_path_template.format(
|
|
|
|
realm_id=realm_id, emoji_file_name=old_filename
|
|
|
|
)
|
|
|
|
new_file_path = self.new_orig_image_path_template.format(
|
|
|
|
realm_id=realm_id, emoji_file_name=new_filename
|
|
|
|
)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.copy_files(old_file_path, new_file_path)
|
|
|
|
|
|
|
|
# Copy resized image file.
|
2021-02-12 08:19:30 +01:00
|
|
|
old_file_path = self.old_path_template.format(
|
|
|
|
realm_id=realm_id, emoji_file_name=old_filename
|
|
|
|
)
|
|
|
|
new_file_path = self.new_path_template.format(
|
|
|
|
realm_id=realm_id, emoji_file_name=new_filename
|
|
|
|
)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.copy_files(old_file_path, new_file_path)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
class LocalUploader(Uploader):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def mkdirs(path: str) -> None:
|
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
if not os.path.isdir(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
|
|
|
def copy_files(self, src_path: str, dst_path: str) -> None:
|
2022-05-31 00:59:29 +02:00
|
|
|
assert settings.LOCAL_UPLOADS_DIR is not None
|
2021-02-12 08:20:45 +01:00
|
|
|
src_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", src_path)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.mkdirs(src_path)
|
2021-02-12 08:20:45 +01:00
|
|
|
dst_path = os.path.join(settings.LOCAL_UPLOADS_DIR, "avatars", dst_path)
|
2018-03-11 18:55:20 +01:00
|
|
|
self.mkdirs(dst_path)
|
|
|
|
shutil.copyfile(src_path, dst_path)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
class S3Uploader(Uploader):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__()
|
2022-07-30 07:09:07 +02:00
|
|
|
session = boto3.session.Session(settings.S3_KEY, settings.S3_SECRET_KEY)
|
2018-03-23 18:47:56 +01:00
|
|
|
self.bucket_name = settings.S3_AVATAR_BUCKET
|
2021-02-12 08:19:30 +01:00
|
|
|
self.bucket = session.resource(
|
2021-02-12 08:20:45 +01:00
|
|
|
"s3", region_name=settings.S3_REGION, endpoint_url=settings.S3_ENDPOINT_URL
|
2021-02-12 08:19:30 +01:00
|
|
|
).Bucket(self.bucket_name)
|
2018-03-11 18:55:20 +01:00
|
|
|
|
|
|
|
def copy_files(self, src_key: str, dst_key: str) -> None:
|
2021-08-10 02:11:16 +02:00
|
|
|
source = CopySourceTypeDef(Bucket=self.bucket_name, Key=src_key)
|
|
|
|
self.bucket.copy(CopySource=source, Key=dst_key)
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
def get_uploader() -> Uploader:
|
|
|
|
if settings.LOCAL_UPLOADS_DIR is None:
|
|
|
|
return S3Uploader()
|
|
|
|
return LocalUploader()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
def get_emoji_file_name(emoji_file_name: str, new_name: str) -> str:
|
|
|
|
_, image_ext = os.path.splitext(emoji_file_name)
|
2021-02-12 08:20:45 +01:00
|
|
|
return "".join((new_name, image_ext))
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def migrate_realm_emoji_image_files(
|
|
|
|
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmEmoji = apps.get_model("zerver", "RealmEmoji")
|
2018-03-11 18:55:20 +01:00
|
|
|
uploader = get_uploader()
|
|
|
|
for realm_emoji in RealmEmoji.objects.all():
|
|
|
|
old_file_name = realm_emoji.file_name
|
|
|
|
new_file_name = get_emoji_file_name(old_file_name, str(realm_emoji.id))
|
|
|
|
uploader.ensure_emoji_images(realm_emoji.realm_id, old_file_name, new_file_name)
|
|
|
|
realm_emoji.file_name = new_file_name
|
2021-02-12 08:20:45 +01:00
|
|
|
realm_emoji.save(update_fields=["file_name"])
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def reversal(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2018-03-11 18:55:20 +01:00
|
|
|
# Ensures that migration can be re-run in case of a failure.
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmEmoji = apps.get_model("zerver", "RealmEmoji")
|
2018-03-11 18:55:20 +01:00
|
|
|
for realm_emoji in RealmEmoji.objects.all():
|
|
|
|
corrupt_file_name = realm_emoji.file_name
|
|
|
|
correct_file_name = get_emoji_file_name(corrupt_file_name, realm_emoji.name)
|
|
|
|
realm_emoji.file_name = correct_file_name
|
2021-02-12 08:20:45 +01:00
|
|
|
realm_emoji.save(update_fields=["file_name"])
|
2018-03-11 18:55:20 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-03-11 18:55:20 +01:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0148_max_invites_forget_default"),
|
2018-03-11 18:55:20 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.AlterUniqueTogether(
|
2021-02-12 08:20:45 +01:00
|
|
|
name="realmemoji",
|
2020-04-09 21:51:58 +02:00
|
|
|
unique_together=set(),
|
2018-03-11 18:55:20 +01:00
|
|
|
),
|
|
|
|
migrations.AlterField(
|
2021-02-12 08:20:45 +01:00
|
|
|
model_name="realmemoji",
|
|
|
|
name="file_name",
|
2018-03-11 18:55:20 +01:00
|
|
|
field=models.TextField(db_index=True, null=True, blank=True),
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(migrate_realm_emoji_image_files, reverse_code=reversal, elidable=True),
|
2018-03-11 18:55:20 +01:00
|
|
|
]
|