fixed crashes on bad media data

This commit is contained in:
unknown 2021-08-18 13:55:44 +05:30
parent d7e3e70430
commit c23be53bfd
2 changed files with 29 additions and 20 deletions

View file

@ -137,11 +137,12 @@ function File({
} }
File.defaultProps = { File.defaultProps = {
file: null, file: null,
type: '',
}; };
File.propTypes = { File.propTypes = {
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
link: PropTypes.string.isRequired, link: PropTypes.string.isRequired,
type: PropTypes.string.isRequired, type: PropTypes.string,
file: PropTypes.shape({}), file: PropTypes.shape({}),
}; };
@ -176,6 +177,7 @@ Image.defaultProps = {
file: null, file: null,
width: null, width: null,
height: null, height: null,
type: '',
}; };
Image.propTypes = { Image.propTypes = {
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
@ -183,7 +185,7 @@ Image.propTypes = {
height: PropTypes.number, height: PropTypes.number,
link: PropTypes.string.isRequired, link: PropTypes.string.isRequired,
file: PropTypes.shape({}), file: PropTypes.shape({}),
type: PropTypes.string.isRequired, type: PropTypes.string,
}; };
function Audio({ function Audio({
@ -220,11 +222,12 @@ function Audio({
} }
Audio.defaultProps = { Audio.defaultProps = {
file: null, file: null,
type: '',
}; };
Audio.propTypes = { Audio.propTypes = {
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
link: PropTypes.string.isRequired, link: PropTypes.string.isRequired,
type: PropTypes.string.isRequired, type: PropTypes.string,
file: PropTypes.shape({}), file: PropTypes.shape({}),
}; };
@ -287,6 +290,7 @@ Video.defaultProps = {
height: null, height: null,
file: null, file: null,
thumbnail: null, thumbnail: null,
type: '',
thumbnailType: null, thumbnailType: null,
thumbnailFile: null, thumbnailFile: null,
}; };
@ -297,7 +301,7 @@ Video.propTypes = {
width: PropTypes.number, width: PropTypes.number,
height: PropTypes.number, height: PropTypes.number,
file: PropTypes.shape({}), file: PropTypes.shape({}),
type: PropTypes.string.isRequired, type: PropTypes.string,
thumbnailFile: PropTypes.shape({}), thumbnailFile: PropTypes.shape({}),
thumbnailType: PropTypes.string, thumbnailType: PropTypes.string,
}; };

View file

@ -61,10 +61,15 @@ function isMedia(mE) {
function genMediaContent(mE) { function genMediaContent(mE) {
const mx = initMatrix.matrixClient; const mx = initMatrix.matrixClient;
const mContent = mE.getContent(); const mContent = mE.getContent();
let mediaMXC = mContent.url; if (!mContent || !mContent.body) return <span style={{ color: 'var(--bg-danger)' }}>Malformed event</span>;
let thumbnailMXC = mContent?.info?.thumbnail_url;
let mediaMXC = mContent?.url;
const isEncryptedFile = typeof mediaMXC === 'undefined'; const isEncryptedFile = typeof mediaMXC === 'undefined';
if (isEncryptedFile) mediaMXC = mContent.file.url; if (isEncryptedFile) mediaMXC = mContent?.file?.url;
let thumbnailMXC = mContent?.info?.thumbnail_url;
if (typeof mediaMXC === 'undefined' || mediaMXC === '') return <span style={{ color: 'var(--bg-danger)' }}>Malformed event</span>;
switch (mE.getContent()?.msgtype) { switch (mE.getContent()?.msgtype) {
case 'm.file': case 'm.file':
@ -72,19 +77,19 @@ function genMediaContent(mE) {
<Media.File <Media.File
name={mContent.body} name={mContent.body}
link={mx.mxcUrlToHttp(mediaMXC)} link={mx.mxcUrlToHttp(mediaMXC)}
file={mContent.file} type={mContent.info?.mimetype}
type={mContent.info.mimetype} file={mContent.file || null}
/> />
); );
case 'm.image': case 'm.image':
return ( return (
<Media.Image <Media.Image
name={mContent.body} name={mContent.body}
width={mContent.info.w || null} width={typeof mContent.info?.w === 'number' ? mContent.info?.w : null}
height={mContent.info.h || null} height={typeof mContent.info?.h === 'number' ? mContent.info?.h : null}
link={mx.mxcUrlToHttp(mediaMXC)} link={mx.mxcUrlToHttp(mediaMXC)}
file={isEncryptedFile ? mContent.file : null} file={isEncryptedFile ? mContent.file : null}
type={mContent.info.mimetype} type={mContent.info?.mimetype}
/> />
); );
case 'm.audio': case 'm.audio':
@ -92,8 +97,8 @@ function genMediaContent(mE) {
<Media.Audio <Media.Audio
name={mContent.body} name={mContent.body}
link={mx.mxcUrlToHttp(mediaMXC)} link={mx.mxcUrlToHttp(mediaMXC)}
type={mContent.info.mimetype} type={mContent.info?.mimetype}
file={mContent.file} file={mContent.file || null}
/> />
); );
case 'm.video': case 'm.video':
@ -105,16 +110,16 @@ function genMediaContent(mE) {
name={mContent.body} name={mContent.body}
link={mx.mxcUrlToHttp(mediaMXC)} link={mx.mxcUrlToHttp(mediaMXC)}
thumbnail={thumbnailMXC === null ? null : mx.mxcUrlToHttp(thumbnailMXC)} thumbnail={thumbnailMXC === null ? null : mx.mxcUrlToHttp(thumbnailMXC)}
thumbnailFile={isEncryptedFile ? mContent.info.thumbnail_file : null} thumbnailFile={isEncryptedFile ? mContent.info?.thumbnail_file : null}
thumbnailType={mContent.info.thumbnail_info?.mimetype || null} thumbnailType={mContent.info?.thumbnail_info?.mimetype || null}
width={mContent.info.w || null} width={typeof mContent.info?.w === 'number' ? mContent.info?.w : null}
height={mContent.info.h || null} height={typeof mContent.info?.h === 'number' ? mContent.info?.h : null}
file={isEncryptedFile ? mContent.file : null} file={isEncryptedFile ? mContent.file : null}
type={mContent.info.mimetype} type={mContent.info?.mimetype}
/> />
); );
default: default:
return 'Unable to attach media file!'; return <span style={{ color: 'var(--bg-danger)' }}>Malformed event</span>;
} }
} }