/** * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug * Copyright (c) * Released under the AGPL-3.0 License. */ import React, { useState, useEffect } from 'react'; import { observer } from 'mobx-react'; import { ThunderboltOutlined, QuestionCircleOutlined, UploadOutlined, CloudServerOutlined, BulbOutlined, } from '@ant-design/icons'; import { Form, Button, Tooltip, Space, Card, Table, Input, Upload, message } from 'antd'; import { AuthDiv, Breadcrumb } from 'components'; import HostSelector from 'pages/host/Selector'; import Output from './Output'; import { http, uniqueId } from 'libs'; import moment from 'moment'; import store from './store'; import style from './index.module.less'; function TransferIndex() { const [loading, setLoading] = useState(false) const [files, setFiles] = useState([]) const [dir, setDir] = useState('') const [hosts, setHosts] = useState([]) const [percent, setPercent] = useState() const [token, setToken] = useState() const [histories, setHistories] = useState([]) useEffect(() => { if (!loading) { http.get('/api/exec/transfer/') .then(res => setHistories(res)) } }, [loading]) function _handleProgress(e) { const data = e.loaded / e.total * 100 if (!percent && data === 100) return setPercent(String(data).replace(/(\d+\.\d).*/, '$1')) } function handleSubmit() { const formData = new FormData(); if (files.length === 0) return message.error('请添加数据源') if (!dir) return message.error('请输入目标路径') if (hosts.length === 0) return message.error('请选择目标主机') const data = {dst_dir: dir, host_ids: hosts.map(x => x.id)} for (let index in files) { const item = files[index] if (item.type === 'host') { data.host = JSON.stringify([item.host_id, item.path]) } else { formData.append(`file${index}`, item.path) } } formData.append('data', JSON.stringify(data)) setLoading(true) http.post('/api/exec/transfer/', formData, {timeout: 600000, onUploadProgress: _handleProgress}) .then(res => { const tmp = {} for (let host of hosts) { tmp[host.id] = { title: `${host.name}(${host.hostname}:${host.port})`, data: '\x1b[36m### WebSocket connecting ...\x1b[0m', status: -2 } } store.outputs = tmp setToken(res) }) .finally(() => { setLoading(false) setPercent() }) } function makeFile(row) { setFiles([{ id: uniqueId(), type: 'host', name: row.name, path: '', host_id: row.id }]) } function handleUpload(_, fileList) { const tmp = files.length > 0 && files[0].type === 'upload' ? [...files] : [] for (let file of fileList) { tmp.push({id: uniqueId(), type: 'upload', name: '本地上传', path: file}) } setFiles(tmp) return Upload.LIST_IGNORE } function handleRemove(index) { files.splice(index, 1) setFiles([...files]) } function handleCloseOutput() { setToken() if (!store.counter['0'] && !store.counter['2']) { setFiles([]) } } return ( 首页 批量执行 文件分发 {token ? : null} ) } export default observer(TransferIndex)