2018-12-06 03:28:16 +00:00
|
|
|
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';
|
2019-05-15 06:56:46 +00:00
|
|
|
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, Alert } from 'reactstrap';
|
2019-06-27 06:08:10 +00:00
|
|
|
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';
|
2019-01-04 10:11:13 +00:00
|
|
|
import SharedUploadInfo from '../../models/shared-upload-info';
|
|
|
|
import toaster from '../toast';
|
2019-06-27 06:08:10 +00:00
|
|
|
import SendLink from '../send-link';
|
2019-05-12 07:05:53 +00:00
|
|
|
import SessionExpiredTip from '../session-expired-tip';
|
2018-12-06 03:28:16 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
itemPath: PropTypes.string.isRequired,
|
2019-01-05 12:40:41 +00:00
|
|
|
repoID: PropTypes.string.isRequired,
|
|
|
|
closeShareDialog: PropTypes.func.isRequired,
|
2018-12-06 03:28:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
expireDays: 0,
|
2018-12-06 03:28:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getUploadLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
getUploadLink = () => {
|
|
|
|
let path = this.props.itemPath;
|
|
|
|
let repoID = this.props.repoID;
|
|
|
|
seafileAPI.getUploadLinks(repoID, path).then((res) => {
|
|
|
|
if (res.data.length !== 0) {
|
2019-01-04 10:11:13 +00:00
|
|
|
let sharedUploadInfo = new SharedUploadInfo(res.data[0]);
|
|
|
|
this.setState({sharedUploadInfo: sharedUploadInfo});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
2019-05-12 07:05:53 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
if (err.response.status === 403) {
|
|
|
|
toaster.danger(
|
|
|
|
<SessionExpiredTip />,
|
|
|
|
{id: 'session_expired', duration: 3600}
|
2019-05-13 10:36:58 +00:00
|
|
|
);
|
2019-05-12 07:05:53 +00:00
|
|
|
this.props.closeShareDialog();
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
|
|
|
password: val,
|
|
|
|
passwordnew: val
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inputPassword = (e) => {
|
|
|
|
this.setState({
|
|
|
|
password: e.target.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inputPasswordNew = (e) => {
|
|
|
|
this.setState({
|
|
|
|
passwordnew: e.target.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
generateUploadLink = () => {
|
|
|
|
let path = this.props.itemPath;
|
|
|
|
let repoID = this.props.repoID;
|
2019-07-02 03:06:39 +00:00
|
|
|
let { password, expireDays } = this.state;
|
2018-12-06 03:28:16 +00:00
|
|
|
|
2019-07-02 03:06:39 +00:00
|
|
|
let isValid = this.validateParamsInput();
|
|
|
|
if (isValid) {
|
|
|
|
seafileAPI.createUploadLink(repoID, path, password, expireDays).then((res) => {
|
2019-01-04 10:11:13 +00:00
|
|
|
let sharedUploadInfo = new SharedUploadInfo(res.data);
|
|
|
|
this.setState({sharedUploadInfo: sharedUploadInfo});
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-02 03:06:39 +00:00
|
|
|
validateParamsInput = () => {
|
|
|
|
let { showPasswordInput , password, passwordnew, isExpireChecked, expireDays } = this.state;
|
|
|
|
|
|
|
|
// check password params
|
|
|
|
if (showPasswordInput) {
|
|
|
|
if (password.length === 0) {
|
|
|
|
this.setState({errorInfo: 'Please enter password'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password.length < shareLinkPasswordMinLength) {
|
|
|
|
this.setState({errorInfo: 'Password is too short'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password !== passwordnew) {
|
|
|
|
this.setState({errorInfo: 'Passwords don\'t match'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check expire day params
|
|
|
|
let reg = /^\d+$/;
|
|
|
|
if (isExpireChecked) {
|
|
|
|
if (!expireDays) {
|
|
|
|
this.setState({errorInfo: 'Please enter days'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!reg.test(expireDays)) {
|
|
|
|
this.setState({errorInfo: 'Please enter a non-negative integer'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.setState({expireDays: parseInt(expireDays)});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
onExpireChecked = (e) => {
|
|
|
|
this.setState({isExpireChecked: e.target.checked});
|
|
|
|
}
|
|
|
|
|
|
|
|
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.'));
|
2019-01-05 12:40:41 +00:00
|
|
|
this.props.closeShareDialog();
|
2019-01-04 10:11:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
deleteUploadLink = () => {
|
2019-01-31 09:37:02 +00:00
|
|
|
let sharedUploadInfo = this.state.sharedUploadInfo;
|
2019-01-04 10:11:13 +00:00
|
|
|
seafileAPI.deleteUploadLink(sharedUploadInfo.token).then(() => {
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
|
|
|
showPasswordInput: false,
|
|
|
|
password: '',
|
|
|
|
passwordnew: '',
|
2019-01-04 10:11:13 +00:00
|
|
|
sharedUploadInfo: null,
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:08:10 +00:00
|
|
|
toggleSendLink = () => {
|
|
|
|
this.setState({
|
|
|
|
isSendLinkShown: !this.state.isSendLinkShown
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
render() {
|
2019-05-06 08:08:34 +00:00
|
|
|
|
2019-06-27 06:08:10 +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;
|
2018-12-06 03:28:16 +00:00
|
|
|
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>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2019-01-04 10:11:13 +00:00
|
|
|
</Form>
|
2019-06-27 06:08:10 +00:00
|
|
|
{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>
|
2018-12-06 03:28:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Form className="generate-upload-link">
|
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input type="checkbox" onChange={this.addPassword}/>{' '}{gettext('Add password protection')}
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
{this.state.showPasswordInput &&
|
|
|
|
<FormGroup className="link-operation-content">
|
2018-12-20 07:55:07 +00:00
|
|
|
{/* todo translate */}
|
2019-05-06 08:08:34 +00:00
|
|
|
<Label className="font-weight-bold">{gettext('Password')}</Label>{' '}<span className="tip">{passwordLengthTip}</span>
|
2018-12-06 03:28:16 +00:00
|
|
|
<InputGroup className="passwd">
|
|
|
|
<Input 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>
|
2019-01-28 08:35:45 +00:00
|
|
|
<Label className="font-weight-bold">{gettext('Password again')}</Label>
|
2018-12-06 03:28:16 +00:00
|
|
|
<Input className="passwd" type={this.state.passwordVisible ? 'text' : 'password'} value={this.state.passwordnew || ''} onChange={this.inputPasswordNew} />
|
|
|
|
</FormGroup>
|
|
|
|
}
|
2019-07-02 03:06:39 +00:00
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input className="expire-checkbox" type="checkbox" onChange={this.onExpireChecked}/>{' '}{gettext('Add auto expiration')}
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
{this.state.isExpireChecked &&
|
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input className="expire-input expire-input-border" type="text" value={this.state.expireDays} onChange={this.onExpireDaysChanged} readOnly={!this.state.isExpireChecked}/><span className="expir-span">{gettext('days')}</span>
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
}
|
2019-02-12 03:16:49 +00:00
|
|
|
{this.state.errorInfo && <Alert color="danger" className="mt-2">{this.state.errorInfo}</Alert>}
|
2018-12-06 03:28:16 +00:00
|
|
|
<Button className="generate-link-btn" onClick={this.generateUploadLink}>{gettext('Generate')}</Button>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GenerateUploadLink.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default GenerateUploadLink;
|