import React, { Component, Fragment } from 'react'; import produce from 'immer'; import PropTypes from 'prop-types'; import { message as toast, Modal, Input, Empty, Button, Icon, Upload, Steps } from 'antd'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import QRCode from 'qrcode.react'; import Peer from '../Peer'; import FileChunker from '../FileChunker'; import { formatBytes, } from '../common/util'; import styles from './SendFileModal.cm.styl'; const { Step } = Steps; function getInitialState(props) { return { currentStep: 0, fileList: props ? props.initFileList : [], message: '', curFileId: '', sendSizes: {}, peerState: '', // 连接状态 }; } class SendFileModal extends Component { constructor(props) { super(props); this.state = getInitialState(props); this.onChangeFile = this.onChangeFile.bind(this); this.onRemoveFile = this.onRemoveFile.bind(this); this.onChangeMessage = this.onChangeMessage.bind(this); this.onPrepareUpload = this.onPrepareUpload.bind(this); this.renderStep0Content = this.renderStep0Content.bind(this); this.renderStep1Content = this.renderStep1Content.bind(this); this.renderStep2Content = this.renderStep2Content.bind(this); this.sendSizes = {}; setInterval(() => { this.setState(produce(draft => { draft.sendSizes = { ...this.sendSizes }; })); }, 1000); } onPrepareUpload() { const { message, fileList, } = this.state; this.setState(produce(draft => { draft.currentStep = 1; })); const files = fileList.map(f => { return { uid: f.uid, name: f.name, size: f.size, type: f.type, }; }); this.props.prepareUpload(message, files); const peer = new Peer(); peer.on('connecting', () => { this.setState({ peerState: '正在连接', }); }); peer.on('connected', () => { toast.success('已连接'); this.setState({ peerState: '已连接', }); }); peer.on('connectFailed', () => { toast.error('连接失败'); this.setState({ peerState: '连接失败', }); }); peer.on('disconnected', async() => { toast.error('连接断开'); this.setState(produce(draft => { draft.peerState = '未连接'; })); }); peer.on('channelOpen', async() => { this.setState(produce(draft => { if (draft.currentStep === 1) { draft.currentStep = 2; } })); for (let i = 0; i < fileList.length; i++) { const file = fileList[i]; const uid = file.uid; await peer.sendJSON({ type: 'fileStart', fileId: file.uid, }); this.setState({ curFileId: uid }); const chunker = new FileChunker(file); let done = false; while (!done) { const result = await chunker.getNextChunk(); done = result.done; const { chunk, offset, } = result; try { await peer.send(chunk); } catch (err) { toast.error('传输错误:' + err); break; } this.sendSizes[uid] = offset; } if (done) { await peer.sendJSON({ type: 'fileEnd', fileId: file.uid, }); } } }); } onChangeFile(e) { this.setState(produce(draft => { draft.fileList.push(e.file); })); } onChangeMessage(e) { this.setState({ message: e.target.value, }); } componentDidUpdate(prevProps, prevState) { if (this.props.initFileList !== prevProps.initFileList) { this.setState({ fileList: this.state.fileList.concat(this.props.initFileList), }); } if (!this.props.isOpen && !!prevProps.isOpen) { this.setState(getInitialState()); } } onRemoveFile(uid) { return () => { this.setState({ fileList: this.state.fileList.filter(f => f.uid !== uid), }); }; } renderFileList() { const { fileList, } = this.state; return fileList.map(f => { return (