thumbnail: Move get_image_thumbnail_path and split_thumbnail_path.

This commit is contained in:
Alex Vandiver 2024-07-17 01:11:57 +00:00 committed by Tim Abbott
parent d474600056
commit 4351cc5914
7 changed files with 26 additions and 40 deletions

View File

@ -3,8 +3,8 @@ from typing import Any
from zerver.lib.attachments import get_old_unclaimed_attachments, validate_attachment_request
from zerver.lib.markdown import MessageRenderingResult
from zerver.lib.thumbnail import StoredThumbnailFormat
from zerver.lib.upload import claim_attachment, delete_message_attachments, get_image_thumbnail_path
from zerver.lib.thumbnail import StoredThumbnailFormat, get_image_thumbnail_path
from zerver.lib.upload import claim_attachment, delete_message_attachments
from zerver.models import (
Attachment,
ImageAttachment,

View File

@ -305,3 +305,19 @@ def maybe_thumbnail(attachment: AbstractAttachment, content: bytes) -> ImageAtta
return image_row
except BadImageError:
return None
def get_image_thumbnail_path(
image_attachment: ImageAttachment,
thumbnail_format: BaseThumbnailFormat,
) -> str:
return f"thumbnail/{image_attachment.path_id}/{thumbnail_format!s}"
def split_thumbnail_path(file_path: str) -> tuple[str, BaseThumbnailFormat]:
assert file_path.startswith("thumbnail/")
path_parts = file_path.split("/")
thumbnail_format = BaseThumbnailFormat.from_string(path_parts.pop())
assert thumbnail_format is not None
path_id = "/".join(path_parts[1:])
return path_id, thumbnail_format

View File

@ -22,21 +22,12 @@ from zerver.lib.thumbnail import (
MEDIUM_AVATAR_SIZE,
THUMBNAIL_ACCEPT_IMAGE_TYPES,
BadImageError,
BaseThumbnailFormat,
maybe_thumbnail,
resize_avatar,
resize_emoji,
)
from zerver.lib.upload.base import INLINE_MIME_TYPES, ZulipUploadBackend
from zerver.models import (
Attachment,
ImageAttachment,
Message,
Realm,
RealmEmoji,
ScheduledMessage,
UserProfile,
)
from zerver.models import Attachment, Message, Realm, RealmEmoji, ScheduledMessage, UserProfile
from zerver.models.users import is_cross_realm_bot_email
@ -136,22 +127,6 @@ def sanitize_name(value: str) -> str:
return value
def get_image_thumbnail_path(
image_attachment: ImageAttachment,
thumbnail_format: BaseThumbnailFormat,
) -> str:
return f"thumbnail/{image_attachment.path_id}/{thumbnail_format!s}"
def split_thumbnail_path(file_path: str) -> tuple[str, BaseThumbnailFormat]:
assert file_path.startswith("thumbnail/")
path_parts = file_path.split("/")
thumbnail_format = BaseThumbnailFormat.from_string(path_parts.pop())
assert thumbnail_format is not None
path_id = "/".join(path_parts[1:])
return path_id, thumbnail_format
def upload_message_attachment(
uploaded_file_name: str,
content_type: str,

View File

@ -9,11 +9,8 @@ from typing_extensions import override
from zerver.actions.uploads import do_delete_old_unclaimed_attachments
from zerver.lib.attachments import get_old_unclaimed_attachments
from zerver.lib.management import ZulipBaseCommand, abort_unless_locked
from zerver.lib.upload import (
all_message_attachments,
delete_message_attachments,
split_thumbnail_path,
)
from zerver.lib.thumbnail import split_thumbnail_path
from zerver.lib.upload import all_message_attachments, delete_message_attachments
from zerver.models import ArchivedAttachment, Attachment

View File

@ -18,14 +18,12 @@ from zerver.lib.thumbnail import (
BaseThumbnailFormat,
StoredThumbnailFormat,
ThumbnailFormat,
get_image_thumbnail_path,
missing_thumbnails,
resize_emoji,
)
from zerver.lib.upload import (
all_message_attachments,
get_image_thumbnail_path,
split_thumbnail_path,
)
from zerver.lib.upload import all_message_attachments
from zerver.models import Attachment, ImageAttachment
from zerver.views.upload import closest_thumbnail_format
from zerver.worker.thumbnail import ensure_thumbnails

View File

@ -35,10 +35,10 @@ from zerver.lib.thumbnail import (
THUMBNAIL_OUTPUT_FORMATS,
BaseThumbnailFormat,
StoredThumbnailFormat,
get_image_thumbnail_path,
)
from zerver.lib.upload import (
check_upload_within_quota,
get_image_thumbnail_path,
get_public_upload_root_url,
upload_message_attachment_from_request,
)

View File

@ -9,8 +9,8 @@ from django.db import transaction
from typing_extensions import override
from zerver.lib.mime_types import guess_type
from zerver.lib.thumbnail import StoredThumbnailFormat, missing_thumbnails
from zerver.lib.upload import get_image_thumbnail_path, save_attachment_contents, upload_backend
from zerver.lib.thumbnail import StoredThumbnailFormat, get_image_thumbnail_path, missing_thumbnails
from zerver.lib.upload import save_attachment_contents, upload_backend
from zerver.models import ImageAttachment
from zerver.worker.base import QueueProcessingWorker, assign_queue