1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-19 03:42:52 +00:00
seahub/frontend/src/components/dialog/generate-upload-link.js

335 lines
11 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
2019-01-04 10:11:13 +00:00
import copy from 'copy-to-clipboard';
2019-07-02 03:06:39 +00:00
import moment from 'moment';
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, InputGroupText, Alert } from 'reactstrap';
import { gettext, shareLinkPasswordMinLength, canSendShareLinkEmail } from '../../utils/constants';
2019-01-04 10:11:13 +00:00
import { seafileAPI } from '../../utils/seafile-api';
2019-05-06 08:08:34 +00:00
import { Utils } from '../../utils/utils';
import UploadLink from '../../models/upload-link';
2019-01-04 10:11:13 +00:00
import toaster from '../toast';
import SendLink from '../send-link';
import DateTimePicker from '../date-and-time-picker';
const propTypes = {
itemPath: PropTypes.string.isRequired,
repoID: PropTypes.string.isRequired,
closeShareDialog: PropTypes.func.isRequired,
};
const inputWidth = Utils.isDesktop() ? 250 : 210;
class GenerateUploadLink extends React.Component {
constructor(props) {
super(props);
this.state = {
showPasswordInput: false,
passwordVisible: false,
password: '',
passwdnew: '',
2019-01-04 10:11:13 +00:00
sharedUploadInfo: null,
2019-07-02 03:06:39 +00:00
isSendLinkShown: false,
isExpireChecked: false,
setExp: 'by-days',
expireDays: '',
expDate: null
};
}
componentDidMount() {
this.getUploadLink();
}
getUploadLink = () => {
let path = this.props.itemPath;
let repoID = this.props.repoID;
2020-01-11 05:29:04 +00:00
seafileAPI.getUploadLink(repoID, path).then((res) => {
if (res.data.length !== 0) {
let sharedUploadInfo = new UploadLink(res.data[0]);
2019-01-04 10:11:13 +00:00
this.setState({sharedUploadInfo: sharedUploadInfo});
}
2019-05-12 07:05:53 +00:00
}).catch((err) => {
let errMsg = Utils.getErrorMsg(err, true);
if (!err.response || err.response.status !== 403) {
toaster.danger(errMsg);
2019-05-12 07:05:53 +00:00
}
this.props.closeShareDialog();
});
}
addPassword = () => {
this.setState({
showPasswordInput: !this.state.showPasswordInput,
password: '',
passwdnew: '',
errorInfo: ''
});
}
togglePasswordVisible = () => {
this.setState({
passwordVisible: !this.state.passwordVisible
});
}
generatePassword = () => {
2019-05-06 08:08:34 +00:00
let val = Utils.generatePassword(shareLinkPasswordMinLength);
this.setState({
password: val,
passwordnew: val
});
}
inputPassword = (e) => {
this.setState({
password: e.target.value
});
}
inputPasswordNew = (e) => {
this.setState({
passwordnew: e.target.value
});
}
generateUploadLink = () => {
2019-07-02 03:06:39 +00:00
let isValid = this.validateParamsInput();
if (isValid) {
this.setState({errorInfo: ''});
let { itemPath, repoID } = this.props;
let { password, isExpireChecked, setExp, expireDays, expDate } = this.state;
let expirationTime = '';
if (isExpireChecked) {
if (setExp == 'by-days') {
expirationTime = moment().add(parseInt(expireDays), 'days').format();
} else {
expirationTime = expDate.format();
}
}
seafileAPI.createUploadLink(repoID, itemPath, password, expirationTime).then((res) => {
let sharedUploadInfo = new UploadLink(res.data);
2019-01-04 10:11:13 +00:00
this.setState({sharedUploadInfo: sharedUploadInfo});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
}
}
2019-07-02 03:06:39 +00:00
validateParamsInput = () => {
let { showPasswordInput, password, passwordnew, isExpireChecked, setExp, expireDays, expDate } = this.state;
2019-07-02 03:06:39 +00:00
// check password params
if (showPasswordInput) {
if (password.length === 0) {
2019-07-04 10:14:13 +00:00
this.setState({errorInfo: gettext('Please enter password')});
2019-07-02 03:06:39 +00:00
return false;
}
if (password.length < shareLinkPasswordMinLength) {
2019-07-04 10:14:13 +00:00
this.setState({errorInfo: gettext('Password is too short')});
2019-07-02 03:06:39 +00:00
return false;
}
if (password !== passwordnew) {
2019-07-04 10:14:13 +00:00
this.setState({errorInfo: gettext('Passwords don\'t match')});
2019-07-02 03:06:39 +00:00
return false;
}
}
if (isExpireChecked) {
if (setExp == 'by-date') {
if (!expDate) {
this.setState({errorInfo: 'Please select an expiration time'});
return false;
}
return true;
}
let reg = /^\d+$/;
2019-07-02 03:06:39 +00:00
if (!expireDays) {
2019-07-04 10:14:13 +00:00
this.setState({errorInfo: gettext('Please enter days')});
2019-07-02 03:06:39 +00:00
return false;
}
if (!reg.test(expireDays)) {
2019-07-04 10:14:13 +00:00
this.setState({errorInfo: gettext('Please enter a non-negative integer')});
2019-07-02 03:06:39 +00:00
return false;
}
this.setState({expireDays: parseInt(expireDays)});
}
return true;
}
onExpireChecked = (e) => {
this.setState({isExpireChecked: e.target.checked});
}
setExp = (e) => {
this.setState({
setExp: e.target.value
});
}
disabledDate = (current) => {
if (!current) {
// allow empty select
return false;
}
return current.isBefore(moment(), 'day');
}
onExpDateChanged = (value) => {
this.setState({
expDate: value
});
}
2019-07-02 03:06:39 +00:00
onExpireDaysChanged = (e) => {
let day = e.target.value.trim();
this.setState({expireDays: day});
}
2019-01-04 10:11:13 +00:00
onCopyUploadLink = () => {
let uploadLink = this.state.sharedUploadInfo.link;
copy(uploadLink);
toaster.success(gettext('Upload link is copied to the clipboard.'));
this.props.closeShareDialog();
2019-01-04 10:11:13 +00:00
}
deleteUploadLink = () => {
let sharedUploadInfo = this.state.sharedUploadInfo;
2019-01-04 10:11:13 +00:00
seafileAPI.deleteUploadLink(sharedUploadInfo.token).then(() => {
this.setState({
showPasswordInput: false,
password: '',
passwordnew: '',
2019-01-04 10:11:13 +00:00
sharedUploadInfo: null,
});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
}
toggleSendLink = () => {
this.setState({
isSendLinkShown: !this.state.isSendLinkShown
});
}
render() {
2019-05-06 08:08:34 +00:00
const { isSendLinkShown } = this.state;
2019-05-06 08:08:34 +00:00
let passwordLengthTip = gettext('(at least {passwordLength} characters)');
passwordLengthTip = passwordLengthTip.replace('{passwordLength}', shareLinkPasswordMinLength);
2019-01-04 10:11:13 +00:00
if (this.state.sharedUploadInfo) {
let sharedUploadInfo = this.state.sharedUploadInfo;
return (
2019-01-04 10:11:13 +00:00
<div>
<Form className="mb-4">
<FormGroup>
<dt className="text-secondary font-weight-normal">{gettext('Upload Link:')}</dt>
<dd className="d-flex">
<span>{sharedUploadInfo.link}</span>
<span className="far fa-copy action-icon" onClick={this.onCopyUploadLink}></span>
</dd>
</FormGroup>
2019-07-02 03:06:39 +00:00
{sharedUploadInfo.expire_date && (
<FormGroup className="mb-0">
<dt className="text-secondary font-weight-normal">{gettext('Expiration Date:')}</dt>
<dd>{moment(sharedUploadInfo.expire_date).format('YYYY-MM-DD HH:mm:ss')}</dd>
2019-07-02 03:06:39 +00:00
</FormGroup>
)}
2019-01-04 10:11:13 +00:00
</Form>
{canSendShareLinkEmail && !isSendLinkShown && <Button onClick={this.toggleSendLink} className="mr-2">{gettext('Send')}</Button>}
{!isSendLinkShown && <Button onClick={this.deleteUploadLink}>{gettext('Delete')}</Button>}
{isSendLinkShown &&
<SendLink
linkType='uploadLink'
token={sharedUploadInfo.token}
toggleSendLink={this.toggleSendLink}
closeShareDialog={this.props.closeShareDialog}
/>
}
2019-01-04 10:11:13 +00:00
</div>
);
}
return (
<Form className="generate-upload-link">
<FormGroup check>
<Label check>
<Input type="checkbox" onChange={this.addPassword} />
<span>{gettext('Add password protection')}</span>
</Label>
{this.state.showPasswordInput &&
<div className="ml-4">
<FormGroup>
<Label for="passwd">{gettext('Password')}</Label>
<span className="tip">{passwordLengthTip}</span>
<InputGroup style={{width: inputWidth}}>
<Input id="passwd" type={this.state.passwordVisible ? 'text':'password'} value={this.state.password || ''} onChange={this.inputPassword} />
<InputGroupAddon addonType="append">
<Button onClick={this.togglePasswordVisible}><i className={`link-operation-icon fas ${this.state.passwordVisible ? 'fa-eye': 'fa-eye-slash'}`}></i></Button>
<Button onClick={this.generatePassword}><i className="link-operation-icon fas fa-magic"></i></Button>
</InputGroupAddon>
</InputGroup>
</FormGroup>
<FormGroup>
<Label for="passwd-again">{gettext('Password again')}</Label>
<Input id="passwd-again" style={{width: inputWidth}} type={this.state.passwordVisible ? 'text' : 'password'} value={this.state.passwordnew || ''} onChange={this.inputPasswordNew} />
</FormGroup>
</div>
}
</FormGroup>
2019-07-02 03:06:39 +00:00
<FormGroup check>
<Label check>
<Input type="checkbox" onChange={this.onExpireChecked} />
<span>{gettext('Add auto expiration')}</span>
2019-07-02 03:06:39 +00:00
</Label>
{this.state.isExpireChecked &&
<div className="ml-4">
<FormGroup check>
<Label check>
<Input type="radio" name="set-exp" value="by-days" checked={this.state.setExp == 'by-days'} onChange={this.setExp} className="mr-1" />
<span>{gettext('Expiration days')}</span>
</Label>
{this.state.setExp == 'by-days' && (
<InputGroup style={{width: inputWidth}}>
<Input type="text" value={this.state.expireDays} onChange={this.onExpireDaysChanged} />
<InputGroupAddon addonType="append">
<InputGroupText>{gettext('days')}</InputGroupText>
</InputGroupAddon>
</InputGroup>
)}
</FormGroup>
<FormGroup check>
<Label check>
<Input type="radio" name="set-exp" value="by-date" checked={this.state.setExp == 'by-date'} onChange={this.setExp} className="mr-1" />
<span>{gettext('Expiration time')}</span>
</Label>
{this.state.setExp == 'by-date' && (
<DateTimePicker
inputWidth={inputWidth}
disabledDate={this.disabledDate}
value={this.state.expDate}
onChange={this.onExpDateChanged}
/>
)}
</FormGroup>
</div>
}
2019-07-02 03:06:39 +00:00
</FormGroup>
{this.state.errorInfo && <Alert color="danger" className="mt-2">{this.state.errorInfo}</Alert>}
<Button className="generate-link-btn" onClick={this.generateUploadLink}>{gettext('Generate')}</Button>
</Form>
);
}
}
GenerateUploadLink.propTypes = propTypes;
export default GenerateUploadLink;