From e4977cbf902653944ff7e569a01c957cb860b6e6 Mon Sep 17 00:00:00 2001 From: llj Date: Fri, 19 Jul 2024 14:53:40 +0800 Subject: [PATCH] [share dialog - share link] added 'share-link-api.js' back & used it --- .../link-authenticated-emails.js | 8 +- .../link-authenticated-users.js | 8 +- .../share-link-panel/link-creation.js | 3 +- .../share-link-panel/link-details.js | 7 +- frontend/src/utils/seafile-api.js | 3 +- frontend/src/utils/share-link-api.js | 130 ++++++++++++++++++ 6 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 frontend/src/utils/share-link-api.js diff --git a/frontend/src/components/share-link-panel/link-authenticated-emails.js b/frontend/src/components/share-link-panel/link-authenticated-emails.js index 62c46181702..4a092b0c1d0 100644 --- a/frontend/src/components/share-link-panel/link-authenticated-emails.js +++ b/frontend/src/components/share-link-panel/link-authenticated-emails.js @@ -5,7 +5,7 @@ import { Button } from 'reactstrap'; import { Utils } from '../../utils/utils'; import Loading from '../loading'; import toaster from '../toast'; -import { seafileAPI } from '../../utils/seafile-api'; +import { shareLinkAPI } from '../../utils/share-link-api'; class EmailItem extends React.Component { @@ -94,7 +94,7 @@ class LinkAuthenticatedEmails extends React.Component { getItems = () => { const { linkToken, path } = this.props; - seafileAPI.listShareLinkAuthEmails(linkToken, path).then(res => { + shareLinkAPI.listShareLinkAuthEmails(linkToken, path).then(res => { this.setState({authEmails: res.data.auth_list}); }).catch(error => { let errMessage = Utils.getErrorMsg(error); @@ -108,7 +108,7 @@ class LinkAuthenticatedEmails extends React.Component { this.setState({ isSubmitting: true }); - seafileAPI.addShareLinkAuthEmails(linkToken, inputEmails, path).then(res => { + shareLinkAPI.addShareLinkAuthEmails(linkToken, inputEmails, path).then(res => { const { success, failed } = res.data; let newEmails = []; if (success.length) { @@ -139,7 +139,7 @@ class LinkAuthenticatedEmails extends React.Component { deleteItem = (email) => { const { linkToken, path } = this.props; let emails = [email, ]; - seafileAPI.deleteShareLinkAuthEmails(linkToken, emails, path).then(res => { + shareLinkAPI.deleteShareLinkAuthEmails(linkToken, emails, path).then(res => { let authEmails = this.state.authEmails.filter(e => { return e !== email; }); diff --git a/frontend/src/components/share-link-panel/link-authenticated-users.js b/frontend/src/components/share-link-panel/link-authenticated-users.js index 66cfeb35e24..77e19973f1c 100644 --- a/frontend/src/components/share-link-panel/link-authenticated-users.js +++ b/frontend/src/components/share-link-panel/link-authenticated-users.js @@ -2,7 +2,7 @@ import React, { Fragment } from 'react'; import PropTypes from 'prop-types'; import { gettext } from '../../utils/constants'; import { Button } from 'reactstrap'; -import { seafileAPI } from '../../utils/seafile-api'; +import { shareLinkAPI } from '../../utils/share-link-api'; import { Utils } from '../../utils/utils'; import UserSelect from '../user-select'; import toaster from '../toast'; @@ -99,7 +99,7 @@ class LinkAuthenticatedUsers extends React.Component { listLinkAuthUsers = () => { const { linkToken, path } = this.props; - seafileAPI.listShareLinkAuthUsers(linkToken, path).then(res => { + shareLinkAPI.listShareLinkAuthUsers(linkToken, path).then(res => { this.setState({authUsers: res.data.auth_list}); }).catch(error => { let errMessage = Utils.getErrorMsg(error); @@ -114,7 +114,7 @@ class LinkAuthenticatedUsers extends React.Component { return false; } const users = selectedOption.map((item, index) => item.email); - seafileAPI.addShareLinkAuthUsers(linkToken, users, path).then(res => { + shareLinkAPI.addShareLinkAuthUsers(linkToken, users, path).then(res => { const { success, failed } = res.data; if (success.length) { let newNames = success.map(item => item.name); @@ -141,7 +141,7 @@ class LinkAuthenticatedUsers extends React.Component { deleteItem = (username) => { const { linkToken, path } = this.props; let users = [username, ]; - seafileAPI.deleteShareLinkAuthUsers(linkToken, users, path).then(res => { + shareLinkAPI.deleteShareLinkAuthUsers(linkToken, users, path).then(res => { let authUsers = this.state.authUsers.filter(user => { return user.username !== username; }); diff --git a/frontend/src/components/share-link-panel/link-creation.js b/frontend/src/components/share-link-panel/link-creation.js index eb9d05fe2f1..c04298220ca 100644 --- a/frontend/src/components/share-link-panel/link-creation.js +++ b/frontend/src/components/share-link-panel/link-creation.js @@ -4,6 +4,7 @@ import moment from 'moment'; import { Button, Form, FormGroup, Label, Input, InputGroup, InputGroupAddon, Alert } from 'reactstrap'; import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax, shareLinkExpireDaysDefault, shareLinkForceUsePassword, shareLinkPasswordMinLength, shareLinkPasswordStrengthLevel, isEmailConfigured } from '../../utils/constants'; import { seafileAPI } from '../../utils/seafile-api'; +import { shareLinkAPI } from '../../utils/share-link-api'; import { Utils } from '../../utils/utils'; import ShareLink from '../../models/share-link'; import toaster from '../toast'; @@ -133,7 +134,7 @@ class LinkCreation extends React.Component { if (currentScope === 'specific_emails' && inputEmails) { users = inputEmails; } - request = seafileAPI.createMultiShareLink(repoID, itemPath, password, expirationTime, permissions, currentScope, users); + request = shareLinkAPI.createMultiShareLink(repoID, itemPath, password, expirationTime, permissions, currentScope, users); } request.then((res) => { diff --git a/frontend/src/components/share-link-panel/link-details.js b/frontend/src/components/share-link-panel/link-details.js index f42dad44c5c..267026c7ce4 100644 --- a/frontend/src/components/share-link-panel/link-details.js +++ b/frontend/src/components/share-link-panel/link-details.js @@ -6,6 +6,7 @@ import { Button, Input, InputGroup, InputGroupAddon } from 'reactstrap'; import { gettext, shareLinkExpireDaysMin, shareLinkExpireDaysMax, shareLinkExpireDaysDefault, canSendShareLinkEmail } from '../../utils/constants'; import CommonOperationConfirmationDialog from '../../components/dialog/common-operation-confirmation-dialog'; import { seafileAPI } from '../../utils/seafile-api'; +import { shareLinkAPI } from '../../utils/share-link-api'; import { Utils } from '../../utils/utils'; import ShareLink from '../../models/share-link'; import toaster from '../toast'; @@ -40,9 +41,7 @@ class LinkDetails extends React.Component { expireDays: this.props.defaultExpireDays, expDate: null, isLinkDeleteDialogOpen: false, - isSendLinkShown: false, - - currentScope: this.props.sharedLinkInfo.user_scope, // all_users, specific_users, spcific_emails + isSendLinkShown: false }; } @@ -137,7 +136,7 @@ class LinkDetails extends React.Component { changeScope = (scope) => { const { sharedLinkInfo } = this.props; const { token } = sharedLinkInfo; - seafileAPI.updateShareLink(token, '', '', scope).then((res) => { + shareLinkAPI.updateShareLink(token, '', '', scope).then((res) => { this.props.updateLink(new ShareLink(res.data)); }).catch((error) => { let errMessage = Utils.getErrorMsg(error); diff --git a/frontend/src/utils/seafile-api.js b/frontend/src/utils/seafile-api.js index b23da9611ff..d024f29e018 100644 --- a/frontend/src/utils/seafile-api.js +++ b/frontend/src/utils/seafile-api.js @@ -1,5 +1,6 @@ import cookie from 'react-cookies'; -import { SeafileAPI } from 'seafile-js'; +//import { SeafileAPI } from 'seafile-js'; +import { SeafileAPI } from './a'; import { siteRoot } from './constants'; let seafileAPI = new SeafileAPI(); diff --git a/frontend/src/utils/share-link-api.js b/frontend/src/utils/share-link-api.js new file mode 100644 index 00000000000..3ea1f6098b6 --- /dev/null +++ b/frontend/src/utils/share-link-api.js @@ -0,0 +1,130 @@ +import cookie from 'react-cookies'; +import {siteRoot} from './constants'; +import axios from 'axios'; + +class ShareLinkAPI { + init({server, username, password, token}) { + this.server = server; + this.username = username; + this.password = password; + this.token = token; //none + if (this.token && this.server) { + this.req = axios.create({ + baseURL: this.server, + headers: {'Authorization': 'Token ' + this.token}, + }); + } + return this; + } + + initForSeahubUsage({siteRoot, xcsrfHeaders}) { + if (siteRoot && siteRoot.charAt(siteRoot.length - 1) === '/') { + let server = siteRoot.substring(0, siteRoot.length - 1); + this.server = server; + } else { + this.server = siteRoot; + } + this.req = axios.create({ + headers: { + 'X-CSRFToken': xcsrfHeaders, + } + }); + return this; + } + + _sendPostRequest(url, form) { + if (form.getHeaders) { + return this.req.post(url, form, { + headers: form.getHeaders() + }); + } else { + return this.req.post(url, form); + } + } + + listShareLinkAuthUsers(link_token, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path); + return this.req.get(url); + } + + addShareLinkAuthUsers(link_token, emails, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path); + const data = { + emails: emails, + }; + return this.req.post(url, data); + + } + + deleteShareLinkAuthUsers(link_token, emails, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/user-auth/?path=' + encodeURIComponent(path); + const params = { + emails: emails, + }; + return this.req.delete(url, {data: params}); + } + + listShareLinkAuthEmails(link_token, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path); + return this.req.get(url); + } + + addShareLinkAuthEmails(link_token, emails, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path); + const data = { + emails: emails, + }; + return this.req.post(url, data); + + } + + deleteShareLinkAuthEmails(link_token, emails, path) { + const url = this.server + '/api/v2.1/share-links/' + link_token + '/email-auth/?path=' + encodeURIComponent(path); + const params = { + emails: emails, + }; + return this.req.delete(url, {data: params}); + } + + updateShareLink(token, permissions, expirationTime='', userScope='') { + var url = this.server + '/api/v2.1/share-links/' + token + '/'; + let form = new FormData(); + if (permissions) { + form.append('permissions', permissions); + } + if (expirationTime) { + form.append('expiration_time', expirationTime); + } + if (userScope) { + form.append('user_scope', userScope); + } + return this.req.put(url, form); + } + + createMultiShareLink(repoID, path, password, expirationTime, permissions, scope, users) { + const url = this.server + '/api/v2.1/multi-share-links/'; + let form = { + 'path': path, + 'repo_id': repoID, + 'user_scope': scope, + }; + if (permissions) { + form['permissions'] = permissions; + } + if (password) { + form['password'] = password; + } + if (expirationTime) { + form['expiration_time'] = expirationTime; + } + if (users) { + form['emails'] = users; + } + return this._sendPostRequest(url, form); + } +} + +let shareLinkAPI = new ShareLinkAPI(); +let xcsrfHeaders = cookie.load('sfcsrftoken'); +shareLinkAPI.initForSeahubUsage({siteRoot, xcsrfHeaders}); +export {shareLinkAPI};