import React from 'react'; import { FormattedMessage, defineMessages, injectIntl } from 'react-intl'; import SendMessage from './send-message.jsx'; import { bytesToHumanSize } from '../lib/strformat.js'; const messages = defineMessages({ unrecognized_video_format: { id: 'unrecognized_video_format', defaultMessage: 'Format of this video is not recognized', description: 'Error message when uploaded video is invalid', } }); class VideoPreview extends React.PureComponent { constructor(props) { super(props); this.videoRef = React.createRef(); this.handleSendVideo = this.handleSendVideo.bind(this); this.handleKeyDown = this.handleKeyDown.bind(this); } componentDidMount() { document.addEventListener('keydown', this.handleKeyDown); } componentWillUnmount() { document.removeEventListener('keydown', this.handleKeyDown); } handleKeyDown(e) { if (this.props.onSendMessage) { return; } e.preventDefault(); if (e.key === 'Escape') { this.props.onClose(); } } handleSendVideo(caption) { this.props.onClose(); const params = { width: this.videoRef.current.videoWidth, height: this.videoRef.current.videoHeight, duration: (this.videoRef.current.duration * 1000) | 0, mime: this.props.content.mime, name: this.props.content.filename } if (params.width == 0 || params.height == 0) { this.props.onError(this.props.intl.formatMessage(messages.unrecognized_video_format), 'err'); return; } // Capture screen from a video. const canvas = document.createElement('canvas'); canvas.width = params.width; canvas.height = params.height; const ctx = canvas.getContext('2d'); ctx.drawImage(this.videoRef.current, 0, 0, canvas.width, canvas.height); ctx.canvas.toBlob( preview => this.props.onSendMessage(caption, this.props.content.blob, preview, params), 'image/jpeg', 0.75 ); } render() { if (!this.props.content) { return null; } const width = this.props.content.width || '-'; const height = this.props.content.height || '-'; const controlist = this.props.onSendMessage ? 'nodownload' : ''; const autoPlay = !this.props.onSendMessage; return (