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.attachments import get_old_unclaimed_attachments, validate_attachment_request
from zerver.lib.markdown import MessageRenderingResult from zerver.lib.markdown import MessageRenderingResult
from zerver.lib.thumbnail import StoredThumbnailFormat from zerver.lib.thumbnail import StoredThumbnailFormat, get_image_thumbnail_path
from zerver.lib.upload import claim_attachment, delete_message_attachments, get_image_thumbnail_path from zerver.lib.upload import claim_attachment, delete_message_attachments
from zerver.models import ( from zerver.models import (
Attachment, Attachment,
ImageAttachment, ImageAttachment,

View File

@ -305,3 +305,19 @@ def maybe_thumbnail(attachment: AbstractAttachment, content: bytes) -> ImageAtta
return image_row return image_row
except BadImageError: except BadImageError:
return None 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, MEDIUM_AVATAR_SIZE,
THUMBNAIL_ACCEPT_IMAGE_TYPES, THUMBNAIL_ACCEPT_IMAGE_TYPES,
BadImageError, BadImageError,
BaseThumbnailFormat,
maybe_thumbnail, maybe_thumbnail,
resize_avatar, resize_avatar,
resize_emoji, resize_emoji,
) )
from zerver.lib.upload.base import INLINE_MIME_TYPES, ZulipUploadBackend from zerver.lib.upload.base import INLINE_MIME_TYPES, ZulipUploadBackend
from zerver.models import ( from zerver.models import Attachment, Message, Realm, RealmEmoji, ScheduledMessage, UserProfile
Attachment,
ImageAttachment,
Message,
Realm,
RealmEmoji,
ScheduledMessage,
UserProfile,
)
from zerver.models.users import is_cross_realm_bot_email from zerver.models.users import is_cross_realm_bot_email
@ -136,22 +127,6 @@ def sanitize_name(value: str) -> str:
return value 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( def upload_message_attachment(
uploaded_file_name: str, uploaded_file_name: str,
content_type: 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.actions.uploads import do_delete_old_unclaimed_attachments
from zerver.lib.attachments import get_old_unclaimed_attachments from zerver.lib.attachments import get_old_unclaimed_attachments
from zerver.lib.management import ZulipBaseCommand, abort_unless_locked from zerver.lib.management import ZulipBaseCommand, abort_unless_locked
from zerver.lib.upload import ( from zerver.lib.thumbnail import split_thumbnail_path
all_message_attachments, from zerver.lib.upload import all_message_attachments, delete_message_attachments
delete_message_attachments,
split_thumbnail_path,
)
from zerver.models import ArchivedAttachment, Attachment from zerver.models import ArchivedAttachment, Attachment

View File

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

View File

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

View File

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