import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { withRouter } from 'react-router'; import produce from 'immer'; import { Modal, Button, message } from 'antd'; import Peer from '../Peer'; import ws from '../ws'; import { formatBytes, } from '../common/util'; import styles from './RecvFileModal.cm.styl'; class RecvFileModal extends Component { constructor(props) { super(props); this.state = { receivingFileId: null, downloadUrls: {}, recvSizes: {}, targetId: '', message: '', files: [], peerState: '', }; this.onDownload = this.onDownload.bind(this); this.onRecvData = this.onRecvData.bind(this); this.handleMsg = this.handleMsg.bind(this); this.handlePrepareDownloadRes = this.handlePrepareDownloadRes.bind(this); this.recvBuffer = []; this.recvSizes = {}; this.timer = setInterval(() => { this.setState(produce(draft => { draft.recvSizes = { ...this.recvSizes }; })); }, 1000); } componentDidMount() { ws.sendJSON({ type: 'c2s_prepare_download', payload: { downloadCode: this.props.match.params.downloadCode, }, }); ws.registerMessageHandler('s2c_prepare_download', this.handlePrepareDownloadRes); } handlePrepareDownloadRes(payload) { this.setState(produce(draft => { draft.showRecvFileModal = true; draft.targetId = payload.clientId; draft.message = payload.message; draft.files = payload.files; })); ws.removeMessageHandler('s2c_prepare_download', this.handlePrepareDownloadRes); } componentWillUnmount() { clearInterval(this.timer); } onDownload() { const peer = new Peer(); peer.on('connecting', () => { this.setState({ peerState: '正在连接', }); }); peer.on('connected', () => { message.success('已连接'); this.setState({ peerState: '已连接', }); }); peer.on('disconnected', async() => { message.error('连接断开'); this.setState(produce(draft => { draft.peerState = '未连接'; })); }); peer.on('connectFailed', () => { message.error('连接失败'); this.setState({ peerState: '连接失败', }); }); peer.connectPeer(this.state.targetId); peer.on('data', this.onRecvData); } onRecvData(data) { if (typeof data === 'string') { const msg = JSON.parse(data); this.handleMsg(msg); } else { this.recvBuffer.push(data); const curRecvBytes = this.recvSizes[this.state.receivingFileId] || 0; const newRecvBytes = curRecvBytes + data.byteLength; this.recvSizes[this.state.receivingFileId] = newRecvBytes; } } handleMsg(msg) { if (msg.type === 'fileStart') { this.setState({ receivingFileId: msg.fileId, }); } else if (msg.type === 'fileEnd') { const fileId = msg.fileId; const blob = new Blob(this.recvBuffer); const url = window.URL.createObjectURL(blob); this.setState(produce(draft => { draft.downloadUrls[fileId] = url; })); } } render() { const { isOpen, } = this.props; const { message, files, } = this.state; if (!isOpen) { return null; } const totalBytes = files.reduce((sum, cur) => { return sum + cur.size; }, 0); return (

对方发送给您以下文件及信息:

{!!message && (
{message}
)}
{files.map(f => { const { uid, name, size, } = f; const downloadUrl = this.state.downloadUrls[uid]; const recvBytes = this.state.recvSizes[uid] || 0; let downloadInfo = null; if (downloadUrl) { downloadInfo = 下载; } else if (uid === this.state.receivingFileId) { downloadInfo = {((recvBytes / size) * 100).toFixed(2)}%; } else { downloadInfo = 等待中; } return (
{name} {formatBytes(size)} {downloadInfo}
); })}
{files.length} 个文件,共 {formatBytes(totalBytes)}
); } } RecvFileModal.propTypes = { isOpen: PropTypes.bool, onCancel: PropTypes.func, match: PropTypes.object, }; export default withRouter(RecvFileModal);