diff --git a/frontend/src/components/dialog/share-link-zip-download-dialog.js b/frontend/src/components/dialog/share-link-zip-download-dialog.js
deleted file mode 100644
index 5fe1100008..0000000000
--- a/frontend/src/components/dialog/share-link-zip-download-dialog.js
+++ /dev/null
@@ -1,127 +0,0 @@
-import React from 'react';
-import PropTypes from 'prop-types';
-import { Modal, ModalHeader, ModalBody } from 'reactstrap';
-import { gettext, fileServerRoot } from '../../utils/constants';
-import { seafileAPI } from '../../utils/seafile-api';
-import Loading from '../loading';
-
-const propTypes = {
- token: PropTypes.string.isRequired,
- path: PropTypes.string.isRequired,
- toggleDialog: PropTypes.func.isRequired
-};
-
-let interval;
-
-class ShareLinkZipDownloadDialog extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- isLoading: true,
- errorMsg: '',
- zipProgress: null
- };
- }
-
- componentDidMount() {
- const { token, path } = this.props;
- seafileAPI.getShareLinkZipTask(token, path).then((res) => {
- const zipToken = res.data['zip_token'];
- this.setState({
- isLoading: false,
- errorMsg: '',
- zipToken: zipToken
- });
- this.queryZipProgress();
- interval = setInterval(this.queryZipProgress, 1000);
- }).catch((error) => {
- let errorMsg = '';
- if (error.response) {
- errorMsg = gettext('Error');
- } else {
- errorMsg = gettext('Please check the network.');
- }
- this.setState({
- isLoading: false,
- errorMsg: errorMsg
- });
- });
- }
-
- queryZipProgress = () => {
- const zipToken = this.state.zipToken;
- seafileAPI.queryZipProgress(zipToken).then((res) => {
- const data = res.data;
- this.setState({
- zipProgress: data.total == 0 ? '100%' : (data.zipped/data.total*100).toFixed(2) + '%'
- });
- if (data['total'] == data['zipped']) {
- clearInterval(interval);
- this.props.toggleDialog();
- location.href = `${fileServerRoot}zip/${zipToken}`;
- }
- }).catch((error) => {
- clearInterval(interval);
- let errorMsg = '';
- if (error.response) {
- errorMsg = gettext('Error');
- } else {
- errorMsg = gettext('Please check the network.');
- }
- this.setState({
- isLoading: false,
- errorMsg: errorMsg
- });
- });
- }
-
- cancelZipTask = () => {
- const zipToken = this.state.zipToken;
- seafileAPI.cancelZipTask(zipToken).then((res) => {
- // do nothing
- }).catch((error) => {
- // do nothing
- });
- }
-
- toggleDialog = () => {
- const zipProgress = this.state.zipProgress;
- if (zipProgress && zipProgress != '100%') {
- clearInterval(interval);
- this.cancelZipTask();
- }
- this.props.toggleDialog();
- }
-
- render() {
- return (
-
{errorMsg}
; - } - - return{`${gettext('Packaging...')} ${zipProgress}`}
; - } -} - -ShareLinkZipDownloadDialog.propTypes = propTypes; - -export default ShareLinkZipDownloadDialog; diff --git a/frontend/src/components/dialog/zip-download-dialog.js b/frontend/src/components/dialog/zip-download-dialog.js index ff8ccf26e6..b1613b37fd 100644 --- a/frontend/src/components/dialog/zip-download-dialog.js +++ b/frontend/src/components/dialog/zip-download-dialog.js @@ -1,30 +1,135 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { Modal, ModalHeader, ModalBody } from 'reactstrap'; +import { Modal, ModalHeader, ModalBody } from 'reactstrap'; +import { gettext, fileServerRoot } from '../../utils/constants'; +import { seafileAPI } from '../../utils/seafile-api'; +import Loading from '../loading'; const propTypes = { - onCancelDownload: PropTypes.func.isRequired, - progress: PropTypes.number.isRequired, + token: PropTypes.string, + path: PropTypes.string.isRequired, + repoID: PropTypes.string, + target: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.array + ]), + toggleDialog: PropTypes.func.isRequired }; -class ZipDownloadDialog extends React.Component { +let interval; - toggle = () => { - this.props.onCancelDownload(); +class ZipDownloadDialog extends React.Component { + constructor(props) { + super(props); + this.state = { + isLoading: true, + errorMsg: '', + zipProgress: null + }; + } + + componentDidMount() { + const { token, path, repoID, target } = this.props; + const getZipTask = token ? + seafileAPI.getShareLinkZipTask(token, path) : + seafileAPI.zipDownload(repoID, path, target); + getZipTask.then((res) => { + const zipToken = res.data['zip_token']; + this.setState({ + isLoading: false, + errorMsg: '', + zipToken: zipToken + }); + this.queryZipProgress(); + interval = setInterval(this.queryZipProgress, 1000); + }).catch((error) => { + let errorMsg = ''; + if (error.response) { + errorMsg = error.response.data.error_msg || gettext('Error'); + } else { + errorMsg = gettext('Please check the network.'); + } + this.setState({ + isLoading: false, + errorMsg: errorMsg + }); + }); + } + + queryZipProgress = () => { + const zipToken = this.state.zipToken; + seafileAPI.queryZipProgress(zipToken).then((res) => { + const data = res.data; + this.setState({ + zipProgress: data.total == 0 ? '100%' : (data.zipped/data.total*100).toFixed(2) + '%' + }); + if (data['total'] == data['zipped']) { + clearInterval(interval); + this.props.toggleDialog(); + location.href = `${fileServerRoot}zip/${zipToken}`; + } + }).catch((error) => { + clearInterval(interval); + let errorMsg = ''; + if (error.response) { + errorMsg = gettext('Error'); + } else { + errorMsg = gettext('Please check the network.'); + } + this.setState({ + isLoading: false, + errorMsg: errorMsg + }); + }); + } + + cancelZipTask = () => { + const zipToken = this.state.zipToken; + seafileAPI.cancelZipTask(zipToken).then((res) => { + // do nothing + }).catch((error) => { + // do nothing + }); + } + + toggleDialog = () => { + const zipProgress = this.state.zipProgress; + if (zipProgress && zipProgress != '100%') { + clearInterval(interval); + this.cancelZipTask(); + } + this.props.toggleDialog(); } render() { return ( -{errorMsg}
; + } + + return{`${gettext('Packaging...')} ${zipProgress}`}
; + } +} + ZipDownloadDialog.propTypes = propTypes; export default ZipDownloadDialog; diff --git a/frontend/src/components/dirent-list-view/dirent-list-item.js b/frontend/src/components/dirent-list-view/dirent-list-item.js index 34f164c8f1..38e01f8720 100644 --- a/frontend/src/components/dirent-list-view/dirent-list-item.js +++ b/frontend/src/components/dirent-list-view/dirent-list-item.js @@ -9,11 +9,10 @@ import URLDecorator from '../../utils/url-decorator'; import DirentMenu from './dirent-menu'; import Rename from '../rename'; import ModalPortal from '../modal-portal'; -import ZipDownloadDialog from '../dialog/zip-download-dialog'; import MoveDirentDialog from '../dialog/move-dirent-dialog'; import CopyDirentDialog from '../dialog/copy-dirent-dialog'; import ShareDialog from '../dialog/share-dialog'; -import toaster from '../toast'; +import ZipDownloadDialog from '../dialog/zip-download-dialog'; import '../../css/dirent-list-item.css'; @@ -51,8 +50,7 @@ class DirentListItem extends React.Component { this.state = { isOperationShow: false, highlight: false, - progress: 0, - isProgressDialogShow: false, + isZipDialogOpen: false, isMoveDialogShow: false, isCopyDialogShow: false, isShareDialogShow: false, @@ -61,7 +59,6 @@ class DirentListItem extends React.Component { isDragTipShow: false, isDropTipshow: false, }; - this.zipToken = null; } componentWillReceiveProps(nextProps) { @@ -296,16 +293,8 @@ class DirentListItem extends React.Component { let repoID = this.props.repoID; let direntPath = this.getDirentPath(dirent); if (dirent.type === 'dir') { - this.setState({isProgressDialogShow: true, progress: 0}); - seafileAPI.zipDownload(repoID, this.props.path, dirent.name).then(res => { - this.zipToken = res.data['zip_token']; - this.addDownloadAnimation(); - this.interval = setInterval(this.addDownloadAnimation, 1000); - }).catch((error) => { - clearInterval(this.interval); - this.setState({isProgressDialogShow: false}); - let errorMessage = error.response.data.error_msg; - toaster.danger(errorMessage); + this.setState({ + isZipDialogOpen: true }); } else { let url = URLDecorator.getUrl({type: 'download_file_url', repoID: repoID, filePath: direntPath}); @@ -313,34 +302,9 @@ class DirentListItem extends React.Component { } } - addDownloadAnimation = () => { - let _this = this; - let token = this.zipToken; - seafileAPI.queryZipProgress(token).then(res => { - let data = res.data; - let progress = data.total === 0 ? 100 : (data.zipped / data.total * 100).toFixed(0); - this.setState({progress: parseInt(progress)}); - - if (data['total'] === data['zipped']) { - this.setState({ - progress: 100 - }); - clearInterval(this.interval); - location.href = URLDecorator.getUrl({type: 'download_dir_zip_url', token: token}); - setTimeout(function() { - _this.setState({isProgressDialogShow: false}); - }, 500); - } - }); - } - - onCancelDownload = () => { - let zipToken = this.zipToken; - seafileAPI.cancelZipTask(zipToken).then(res => { - clearInterval(this.interval); - this.setState({ - isProgressDialogShow: false, - }); + closeZipDialog = () => { + this.setState({ + isZipDialogOpen: false }); } @@ -364,7 +328,7 @@ class DirentListItem extends React.Component { let dragStartItemData = {nodeDirent: this.props.dirent, nodeParentPath: this.props.path, nodeRootPath: nodeRootPath}; dragStartItemData = JSON.stringify(dragStartItemData); - e.dataTransfer.effectAllowed = "move"; + e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setDragImage(this.refs.drag_icon, 15, 15); e.dataTransfer.setData('applicaiton/drag-item-info', dragStartItemData); } @@ -560,11 +524,13 @@ class DirentListItem extends React.Component { /> } - {this.state.isProgressDialogShow && + {this.state.isZipDialogOpen &&