Skip to content

Commit

Permalink
fix(media): [closes #311] don't attempt to render unsupported media
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Jun 8, 2024
1 parent da529ef commit 56dbbf2
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/components/Message/InlineMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,59 @@ interface InlineFileProps {
file: TorrentFile
}

// NOTE: These filename extensions are copied from render-media, the upstream
// library used to embed media files:
// https://github.com/feross/render-media/blob/a445b2ab90fcd4a248552d32027b2bc6a02600c8/index.js#L15-L72
const supportedImageExtensions = [
'.bmp',
'.gif',
'.jpeg',
'.jpg',
'.png',
'.svg',
]

const supportedAudioExtensions = ['.aac', '.oga', '.ogg', '.wav', '.flac']

const supportedMediaExtensions = [
...supportedImageExtensions,
...supportedAudioExtensions,
]

export const InlineFile = ({ file }: InlineFileProps) => {
const containerRef = useRef(null)
const [didRenderingMediaFail, setDidRenderingMediaFail] = useState(false)
const [isMediaSupported, setIsMediaSupported] = useState(true)
const shellContext = useContext(ShellContext)

useEffect(() => {
;(async () => {
const { current: container } = containerRef
const { current: container } = containerRef

if (!container) return
if (!container) return

try {
file.appendTo(container)
} catch (e) {
console.error(e)
setDidRenderingMediaFail(true)
}
})()
const { name } = file
const fileNameExtension = name.split('.').pop() ?? ''

if (!supportedMediaExtensions.includes(`.${fileNameExtension}`)) {
setIsMediaSupported(false)
return
}

try {
file.appendTo(container)
} catch (e) {
console.error(e)
setDidRenderingMediaFail(true)
}
}, [file, containerRef, shellContext.roomId])

return (
<Box ref={containerRef} sx={{ '& img': { maxWidth: '100%' } }}>
{!isMediaSupported && (
<Typography sx={{ fontStyle: 'italic' }}>
Media preview not supported
</Typography>
)}
{didRenderingMediaFail && (
<Typography sx={{ fontStyle: 'italic' }}>
Media failed to render
Expand Down

0 comments on commit 56dbbf2

Please sign in to comment.