From 2d7d1a7fd82f5b66f364c55244b3b8d7c1e1ab6a Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 10 Mar 2023 02:34:07 +0100 Subject: [PATCH] Attachmant Modal: Fix sending media with absurd aspect ratio (#2757) --- .../composer/helpers/buildAttachment.ts | 45 +++++++++++++------ .../composer/hooks/useAttachmentModal.ts | 2 + 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/components/middle/composer/helpers/buildAttachment.ts b/src/components/middle/composer/helpers/buildAttachment.ts index 64533bec6..7b2feb10c 100644 --- a/src/components/middle/composer/helpers/buildAttachment.ts +++ b/src/components/middle/composer/helpers/buildAttachment.ts @@ -14,6 +14,7 @@ import { import { scaleImage } from '../../../../util/imageResize'; const MAX_QUICK_IMG_SIZE = 1280; // px +const MAX_ASPECT_RATIO = 20; const FILE_EXT_REGEX = /\.[^/.]+$/; export default async function buildAttachment( @@ -24,32 +25,42 @@ export default async function buildAttachment( let quick; let audio; let previewBlobUrl; + let shouldSendAsFile; if (SUPPORTED_IMAGE_CONTENT_TYPES.has(mimeType)) { const img = await preloadImage(blobUrl); const { width, height } = img; + shouldSendAsFile = !validateAspectRatio(width, height); + const shouldShrink = Math.max(width, height) > MAX_QUICK_IMG_SIZE; const isGif = mimeType === GIF_MIME_TYPE; - if (!options?.compressedBlobUrl && !isGif && (shouldShrink || mimeType !== 'image/jpeg')) { - const resizedUrl = await scaleImage( - blobUrl, shouldShrink ? MAX_QUICK_IMG_SIZE / Math.max(width, height) : 1, 'image/jpeg', - ); - URL.revokeObjectURL(blobUrl); - return buildAttachment(filename, blob, { - compressedBlobUrl: resizedUrl, - }); + if (!shouldSendAsFile) { + if (!options?.compressedBlobUrl && !isGif && (shouldShrink || mimeType !== 'image/jpeg')) { + const resizedUrl = await scaleImage( + blobUrl, shouldShrink ? MAX_QUICK_IMG_SIZE / Math.max(width, height) : 1, 'image/jpeg', + ); + URL.revokeObjectURL(blobUrl); + return buildAttachment(filename, blob, { + compressedBlobUrl: resizedUrl, + }); + } + + if (mimeType === 'image/jpeg') { + filename = filename.replace(FILE_EXT_REGEX, '.jpg'); + } + + quick = { width, height }; } - if (mimeType === 'image/jpeg') { - filename = filename.replace(FILE_EXT_REGEX, '.jpg'); - } - - quick = { width, height }; previewBlobUrl = blobUrl; } else if (SUPPORTED_VIDEO_CONTENT_TYPES.has(mimeType)) { const { videoWidth: width, videoHeight: height, duration } = await preloadVideo(blobUrl); - quick = { width, height, duration }; + shouldSendAsFile = !validateAspectRatio(width, height); + + if (!shouldSendAsFile) { + quick = { width, height, duration }; + } previewBlobUrl = await createPosterForVideo(blobUrl); } else if (SUPPORTED_AUDIO_CONTENT_TYPES.has(mimeType)) { @@ -72,6 +83,7 @@ export default async function buildAttachment( quick, audio, previewBlobUrl, + shouldSendAsFile: shouldSendAsFile || undefined, uniqueId: `${Date.now()}-${Math.random()}`, ...options, }; @@ -98,3 +110,8 @@ export function prepareAttachmentsToSend( }; }); } + +function validateAspectRatio(width: number, height: number) { + const maxAspectRatio = Math.max(width, height) / Math.min(width, height); + return maxAspectRatio <= MAX_ASPECT_RATIO; +} diff --git a/src/components/middle/composer/hooks/useAttachmentModal.ts b/src/components/middle/composer/hooks/useAttachmentModal.ts index 6af8bfece..2152c8e86 100644 --- a/src/components/middle/composer/hooks/useAttachmentModal.ts +++ b/src/components/middle/composer/hooks/useAttachmentModal.ts @@ -108,6 +108,8 @@ export default function useAttachmentModal({ } function getAttachmentType(attachment: ApiAttachment) { + if (attachment.shouldSendAsFile) return 'file'; + if (SUPPORTED_IMAGE_CONTENT_TYPES.has(attachment.mimeType)) { return 'image'; }