Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Improve MImageBody error handling (#9663)
Browse files Browse the repository at this point in the history
* Improve MImageBody error handling

* Fix strict errors

* We can assert this as isAnimated would be false if no content.info.mimetype
  • Loading branch information
t3chguy committed Dec 5, 2022
1 parent 7065c58 commit 5f76528
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/components/views/messages/MImageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {

// Set a placeholder image when we can't decrypt the image.
this.setState({ error });
return;
}
} else {
thumbUrl = this.getThumbUrl();
Expand All @@ -291,16 +292,27 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
img.crossOrigin = "Anonymous"; // CORS allow canvas access
img.src = contentUrl;

await loadPromise;

const blob = await this.props.mediaEventHelper.sourceBlob.value;
if (!await blobIsAnimated(content.info.mimetype, blob)) {
isAnimated = false;
try {
await loadPromise;
} catch (error) {
logger.error("Unable to download attachment: ", error);
this.setState({ error: error as Error });
return;
}

if (isAnimated) {
const thumb = await createThumbnail(img, img.width, img.height, content.info.mimetype, false);
thumbUrl = URL.createObjectURL(thumb.thumbnail);
try {
const blob = await this.props.mediaEventHelper.sourceBlob.value;
if (!await blobIsAnimated(content.info?.mimetype, blob)) {
isAnimated = false;
}

if (isAnimated) {
const thumb = await createThumbnail(img, img.width, img.height, content.info!.mimetype, false);
thumbUrl = URL.createObjectURL(thumb.thumbnail);
}
} catch (error) {
// This is a non-critical failure, do not surface the error or bail the method here
logger.warn("Unable to generate thumbnail for animated image: ", error);
}
}
}
Expand Down

0 comments on commit 5f76528

Please sign in to comment.