File size: 5,298 Bytes
181e22f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | 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 (
<div className={styles.base}>
<Modal
title="接收文件"
footer={null}
visible={isOpen}
onCancel={this.props.onCancel}
>
<h4>对方发送给您以下文件及信息:</h4>
{!!message && (
<div className={styles.message}>{message}</div>
)}
<div className={styles.fileBox}>
{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 = <a href={downloadUrl} download={name}>下载</a>;
} else if (uid === this.state.receivingFileId) {
downloadInfo = <span>{((recvBytes / size) * 100).toFixed(2)}%</span>;
} else {
downloadInfo = <span>等待中</span>;
}
return (
<div key={uid} className={styles.file}>
<span className={styles.fileName}>
{name}
</span>
<span>{formatBytes(size)}</span>
{downloadInfo}
</div>
);
})}
</div>
<div className={styles.summary}>
{files.length} 个文件,共 {formatBytes(totalBytes)}
</div>
<Button type="primary" block size="large" onClick={this.onDownload}>
开始下载
</Button>
</Modal>
</div>
);
}
}
RecvFileModal.propTypes = {
isOpen: PropTypes.bool,
onCancel: PropTypes.func,
match: PropTypes.object,
};
export default withRouter(RecvFileModal);
|