emoji: Pass down content-type, rather than guessing from extension.

This commit is contained in:
Alex Vandiver
2024-07-11 18:46:33 +00:00
committed by Tim Abbott
parent f3473defe1
commit 62a0611ddb
12 changed files with 69 additions and 24 deletions

View File

@@ -10,6 +10,7 @@ from django.db import connection
from zerver.lib.avatar_hash import user_avatar_path
from zerver.lib.mime_types import guess_type
from zerver.lib.thumbnail import BadImageError
from zerver.lib.upload import upload_emoji_image, write_avatar_images
from zerver.lib.upload.s3 import S3UploadBackend, upload_image_to_s3
from zerver.models import Attachment, RealmEmoji, UserProfile
@@ -113,13 +114,21 @@ def _transfer_emoji_to_s3(realm_emoji: RealmEmoji) -> None:
)
assert settings.LOCAL_UPLOADS_DIR is not None
assert settings.LOCAL_AVATARS_DIR is not None
content_type = guess_type(emoji_path)[0]
emoji_path = os.path.join(settings.LOCAL_AVATARS_DIR, emoji_path) + ".original"
if content_type is None: # nocoverage
logging.error("Emoji %d has no recognizable file extension", realm_emoji.id)
return
try:
with open(emoji_path, "rb") as f:
upload_emoji_image(f, realm_emoji.file_name, realm_emoji.author, backend=s3backend)
upload_emoji_image(
f, realm_emoji.file_name, realm_emoji.author, content_type, backend=s3backend
)
logging.info("Uploaded emoji file in path %s", emoji_path)
except FileNotFoundError: # nocoverage
logging.error("Emoji %d could not be loaded from local disk", realm_emoji.id)
except BadImageError as e:
logging.error("Emoji %d is invalid: %s", realm_emoji.id, e)
def transfer_emoji_to_s3(processes: int) -> None: