Files
Desktop/ts/linkPreviews/shouldUseFullSizeLinkPreviewImage.ts
2025-10-06 12:23:41 -07:00

35 lines
906 B
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { LinkPreviewType } from '../types/message/LinkPreviews.js';
import { isImageAttachment } from '../util/Attachment.js';
const MINIMUM_FULL_SIZE_DIMENSION = 200;
export function shouldUseFullSizeLinkPreviewImage({
isStickerPack,
image,
}: Readonly<LinkPreviewType>): boolean {
if (isStickerPack || !image || !isImageAttachment(image)) {
return false;
}
const { width, height } = image;
return (
isDimensionFullSize(width) &&
isDimensionFullSize(height) &&
!isRoughlySquare(width, height)
);
}
function isDimensionFullSize(dimension: unknown): dimension is number {
return (
typeof dimension === 'number' && dimension >= MINIMUM_FULL_SIZE_DIMENSION
);
}
function isRoughlySquare(width: number, height: number): boolean {
return Math.abs(1 - width / height) < 0.05;
}