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 (