1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-05 08:53:14 +00:00

fix export file access log (#6944)

* fix

* update

* optimize export log

* update

* update

* update

* fomrat-update

---------

Co-authored-by: 孙永强 <11704063+s-yongqiang@user.noreply.gitee.com>
Co-authored-by: r350178982 <32759763+r350178982@users.noreply.github.com>
This commit is contained in:
awu0403
2024-10-28 11:50:56 +08:00
committed by GitHub
parent 2398a66570
commit 0d877b2287
7 changed files with 278 additions and 122 deletions

View File

@@ -7,6 +7,8 @@ import { orgAdminAPI } from '../../utils/org-admin-api';
import { userAPI } from '../../utils/user-api';
import toaster from '../../components/toast';
import { Utils } from '../../utils/utils';
import SeahubIODialog from '../dialog/seahub-io-dialog';
class OrgLogsExportExcelDialog extends React.Component {
@@ -18,6 +20,7 @@ class OrgLogsExportExcelDialog extends React.Component {
endDateStr: '',
errMsg: '',
taskId: '',
isShowIODialog: false,
};
}
@@ -38,34 +41,51 @@ class OrgLogsExportExcelDialog extends React.Component {
}
};
queryIOStatus = (task_id, logType) => {
userAPI.queryIOStatus(task_id).then(res => {
if (res.data.is_finished === true) {
this.setState({
isShowIODialog: false
});
this.props.toggle();
location.href = siteRoot + 'api/v2.1/org/admin/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
} else {
setTimeout(() => {
this.queryIOStatus(task_id, logType);
}, 1000);
}
}).catch(err => {
this.setState({
isShowIODialog: false
});
this.props.toggle();
toaster.danger(gettext('Failed to export. Please check whether the size of table attachments exceeds the limit.'));
});
};
orgAdminExportLogs = (logType) => {
let { startDateStr, endDateStr } = this.state;
let task_id = '';
this.setState({
isShowIODialog: true
}, () => {
orgAdminAPI.orgAdminExportLogsExcel(orgID, startDateStr, endDateStr, logType).then(res => {
task_id = res.data.task_id;
this.setState({
taskId: task_id
});
this.props.toggle();
return userAPI.queryIOStatus(task_id);
}).then(res => {
if (res.data.is_finished === true) {
this.setState({
isShowIODialog: false
});
this.props.toggle();
location.href = siteRoot + 'api/v2.1/org/admin/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
} else {
this.timer = setInterval(() => {
userAPI.queryIOStatus(task_id).then(res => {
if (res.data.is_finished === true) {
clearInterval(this.timer);
location.href = siteRoot + 'api/v2.1/org/admin/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
}
}).catch(err => {
clearInterval(this.timer);
toaster.danger(gettext('Failed to export. Please check whether the size of table attachments exceeds the limit.'));
});
}, 1000);
this.queryIOStatus(task_id, logType);
}
}).catch(error => {
this.props.toggle();
if (error.response && error.response.status === 500) {
const error_msg = error.response.data ? error.response.data['error_msg'] : null;
if (error_msg && error_msg !== 'Internal Server Error') {
@@ -77,7 +97,10 @@ class OrgLogsExportExcelDialog extends React.Component {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
}
this.props.toggle();
});
});
};
isValidDateStr = () => {
@@ -111,8 +134,17 @@ class OrgLogsExportExcelDialog extends React.Component {
});
};
onExportToggle = (e) => {
this.setState({
isShowIODialog: !this.state.isShowIODialog,
});
this.props.toggle();
};
render() {
return (
<React.Fragment>
{!this.state.isShowIODialog &&
<Modal isOpen={true} toggle={this.props.toggle} autoFocus={false}>
<ModalHeader toggle={this.props.toggle}>{gettext('Choose date')}</ModalHeader>
<ModalBody>
@@ -143,7 +175,13 @@ class OrgLogsExportExcelDialog extends React.Component {
<Button color="secondary" onClick={this.props.toggle}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.downloadExcel}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
</Modal>}
{this.state.isShowIODialog &&
<SeahubIODialog
toggle={this.onExportToggle}
/>
}
</React.Fragment>
);
}
}

View File

@@ -0,0 +1,39 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Modal, ModalBody, ModalHeader } from 'reactstrap';
import { gettext } from '../../utils/constants';
import Loading from '../loading';
import '../../css/seahub-io-dialog.css';
const propTypes = {
toggle: PropTypes.func.isRequired,
};
class SeahubIODialog extends React.Component {
toggle = () => {
this.props.toggle();
};
render() {
return (
<Modal className='seahub-io-dialog' isOpen={true} toggle={this.toggle}>
<ModalHeader toggle={this.toggle}>
{gettext('Exporting')}
</ModalHeader>
<ModalBody>
<>
<Loading/>
<div className="seahub-io-dialog-parsing-text">{gettext('Exporting...')}</div>
</>
</ModalBody>
</Modal>
);
}
}
SeahubIODialog.propTypes = propTypes;
export default SeahubIODialog;

View File

@@ -7,6 +7,7 @@ import { systemAdminAPI } from '../../../utils/system-admin-api';
import { userAPI } from '../../../utils/user-api';
import toaster from '../../../components/toast';
import { Utils } from '../../../utils/utils';
import SeahubIODialog from '../../dialog/seahub-io-dialog';
class LogsExportExcelDialog extends React.Component {
@@ -18,6 +19,7 @@ class LogsExportExcelDialog extends React.Component {
endDateStr: '',
errMsg: '',
taskId: '',
isShowIODialog: false,
};
}
@@ -41,34 +43,51 @@ class LogsExportExcelDialog extends React.Component {
}
};
queryIOStatus = (task_id, logType) => {
userAPI.queryIOStatus(task_id).then(res => {
if (res.data.is_finished === true) {
this.setState({
isShowIODialog: false
});
this.props.toggle();
location.href = siteRoot + 'sys/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
} else {
setTimeout(() => {
this.queryIOStatus(task_id, logType);
}, 1000);
}
}).catch(err => {
this.setState({
isShowIODialog: false
});
toaster.danger(gettext('Failed to export. Please check whether the size of table attachments exceeds the limit.'));
});
};
sysExportLogs = (logType) => {
let { startDateStr, endDateStr } = this.state;
let task_id = '';
this.setState({
isShowIODialog: true
}, () => {
systemAdminAPI.sysAdminExportLogsExcel(startDateStr, endDateStr, logType).then(res => {
task_id = res.data.task_id;
this.setState({
taskId: task_id
taskId: task_id,
});
this.props.toggle();
return userAPI.queryIOStatus(task_id);
}).then(res => {
if (res.data.is_finished === true) {
this.setState({
isShowIODialog: false,
});
this.props.toggle();
location.href = siteRoot + 'sys/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
} else {
this.timer = setInterval(() => {
userAPI.queryIOStatus(task_id).then(res => {
if (res.data.is_finished === true) {
clearInterval(this.timer);
location.href = siteRoot + 'sys/log/export-excel/?task_id=' + task_id + '&log_type=' + logType;
}
}).catch(err => {
clearInterval(this.timer);
toaster.danger(gettext('Failed to export. Please check whether the size of table attachments exceeds the limit.'));
});
}, 1000);
this.queryIOStatus(task_id, logType);
}
}).catch(error => {
this.props.toggle();
if (error.response && error.response.status === 500) {
const error_msg = error.response.data ? error.response.data['error_msg'] : null;
if (error_msg && error_msg !== 'Internal Server Error') {
@@ -80,6 +99,11 @@ class LogsExportExcelDialog extends React.Component {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
}
this.setState({
isShowIODialog: false
});
this.props.toggle();
});
});
};
@@ -114,8 +138,17 @@ class LogsExportExcelDialog extends React.Component {
});
};
onExportToggle = (e) => {
this.setState({
isShowIODialog: !this.state.isShowIODialog,
});
this.props.toggle();
};
render() {
return (
<React.Fragment>
{!this.state.isShowIODialog &&
<Modal isOpen={true} toggle={this.props.toggle} autoFocus={false}>
<ModalHeader toggle={this.props.toggle}>{gettext('Choose date')}</ModalHeader>
<ModalBody>
@@ -146,7 +179,15 @@ class LogsExportExcelDialog extends React.Component {
<Button color="secondary" onClick={this.props.toggle}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.downloadExcel}>{gettext('Submit')}</Button>
</ModalFooter>
</Modal>
}
{this.state.isShowIODialog &&
<SeahubIODialog
toggle={this.onExportToggle}
/>
}
</React.Fragment>
);
}
}

View File

@@ -0,0 +1,12 @@
.seahub-io-dialog .modal-body {
height: 142px;
display: flex;
flex-direction: column;
justify-content: center;
}
.seahub-io-dialog .modal-body .seahub-io-dialog-parsing-text {
text-align: center;
margin-top: 14px;
}

View File

@@ -1,5 +1,6 @@
import os
from shutil import rmtree
from datetime import datetime
from django.http import FileResponse
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
@@ -16,6 +17,8 @@ from seahub.api2.throttling import UserRateThrottle
from seahub.api2.utils import api_error
from seahub.auth.decorators import login_required
from seahub.base.decorators import sys_staff_required
from seahub.settings import ADMIN_LOGS_EXPORT_MAX_DAYS
class SysLogsExport(APIView):
@@ -32,6 +35,15 @@ class SysLogsExport(APIView):
error_msg = 'Failed to export excel, invalid start or end date.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
start_date = datetime.strptime(start, '%Y-%m-%d')
end_date = datetime.strptime(end, '%Y-%m-%d')
if start_date > end_date:
error_msg = 'invalid start or end date'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
if (end_date - start_date).days > ADMIN_LOGS_EXPORT_MAX_DAYS:
error_msg = 'Failed to export excel, only can export logs within %s days' % ADMIN_LOGS_EXPORT_MAX_DAYS
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
task_id = export_logs_to_excel(start, end, log_type)
res_data = {'task_id': task_id}
return Response(res_data)

View File

@@ -1,6 +1,7 @@
import os
import logging
from shutil import rmtree
from datetime import datetime
from django.http import FileResponse
from rest_framework.authentication import SessionAuthentication
from rest_framework.response import Response
@@ -17,6 +18,7 @@ from seahub.api2.utils import api_error
from seahub.auth.decorators import login_required
from seahub.organizations.api.permissions import IsOrgAdmin
from seahub.organizations.decorators import org_staff_required
from seahub.settings import ADMIN_LOGS_EXPORT_MAX_DAYS
@@ -35,6 +37,15 @@ class OrgLogsExport(APIView):
if not check_time_period_valid(start, end):
error_msg = 'Failed to export excel, invalid start or end date.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
start_date = datetime.strptime(start, '%Y-%m-%d')
end_date = datetime.strptime(end, '%Y-%m-%d')
if start_date > end_date:
error_msg = 'invalid start or end date'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
if (end_date - start_date).days > ADMIN_LOGS_EXPORT_MAX_DAYS:
error_msg = 'Failed to export excel, only can export logs within %s days' % ADMIN_LOGS_EXPORT_MAX_DAYS
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
task_id = export_logs_to_excel(start, end, log_type, org_id)
res_data = {'task_id': task_id}
return Response(res_data)

View File

@@ -494,6 +494,9 @@ ENABLE_SEAFILE_DOCS = False
ENABLE_CONVERT_TO_TEAM_ACCOUNT = False
ADMIN_LOGS_EXPORT_MAX_DAYS = 180
# File preview
FILE_PREVIEW_MAX_SIZE = 30 * 1024 * 1024
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']