migrations: Fix emoji re-thumbnailing for imports.

Imports (including conversions from Slack, Hipchat, etc) do not write
the `.original` of the file (nor the still versions, for animations),
only the thumbnailed (and possibly animated) version.  Looking for
`.original` would thus fail, and overwrite the (working) emoji with
the "?" and disable it.

In the event that we do not find the `.original`, fall back to the
bytes from the non-`.original`, to handle these import cases.
This commit is contained in:
Alex Vandiver 2024-07-15 19:05:10 +00:00 committed by Tim Abbott
parent 1745d87f2e
commit 90701978d9
1 changed files with 17 additions and 6 deletions

View File

@ -158,9 +158,11 @@ def thumbnail_local_emoji(apps: StateApps) -> None:
try:
if os.path.exists(f"{base_path}/{new_file_name}.original"):
os.unlink(f"{base_path}/{new_file_name}.original")
os.link(
f"{base_path}/{old_file_name}.original", f"{base_path}/{new_file_name}.original"
)
from_file = f"{base_path}/{old_file_name}.original"
if not os.path.exists(from_file) and os.path.exists(f"{base_path}/{old_file_name}"):
# Imports currently don't write ".original" files, so check without that
from_file = f"{base_path}/{old_file_name}"
os.link(from_file, f"{base_path}/{new_file_name}.original")
with open(f"{base_path}/{new_file_name}.original", "rb") as fh:
original_bytes = fh.read()
except OSError as e:
@ -226,10 +228,19 @@ def thumbnail_s3(apps: StateApps) -> None:
try:
old_data = avatar_bucket.Object(f"{base_path}/{old_file_name}.original").get()
original_bytes = old_data["Body"].read()
except botocore.exceptions.ClientError as e:
raise SkipImageError(f"Failed to read original file: {e}")
content_type = old_data["ContentType"]
except botocore.exceptions.ClientError:
# Imports currently don't write ".original" files, so check without that
try:
old_data = avatar_bucket.Object(f"{base_path}/{old_file_name}").get()
except botocore.exceptions.ClientError as e:
raise SkipImageError(f"Failed to read .original file: {e}")
original_bytes = old_data["Body"].read()
# They also may have uploaded as "application/octet-stream", so guess the
# content-type from the filename. If we can't guess, then we'll hit the
# SkipImageError case right below this.
content_type = guess_type(old_file_name)[0] or "application/octet-stream"
if content_type not in VALID_EMOJI_CONTENT_TYPE:
raise SkipImageError(f"Invalid content-type: {content_type}")