import React, { Component } from 'react'; import PropTypes from 'prop-types'; import CopyToClipboard from 'react-copy-to-clipboard'; import prettyBytes from 'pretty-bytes'; import classnames from 'classnames'; import uuidv4 from 'uuid/v4'; import { Link, } from 'react-router-dom'; import QRCode from 'qrcode.react'; import Dropzone from 'react-dropzone'; import Icon from './common/Icon'; import Button from './common/Button'; import Steps from './common/Steps'; import Toast from './common/Toast'; import FileBox from './FileBox'; import Peer from '../Peer'; import FileChunker from '../FileChunker'; import { calcPercent } from '../common/util'; import { prepareSend, deleteRecvCode } from '../actions/file'; import styles from './SendFilePanel.cm.styl'; const Step = Steps.Step; class SendFilePanel extends Component { constructor(props) { super(props); this.peer = new Peer(); this.inputRef = React.createRef(); this.onChangeFile = this.onChangeFile.bind(this); this.onRemoveFile = this.onRemoveFile.bind(this); this.onClickSelectDone = this.onClickSelectDone.bind(this); this.onClickBack = this.onClickBack.bind(this); this.onReset = this.onReset.bind(this); // 回到初始状态,用在点击取消或发送完成继续发送等地方 this.onRecvPeerData = this.onRecvPeerData.bind(this); this.handlePeerMsg = this.handlePeerMsg.bind(this); this.sendSizes = {}; this.lastPeerRecvBytes = 0; // (上次)接收方接收到的字节数,用于计算bps this.bps = 0; this.timer = setInterval(() => { const files = this.props.files.map(f => { const sendSize = this.sendSizes[f.uid] || 0; const pct = calcPercent(sendSize, f.size); return { ...f, pct, }; }); this.props.setState({ files, bps: this.bps }); this.bps = 0; }, 1000); } componentDidMount() { document.title = '小鹿快传|简单安全高效的P2P文件传输服务 - 发送文件'; } componentWillUnmount() { clearInterval(this.timer); } onClickSelectDone() { this.props.setState({ waitingPrepareSend: true, }); const files = this.props.files.map(f => { return { uid: f.uid, name: f.name, size: f.size, type: f.type, }; }); prepareSend(files); const peer = this.peer; peer.on('connecting', () => { this.props.setState({ peerState: 'connecting', }); }); peer.on('connected', () => { this.props.setState({ peerState: 'connected', }); }); peer.on('connectFailed', () => { this.props.setState({ peerState: 'connectFailed', }); Toast.error('连接失败'); }); peer.on('disconnected', async() => { this.props.setState({ peerState: 'disconnected', }); }); peer.on('channelOpen', async() => { this.props.setState({ curStep: 3, peerState: 'transfer', }); const files = this.props.files; for (let i = 0; i < files.length; i++) { const file = files[i]; const fileId = file.uid; await peer.sendJSON({ type: 'fileStart', fileId, }); this.props.setState({ curFileId: fileId, }); const chunker = new FileChunker(file.realFile); let done = false; this.lastPeerRecvBytes = 0; while (!done) { const result = await chunker.getNextChunk(); done = result.done; const { chunk, } = result; try { await peer.send(chunk); } catch (err) { Toast.error('传输错误:' + err); break; } } if (done) { await peer.sendJSON({ type: 'fileEnd', fileId, }); } } }); peer.on('data', this.onRecvPeerData); } onRecvPeerData(data) { if (typeof data === 'string') { const msg = JSON.parse(data); this.handlePeerMsg(msg); } } handlePeerMsg(msg) { const { type, payload, } = msg; if (type === 'chunkReceived') { const { fileId, recvBytes, } = payload; this.sendSizes[fileId] = recvBytes; this.bps += (recvBytes - this.lastPeerRecvBytes); this.lastPeerRecvBytes = recvBytes; } } onClickBack() { deleteRecvCode(this.props.recvCode); this.props.setState({ curStep: this.props.curStep - 1 }); } onReset() { this.peer.destroy(); this.props.setState({ curStep: 1, files: [], peerState: '', waitingPrepareSend: false, }); } onChangeFile(files) { const filteredFiles = files.filter(f => { const existed = this.props.files.find(_f1 => { const f1 = _f1.realFile; if (f1.name === f.name && f1.size === f.size && f1.lastModified === f.lastModified && f1.type === f.type) { return true; } return false; }); return !existed; }).map(f => { return { realFile: f, uid: uuidv4(), name: f.name, size: f.size, type: f.type, }; }); if (filteredFiles.length !== files.length) { Toast.info('发现疑似相同的文件,已自动过滤'); } const nextFiles = this.props.files.concat(filteredFiles); this.props.setState({ files: nextFiles, }); event.target.value = null; } onRemoveFile(uid) { return () => { const nextFiles = this.props.files.filter(f => f.uid !== uid); this.props.setState({ files: nextFiles }); }; } renderStep1() { const { files, waitingPrepareSend, } = this.props; const totalBytes = files.reduce((sum, cur) => { return sum + cur.size; }, 0); return ( {({ getRootProps, getInputProps, isDragActive, open }) => ( <>
{files.length === 0 && ( <>
将文件拖动到方框内或点击下方按钮
)} {files.length > 0 && ( )}
{files.length === 0 && (
已有收件码?点击 接收文件
)} {files.length > 0 && ( <>
open()}>添加文件
{files.length} 个文件,共 {prettyBytes(totalBytes)}
)} )}
); } renderStep2() { const { recvCode, } = this.props; const recvLink = `http://${document.location.host}/recv/${recvCode}`; return ( <>
请选择发送方式:
1. 通过链接发送(对方打开链接即可下载文件)
{recvLink} Toast.success('复制成功')}> 复制
2. 通过6位数取件码(对方在小鹿快传网站输入即可下载文件)
{recvCode} Toast.success('复制成功')}> 复制
3. 扫描下方二维码:
温馨提示:
以上取件码在10分钟内有效,请尽快发送给对方
对方输入取件码并确认之后会自动开始发送
); } renderStep3() { const { curFileId, files, peerState, } = this.props; const totalBytes = files.reduce((sum, cur) => { return sum + cur.size; }, 0); let allCompleted = true; files.forEach(f => { if (f.pct < 100) { allCompleted = false; } }); let btnContent; if (allCompleted) { btnContent = '继续发送'; } else if (peerState === 'connecting') { btnContent = '正在连接...'; } else if (peerState === 'connected') { btnContent = '连接成功'; } else if (peerState === 'transfer') { btnContent = `正在发送...(${prettyBytes(this.bps || 0)}/s)`; } else if (peerState === 'disconnected' || peerState === 'connectFailed') { btnContent = '连接断开,等待重连...'; } let loading = true; if (allCompleted) { loading = false; } return ( <>
{files.length}个文件,共{prettyBytes(totalBytes)}
); } render() { const { curStep, files, } = this.props; return (
{((curStep === 1 && files.length > 0) || (curStep === 3)) && (
取消
)} {curStep === 2 && (
返回
)}
发送文件
{curStep === 1 && this.renderStep1()} {curStep === 2 && this.renderStep2()} {curStep === 3 && this.renderStep3()}
); } } SendFilePanel.propTypes = { curStep: PropTypes.number, files: PropTypes.array, waitingPrepareSend: PropTypes.bool, curFileId: PropTypes.string, peerState: PropTypes.string, recvCode: PropTypes.string, setState: PropTypes.func, }; export default SendFilePanel;