diff --git a/src/app/components/message/content/ImageContent.test.tsx b/src/app/components/message/content/ImageContent.test.tsx index d438625c3..deff648c9 100644 --- a/src/app/components/message/content/ImageContent.test.tsx +++ b/src/app/components/message/content/ImageContent.test.tsx @@ -2,7 +2,7 @@ import type { ReactNode } from 'react'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { describe, expect, it, vi } from 'vitest'; import { ImageContent } from './ImageContent'; -import { downloadEncryptedMedia } from '$utils/matrix'; +import { downloadEncryptedMedia, mxcUrlToHttp } from '$utils/matrix'; const screenMocks = vi.hoisted(() => ({ isMobile: true, tauri: false })); vi.mock('$hooks/useScreenSize', () => ({ @@ -21,7 +21,7 @@ vi.mock('@tauri-apps/api/core', () => ({ const SABLE_MEDIA_URL = 'sable-media://https://hs.example/_matrix/client/v1/media/download/example.org/abc123?__sable_media_cache=3'; vi.mock('$utils/matrix', () => ({ - mxcUrlToHttp: () => SABLE_MEDIA_URL, + mxcUrlToHttp: vi.fn<(...args: unknown[]) => string>(() => SABLE_MEDIA_URL), rewriteAuthenticatedMediaUrl: (url: string | null) => url, downloadEncryptedMedia: vi.fn<() => Promise>(), decryptFile: vi.fn<() => Promise>(), @@ -230,6 +230,41 @@ describe('ImageContent', () => { } }); + it('falls back to the original when the homeserver thumbnail is transposed', async () => { + vi.mocked(mxcUrlToHttp).mockClear(); + render( + preview} + renderViewer={() =>
viewer
} + /> + ); + + touchTap(screen.getByRole('button', { name: 'View' })); + const img = await screen.findByAltText('preview'); + expect(vi.mocked(mxcUrlToHttp).mock.calls.at(-1)).toEqual([ + {}, + 'mxc://example.org/abc123', + false, + 800, + 600, + 'scale', + ]); + + Object.defineProperty(img, 'naturalWidth', { value: 800, configurable: true }); + Object.defineProperty(img, 'naturalHeight', { value: 600, configurable: true }); + fireEvent.load(img); + + await waitFor(() => + expect(vi.mocked(mxcUrlToHttp).mock.calls.at(-1)).toEqual([ + {}, + 'mxc://example.org/abc123', + false, + ]) + ); + }); + it('still allows ordinary message touches to start long press', () => { vi.useFakeTimers(); const messageLongPress = vi.fn<() => void>(); diff --git a/src/app/components/message/content/ImageContent.tsx b/src/app/components/message/content/ImageContent.tsx index 6861a64fa..6bdc63da2 100644 --- a/src/app/components/message/content/ImageContent.tsx +++ b/src/app/components/message/content/ImageContent.tsx @@ -1,4 +1,4 @@ -import type { CSSProperties, ReactNode } from 'react'; +import type { CSSProperties, ReactNode, SyntheticEvent } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Badge, @@ -95,6 +95,14 @@ function wantsThumbnail(info: IImageInfo | undefined, width: number, height: num return window.devicePixelRatio === 1 || info.size > THUMBNAIL_MIN_SOURCE_BYTES; } +// `info.w`/`info.h` have the source EXIF orientation applied; homeservers that scale raw pixels +// return a transposed thumbnail with no EXIF left for the browser to correct. +function isTransposedThumbnail(info: IImageInfo | undefined, image: HTMLImageElement): boolean { + const { naturalWidth, naturalHeight } = image; + if (!info?.w || !info.h || !naturalWidth || !naturalHeight) return false; + return info.w > info.h !== naturalWidth > naturalHeight; +} + type RenderViewerProps = { src: string; alt: string; @@ -108,7 +116,7 @@ type RenderImageProps = { title: string; src: string; info?: IImageInfo; - onLoad: () => void; + onLoad: (event?: SyntheticEvent) => void; onError: () => void; onLottieLoad: () => void; onLottieError: () => void; @@ -255,7 +263,11 @@ export const ImageContent = as<'div', ImageContentProps>( }; }, [viewer, usesThumbnail, url, mx, useAuthentication]); - const handleLoad = () => { + const handleLoad = (event?: SyntheticEvent) => { + if (usesThumbnail && event && isTransposedThumbnail(info, event.currentTarget)) { + setThumbnailFailed(true); + return; + } setLoad(true); }; const handleError = () => {