-
-
Notifications
You must be signed in to change notification settings - Fork 361
Load Thumbnail first and fall back to the original image if it cannot be loaded #2445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,13 +184,32 @@ export function RenderMessageContent({ | |
| } | ||
|
|
||
| if (msgType === MsgType.Image) { | ||
| const content: IImageContent = getContent(); | ||
| const width = content?.info?.w; | ||
| const height = content?.info?.h; | ||
| let thumbnail: { | ||
| imgWidth: number, | ||
| imgHeight: number, | ||
| resizeMethod: string | ||
| } | undefined; | ||
| if (width && height) { | ||
| const scale = (width > height ? width : height) / 800; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we already has a standard thumbnail scaling function |
||
| if (width > 800 || height > 800 && scale > 1) { | ||
| thumbnail = { | ||
| imgWidth: width / scale, | ||
| imgHeight: height / scale, | ||
| resizeMethod: "scale", | ||
| } | ||
| } | ||
| } | ||
| return ( | ||
| <> | ||
| <MImage | ||
| content={getContent()} | ||
| content={content} | ||
| renderImageContent={(props) => ( | ||
| <ImageContent | ||
| {...props} | ||
| {...thumbnail} | ||
| autoPlay={mediaAutoLoad} | ||
| renderImage={(p) => <Image {...p} loading="lazy" />} | ||
| renderViewer={(p) => <ImageViewer {...p} />} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,9 @@ export type ImageContentProps = { | |
| autoPlay?: boolean; | ||
| markedAsSpoiler?: boolean; | ||
| spoilerReason?: string; | ||
| imgWidth?: number; | ||
| imgHeight?: number; | ||
| resizeMethod?: string; | ||
| renderViewer: (props: RenderViewerProps) => ReactNode; | ||
| renderImage: (props: RenderImageProps) => ReactNode; | ||
| }; | ||
|
|
@@ -70,6 +73,9 @@ export const ImageContent = as<'div', ImageContentProps>( | |
| autoPlay, | ||
| markedAsSpoiler, | ||
| spoilerReason, | ||
| imgWidth, | ||
| imgHeight, | ||
| resizeMethod, | ||
| renderViewer, | ||
| renderImage, | ||
| ...props | ||
|
|
@@ -84,26 +90,36 @@ export const ImageContent = as<'div', ImageContentProps>( | |
| const [error, setError] = useState(false); | ||
| const [viewer, setViewer] = useState(false); | ||
| const [blurred, setBlurred] = useState(markedAsSpoiler ?? false); | ||
| const [useThumbnail, setUseThumbnail] = useState(imgWidth && imgHeight && resizeMethod); | ||
|
|
||
| const [srcState, loadSrc] = useAsyncCallback( | ||
| useCallback(async () => { | ||
| const mediaUrl = mxcUrlToHttp(mx, url, useAuthentication) ?? url; | ||
| const mediaUrl = (useThumbnail ? mxcUrlToHttp(mx, url, useAuthentication, imgWidth, imgHeight, resizeMethod) : mxcUrlToHttp(mx, url, useAuthentication)) ?? url; | ||
| if (encInfo) { | ||
| const fileContent = await downloadEncryptedMedia(mediaUrl, (encBuf) => | ||
| decryptFile(encBuf, mimeType ?? FALLBACK_MIMETYPE, encInfo) | ||
| ); | ||
| const decrypt = (encBuf: ArrayBuffer) => decryptFile(encBuf, mimeType ?? FALLBACK_MIMETYPE, encInfo); | ||
| let fileContent; | ||
| try { | ||
| fileContent = await downloadEncryptedMedia(mediaUrl, decrypt); | ||
| } catch { | ||
| fileContent = await downloadEncryptedMedia(mxcUrlToHttp(mx, url, useAuthentication) ?? url, decrypt); | ||
| } | ||
|
Comment on lines
+99
to
+105
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should always use the original url for encrypted image as it is always going to fail to generate thumbnail for it. |
||
| return URL.createObjectURL(fileContent); | ||
| } | ||
| return mediaUrl; | ||
| }, [mx, url, useAuthentication, mimeType, encInfo]) | ||
| }, [mx, url, useAuthentication, mimeType, encInfo, useThumbnail, imgWidth, imgHeight, resizeMethod]) | ||
| ); | ||
|
|
||
| const handleLoad = () => { | ||
| setLoad(true); | ||
| }; | ||
| const handleError = () => { | ||
| setLoad(false); | ||
| setError(true); | ||
| if (useThumbnail) { | ||
| setUseThumbnail(false); | ||
| loadSrc(); | ||
| } else { | ||
| setLoad(false); | ||
| setError(true); | ||
| } | ||
|
Comment on lines
+116
to
+122
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any particular reason to use this fallback strategy? |
||
| }; | ||
|
|
||
| const handleRetry = () => { | ||
|
|
@@ -134,7 +150,7 @@ export const ImageContent = as<'div', ImageContentProps>( | |
| onContextMenu={(evt: any) => evt.stopPropagation()} | ||
| > | ||
| {renderViewer({ | ||
| src: srcState.data, | ||
| src: useThumbnail ? (mxcUrlToHttp(mx, url, useAuthentication) ?? url) : srcState.data, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since encrypted message always going to use full image we should use |
||
| alt: body, | ||
| requestClose: () => setViewer(false), | ||
| })} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ export type IImageContent = { | |
| url?: string; | ||
| info?: IImageInfo & IThumbnailContent; | ||
| file?: IEncryptedFile; | ||
| info?: { h: number, w: number }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| [MATRIX_SPOILER_PROPERTY_NAME]?: boolean; | ||
| [MATRIX_SPOILER_REASON_PROPERTY_NAME]?: string; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IImageContentalready provide the thumbnail image along with dimensions, should not we be passing that toImageContentcomponent?