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 moment from 'moment';
|
|
|
|
import copy from 'copy-to-clipboard';
|
|
|
|
import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon } from 'reactstrap';
|
2018-12-06 03:28:16 +00:00
|
|
|
import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax } from '../../utils/constants';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2019-01-04 10:11:13 +00:00
|
|
|
import SharedLinkInfo from '../../models/shared-link-info';
|
|
|
|
import toaster from '../toast';
|
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 GenerateShareLink extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isValidate: false,
|
2018-12-14 07:09:07 +00:00
|
|
|
isShowPasswordInput: false,
|
|
|
|
isPasswordVisible: false,
|
|
|
|
isExpireChecked: false,
|
2018-12-06 03:28:16 +00:00
|
|
|
password: '',
|
|
|
|
passwdnew: '',
|
|
|
|
expireDays: '',
|
2019-01-04 10:11:13 +00:00
|
|
|
errorInfo: '',
|
|
|
|
sharedLinkInfo: null,
|
|
|
|
isNoticeMessageShow: false,
|
2018-12-06 03:28:16 +00:00
|
|
|
};
|
|
|
|
this.permissions = {
|
|
|
|
'can_edit': false,
|
|
|
|
'can_download': true
|
|
|
|
};
|
2018-12-14 07:09:07 +00:00
|
|
|
this.isExpireDaysNoLimit = (parseInt(shareLinkExpireDaysMin) === 0 && parseInt(shareLinkExpireDaysMax) === 0);
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.getShareLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
getShareLink = () => {
|
|
|
|
let path = this.props.itemPath;
|
|
|
|
let repoID = this.props.repoID;
|
|
|
|
seafileAPI.getShareLink(repoID, path).then((res) => {
|
|
|
|
if (res.data.length !== 0) {
|
2019-01-04 10:11:13 +00:00
|
|
|
let sharedLinkInfo = new SharedLinkInfo(res.data[0]);
|
|
|
|
this.setState({sharedLinkInfo: sharedLinkInfo});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-14 07:09:07 +00:00
|
|
|
onPasswordInputChecked = () => {
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
2018-12-14 07:09:07 +00:00
|
|
|
isShowPasswordInput: !this.state.isShowPasswordInput,
|
2018-12-06 03:28:16 +00:00
|
|
|
password: '',
|
|
|
|
passwdnew: '',
|
|
|
|
errorInfo: ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
togglePasswordVisible = () => {
|
|
|
|
this.setState({
|
2018-12-14 07:09:07 +00:00
|
|
|
isPasswordVisible: !this.state.isPasswordVisible
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
generatePassword = () => {
|
2018-12-06 06:39:21 +00:00
|
|
|
let val = Math.random().toString(36).substr(5);
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
|
|
|
password: val,
|
2018-12-14 07:09:07 +00:00
|
|
|
passwdnew: val
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inputPassword = (e) => {
|
2018-12-14 07:21:48 +00:00
|
|
|
let passwd = e.target.value.trim();
|
|
|
|
this.setState({password: passwd});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputPasswordNew = (e) => {
|
2018-12-14 07:21:48 +00:00
|
|
|
let passwd = e.target.value.trim();
|
|
|
|
this.setState({passwdnew: passwd});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setPermission = (permission) => {
|
|
|
|
if (permission == 'previewAndDownload') {
|
|
|
|
this.permissions = {
|
|
|
|
'can_edit': false,
|
|
|
|
'can_download': true
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.permissions = {
|
|
|
|
'can_edit': false,
|
|
|
|
'can_download': false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generateShareLink = () => {
|
2018-12-14 07:09:07 +00:00
|
|
|
let isValid = this.validateParamsInput();
|
|
|
|
if (isValid) {
|
2018-12-14 07:49:24 +00:00
|
|
|
this.setState({errorInfo: ''});
|
2018-12-14 07:09:07 +00:00
|
|
|
let { itemPath, repoID } = this.props;
|
2018-12-06 03:28:16 +00:00
|
|
|
let { password, expireDays } = this.state;
|
|
|
|
let permissions = this.permissions;
|
|
|
|
permissions = JSON.stringify(permissions);
|
2018-12-14 07:09:07 +00:00
|
|
|
seafileAPI.createShareLink(repoID, itemPath, password, expireDays, permissions).then((res) => {
|
2019-01-04 10:11:13 +00:00
|
|
|
let sharedLinkInfo = new SharedLinkInfo(res.data);
|
|
|
|
this.setState({sharedLinkInfo: sharedLinkInfo});
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:11:13 +00:00
|
|
|
onCopySharedLink = () => {
|
|
|
|
let sharedLink = this.state.sharedLinkInfo.link;
|
|
|
|
copy(sharedLink);
|
|
|
|
toaster.success(gettext('Share 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
|
|
|
}
|
2019-01-05 12:40:41 +00:00
|
|
|
|
2019-01-04 10:11:13 +00:00
|
|
|
onCopyDownloadLink = () => {
|
|
|
|
let downloadLink = this.state.sharedLinkInfo.link + '?dl';
|
|
|
|
copy(downloadLink);
|
|
|
|
toaster.success(gettext('Direct download 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
|
|
|
deleteShareLink = () => {
|
2019-01-04 10:11:13 +00:00
|
|
|
let sharedLinkInfo = this.state.sharedLinkInfo;
|
|
|
|
seafileAPI.deleteShareLink(sharedLinkInfo.token).then(() => {
|
2018-12-06 03:28:16 +00:00
|
|
|
this.setState({
|
|
|
|
password: '',
|
|
|
|
passwordnew: '',
|
2018-12-14 07:09:07 +00:00
|
|
|
isShowPasswordInput: false,
|
|
|
|
expireDays: '',
|
|
|
|
isExpireChecked: false,
|
2018-12-14 07:49:24 +00:00
|
|
|
errorInfo: '',
|
2019-01-04 10:11:13 +00:00
|
|
|
sharedLinkInfo: null,
|
|
|
|
isNoticeMessageShow: false,
|
2018-12-06 03:28:16 +00:00
|
|
|
});
|
|
|
|
this.permissions = {
|
|
|
|
'can_edit': false,
|
|
|
|
'can_download': true
|
|
|
|
};
|
|
|
|
});
|
2018-12-14 07:09:07 +00:00
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
|
2018-12-14 07:09:07 +00:00
|
|
|
onExpireChecked = (e) => {
|
|
|
|
this.setState({isExpireChecked: e.target.checked});
|
|
|
|
}
|
|
|
|
|
|
|
|
onExpireDaysChanged = (e) => {
|
2018-12-14 07:21:48 +00:00
|
|
|
let day = e.target.value.trim();
|
|
|
|
this.setState({expireDays: day});
|
2018-12-14 07:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validateParamsInput = () => {
|
|
|
|
let { isShowPasswordInput , password, passwdnew, isExpireChecked, expireDays } = this.state;
|
|
|
|
// validate password
|
|
|
|
if (isShowPasswordInput) {
|
|
|
|
if (password.length === 0) {
|
|
|
|
this.setState({errorInfo: 'Please enter password'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password.length < 8) {
|
|
|
|
this.setState({errorInfo: 'Password is too short'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (password !== passwdnew) {
|
|
|
|
this.setState({errorInfo: 'Passwords don\'t match'});
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 07:09:07 +00:00
|
|
|
// validate days
|
|
|
|
// no limit
|
|
|
|
let reg = /^\d+$/;
|
|
|
|
if (this.isExpireDaysNoLimit) {
|
|
|
|
if (isExpireChecked) {
|
|
|
|
if (!expireDays) {
|
2018-12-14 07:49:24 +00:00
|
|
|
this.setState({errorInfo: 'Please enter days'});
|
2018-12-14 07:09:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let flag = reg.test(expireDays);
|
|
|
|
if (!flag) {
|
|
|
|
this.setState({errorInfo: 'Please enter a non-negative integer'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.setState({expireDays: parseInt(expireDays)});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!expireDays) {
|
2018-12-14 07:49:24 +00:00
|
|
|
this.setState({errorInfo: 'Please enter days'});
|
2018-12-14 07:09:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let flag = reg.test(expireDays);
|
|
|
|
if (!flag) {
|
|
|
|
this.setState({errorInfo: 'Please enter a non-negative integer'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
expireDays = parseInt(expireDays);
|
|
|
|
let minDays = parseInt(shareLinkExpireDaysMin);
|
|
|
|
let maxDays = parseInt(shareLinkExpireDaysMax);
|
|
|
|
|
|
|
|
if (minDays !== 0 && maxDays !== maxDays) {
|
|
|
|
if (expireDays < minDays) {
|
|
|
|
this.setState({errorInfo: 'Please enter valid days'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minDays === 0 && maxDays !== 0 ) {
|
|
|
|
if (expireDays > maxDays) {
|
|
|
|
this.setState({errorInfo: 'Please enter valid days'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minDays !== 0 && maxDays !== 0) {
|
|
|
|
if (expireDays < minDays || expireDays < maxDays) {
|
|
|
|
this.setState({errorInfo: 'Please enter valid days'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setState({expireDays: expireDays});
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 07:09:07 +00:00
|
|
|
return true;
|
2018-12-06 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:11:13 +00:00
|
|
|
onNoticeMessageToggle = () => {
|
|
|
|
this.setState({isNoticeMessageShow: !this.state.isNoticeMessageShow});
|
|
|
|
}
|
|
|
|
|
2018-12-06 03:28:16 +00:00
|
|
|
render() {
|
2019-01-04 10:11:13 +00:00
|
|
|
if (this.state.sharedLinkInfo) {
|
|
|
|
let sharedLinkInfo = this.state.sharedLinkInfo;
|
2018-12-06 03:28:16 +00:00
|
|
|
return (
|
2019-01-04 10:11:13 +00:00
|
|
|
<div>
|
|
|
|
<Form className="mb-4">
|
|
|
|
<FormGroup className="mb-0">
|
|
|
|
<dt className="text-secondary font-weight-normal">{gettext('Link:')}</dt>
|
|
|
|
<dd className="d-flex">
|
|
|
|
<span>{sharedLinkInfo.link}</span>{' '}
|
|
|
|
{sharedLinkInfo.is_expired ?
|
|
|
|
<span className="err-message">({gettext('Expired')})</span> :
|
|
|
|
<span className="far fa-copy action-icon" onClick={this.onCopySharedLink}></span>
|
|
|
|
}
|
|
|
|
</dd>
|
|
|
|
</FormGroup>
|
|
|
|
{!sharedLinkInfo.is_dir && ( //just for file
|
|
|
|
<FormGroup className="mb-0">
|
|
|
|
<dt className="text-secondary font-weight-normal">{gettext('Direct Download Link:')}</dt>
|
|
|
|
<dd className="d-flex">
|
|
|
|
<span>{sharedLinkInfo.link}?dl</span>{' '}
|
|
|
|
{sharedLinkInfo.is_expired ?
|
|
|
|
<span className="err-message">({gettext('Expired')})</span> :
|
|
|
|
<span className="far fa-copy action-icon" onClick={this.onCopyDownloadLink}></span>
|
|
|
|
}
|
|
|
|
</dd>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
{sharedLinkInfo.expire_date && (
|
|
|
|
<FormGroup className="mb-0">
|
|
|
|
<dt className="text-secondary font-weight-normal">{gettext('Expiration Date:')}</dt>
|
|
|
|
<dd>{moment(sharedLinkInfo.expire_date).format('YYYY-MM-DD hh:mm:ss')}</dd>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
{!this.state.isNoticeMessageShow ?
|
|
|
|
<Button onClick={this.onNoticeMessageToggle}>{gettext('Delete')}</Button> :
|
|
|
|
<div className="alert alert-warning">
|
|
|
|
<h4 className="alert-heading">{gettext('Are you sure you want to delete the share link?')}</h4>
|
|
|
|
<p className="mb-4">{gettext('If the share link is deleted, no one will be able to access it any more.')}</p>
|
|
|
|
<button className="btn btn-primary" onClick={this.deleteShareLink}>{gettext('Delete')}</button>{' '}
|
|
|
|
<button className="btn btn-secondary" onClick={this.onNoticeMessageToggle}>{gettext('Cancel')}</button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
2018-12-06 03:28:16 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Form className="generate-share-link">
|
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
2018-12-14 07:09:07 +00:00
|
|
|
<Input type="checkbox" onChange={this.onPasswordInputChecked}/>{' '}{gettext('Add password protection')}
|
2018-12-06 03:28:16 +00:00
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
2018-12-14 07:09:07 +00:00
|
|
|
{this.state.isShowPasswordInput &&
|
2018-12-06 03:28:16 +00:00
|
|
|
<FormGroup className="link-operation-content">
|
2018-12-20 07:55:07 +00:00
|
|
|
{/* todo translate */}
|
2019-01-28 08:35:45 +00:00
|
|
|
<Label className="font-weight-bold">{gettext('Password')}</Label>{' '}<span className="tip">{gettext('(at least 8 characters)')}</span>
|
2018-12-06 03:28:16 +00:00
|
|
|
<InputGroup className="passwd">
|
2018-12-14 07:09:07 +00:00
|
|
|
<Input type={this.state.isPasswordVisible ? 'text' : 'password'} value={this.state.password || ''} onChange={this.inputPassword}/>
|
2018-12-06 03:28:16 +00:00
|
|
|
<InputGroupAddon addonType="append">
|
2018-12-14 07:09:07 +00:00
|
|
|
<Button onClick={this.togglePasswordVisible}><i className={`link-operation-icon fas ${this.state.isPasswordVisible ? 'fa-eye': 'fa-eye-slash'}`}></i></Button>
|
2018-12-06 03:28:16 +00:00
|
|
|
<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-14 07:09:07 +00:00
|
|
|
<Input className="passwd" type={this.state.isPasswordVisible ? 'text' : 'password'} value={this.state.passwdnew || ''} onChange={this.inputPasswordNew} />
|
2018-12-06 03:28:16 +00:00
|
|
|
</FormGroup>
|
|
|
|
}
|
2018-12-14 07:09:07 +00:00
|
|
|
{this.isExpireDaysNoLimit && (
|
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input className="expire-checkbox" type="checkbox" onChange={this.onExpireChecked}/>{' '}{gettext('Add auto expiration')}
|
|
|
|
<Input className="expire-input" type="text" value={this.state.expireDays} onChange={this.onExpireDaysChanged} readOnly={!this.state.isExpireChecked}/><span>{gettext('days')}</span>
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
|
|
|
{!this.isExpireDaysNoLimit && (
|
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input className="expire-checkbox" type="checkbox" onChange={this.onExpireChecked} checked readOnly/>{' '}{gettext('Add auto expiration')}
|
|
|
|
<Input className="expire-input" type="text" value={this.state.expireDays} onChange={this.onExpireDaysChanged} /> <span>{gettext('days')}</span>
|
|
|
|
{(parseInt(shareLinkExpireDaysMin) !== 0 && parseInt(shareLinkExpireDaysMax) !== 0) && (
|
|
|
|
<span> ({shareLinkExpireDaysMin} - {shareLinkExpireDaysMax}{' '}{gettext('days')})</span>
|
|
|
|
)}
|
|
|
|
{(parseInt(shareLinkExpireDaysMin) !== 0 && parseInt(shareLinkExpireDaysMax) === 0) && (
|
|
|
|
<span> ({gettext('Greater than or equal to')} {shareLinkExpireDaysMin}{' '}{gettext('days')})</span>
|
|
|
|
)}
|
|
|
|
{(parseInt(shareLinkExpireDaysMin) === 0 && parseInt(shareLinkExpireDaysMax) !== 0) && (
|
|
|
|
<span> ({gettext('Less than or equal to')} {shareLinkExpireDaysMax}{' '}{gettext('days')})</span>
|
|
|
|
)}
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
)}
|
2018-12-06 03:28:16 +00:00
|
|
|
<FormGroup check>
|
|
|
|
<Label check>
|
|
|
|
<Input type="checkbox" checked readOnly/>{' '}{gettext('Set permission')}
|
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup check className="permission">
|
|
|
|
<Label check>
|
2018-12-22 03:17:03 +00:00
|
|
|
<Input type="radio" name="radio1" defaultChecked={true} onChange={() => this.setPermission('previewAndDownload')}/>{' '}{gettext('Preview and download')}
|
2018-12-06 03:28:16 +00:00
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup check className="permission">
|
|
|
|
<Label check>
|
2018-12-22 03:17:03 +00:00
|
|
|
<Input type="radio" name="radio1" onChange={() => this.setPermission('preview')} />{' '}{gettext('Preview only')}
|
2018-12-06 03:28:16 +00:00
|
|
|
</Label>
|
|
|
|
</FormGroup>
|
2018-12-14 07:49:24 +00:00
|
|
|
<Label className="err-message">{gettext(this.state.errorInfo)}</Label><br />
|
2018-12-06 03:28:16 +00:00
|
|
|
<Button onClick={this.generateShareLink}>{gettext('Generate')}</Button>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GenerateShareLink.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default GenerateShareLink;
|