2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-08-15 00:17:53 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import React from "react";
|
2023-02-02 16:43:51 +08:00
|
|
|
import {Button, Popconfirm, Table, Upload} from "antd";
|
2021-08-15 00:17:53 +08:00
|
|
|
import {UploadOutlined} from "@ant-design/icons";
|
2022-07-10 15:45:55 +08:00
|
|
|
import copy from "copy-to-clipboard";
|
2021-08-15 00:17:53 +08:00
|
|
|
import * as Setting from "./Setting";
|
|
|
|
import * as ResourceBackend from "./backend/ResourceBackend";
|
|
|
|
import i18next from "i18next";
|
|
|
|
import {Link} from "react-router-dom";
|
2021-12-25 10:55:10 +08:00
|
|
|
import BaseListPage from "./BaseListPage";
|
2021-08-15 00:17:53 +08:00
|
|
|
|
2021-12-25 10:55:10 +08:00
|
|
|
class ResourceListPage extends BaseListPage {
|
2021-08-15 00:17:53 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2023-02-02 16:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.setState({
|
2021-08-15 00:17:53 +08:00
|
|
|
fileList: [],
|
|
|
|
uploading: false,
|
2023-02-02 16:43:51 +08:00
|
|
|
});
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteResource(i) {
|
2021-12-25 10:55:10 +08:00
|
|
|
ResourceBackend.deleteResource(this.state.data[i])
|
2021-08-15 00:17:53 +08:00
|
|
|
.then((res) => {
|
2022-12-02 00:06:28 +08:00
|
|
|
if (res.status === "ok") {
|
|
|
|
Setting.showMessage("success", i18next.t("general:Successfully deleted"));
|
|
|
|
this.setState({
|
|
|
|
data: Setting.deleteRow(this.state.data, i),
|
|
|
|
pagination: {total: this.state.pagination.total - 1},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`);
|
|
|
|
}
|
|
|
|
})
|
2021-08-15 00:17:53 +08:00
|
|
|
.catch(error => {
|
2022-12-02 00:06:28 +08:00
|
|
|
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
|
2021-08-15 00:17:53 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpload(info) {
|
|
|
|
this.setState({uploading: true});
|
|
|
|
const filename = info.fileList[0].name;
|
|
|
|
const fullFilePath = `resource/${this.props.account.owner}/${this.props.account.name}/${filename}`;
|
2021-09-06 00:08:16 +08:00
|
|
|
ResourceBackend.uploadResource(this.props.account.owner, this.props.account.name, "custom", "ResourceListPage", fullFilePath, info.file)
|
2021-08-15 00:17:53 +08:00
|
|
|
.then(res => {
|
|
|
|
if (res.status === "ok") {
|
|
|
|
Setting.showMessage("success", i18next.t("application:File uploaded successfully"));
|
|
|
|
window.location.reload();
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", res.msg);
|
|
|
|
}
|
|
|
|
}).finally(() => {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.setState({uploading: false});
|
|
|
|
});
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderUpload() {
|
|
|
|
return (
|
2022-05-04 23:16:24 +08:00
|
|
|
<Upload maxCount={1} accept="image/*,video/*,audio/*,.pdf,.doc,.docx,.csv,.xls,.xlsx" showUploadList={false}
|
2022-07-10 15:45:55 +08:00
|
|
|
beforeUpload={file => {return false;}} onChange={info => {this.handleUpload(info);}}>
|
2021-08-15 00:17:53 +08:00
|
|
|
<Button icon={<UploadOutlined />} loading={this.state.uploading} type="primary" size="small">
|
|
|
|
{i18next.t("resource:Upload a file...")}
|
|
|
|
</Button>
|
|
|
|
</Upload>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTable(resources) {
|
|
|
|
const columns = [
|
2021-08-15 01:09:51 +08:00
|
|
|
{
|
|
|
|
title: i18next.t("general:Provider"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "provider",
|
|
|
|
key: "provider",
|
|
|
|
width: "150px",
|
|
|
|
fixed: "left",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("provider"),
|
2021-08-15 01:09:51 +08:00
|
|
|
render: (text, record, index) => {
|
|
|
|
return (
|
2022-11-21 01:17:55 +08:00
|
|
|
<Link to={`/providers/${record.owner}/${text}`}>
|
2021-08-15 01:09:51 +08:00
|
|
|
{text}
|
|
|
|
</Link>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 01:09:51 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
{
|
2021-09-06 00:08:16 +08:00
|
|
|
title: i18next.t("resource:Application"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "application",
|
|
|
|
key: "application",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("application"),
|
2021-08-15 00:17:53 +08:00
|
|
|
render: (text, record, index) => {
|
2021-09-06 00:08:16 +08:00
|
|
|
return (
|
2022-12-10 22:06:21 +08:00
|
|
|
<Link to={`/applications/${record.organization}/${text}`}>
|
2021-09-06 00:08:16 +08:00
|
|
|
{text}
|
|
|
|
</Link>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
2021-09-05 23:46:38 +08:00
|
|
|
{
|
|
|
|
title: i18next.t("resource:User"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "user",
|
|
|
|
key: "user",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("user"),
|
2021-09-05 23:46:38 +08:00
|
|
|
render: (text, record, index) => {
|
|
|
|
return (
|
|
|
|
<Link to={`/users/${record.owner}/${record.user}`}>
|
|
|
|
{text}
|
|
|
|
</Link>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-09-05 23:46:38 +08:00
|
|
|
},
|
2021-09-04 16:50:26 +08:00
|
|
|
{
|
2021-09-06 00:08:16 +08:00
|
|
|
title: i18next.t("resource:Parent"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "parent",
|
|
|
|
key: "parent",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("parent"),
|
2021-09-06 00:08:16 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("general:Name"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "name",
|
|
|
|
key: "name",
|
|
|
|
width: "150px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("name"),
|
2021-09-06 00:08:16 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("general:Created time"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "createdTime",
|
|
|
|
key: "createdTime",
|
|
|
|
width: "150px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2021-09-04 16:50:26 +08:00
|
|
|
render: (text, record, index) => {
|
2021-09-06 00:08:16 +08:00
|
|
|
return Setting.getFormattedDate(text);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-09-04 16:50:26 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
{
|
2021-09-06 00:08:16 +08:00
|
|
|
title: i18next.t("resource:Tag"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "tag",
|
|
|
|
key: "tag",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("tag"),
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
2021-12-04 16:35:33 +08:00
|
|
|
// {
|
|
|
|
// title: i18next.t("resource:File name"),
|
|
|
|
// dataIndex: 'fileName',
|
|
|
|
// key: 'fileName',
|
|
|
|
// width: '120px',
|
|
|
|
// sorter: (a, b) => a.fileName.localeCompare(b.fileName),
|
|
|
|
// },
|
2021-08-15 00:17:53 +08:00
|
|
|
{
|
2021-09-06 00:08:16 +08:00
|
|
|
title: i18next.t("resource:Type"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "fileType",
|
|
|
|
key: "fileType",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("fileType"),
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
{
|
2021-09-06 00:08:16 +08:00
|
|
|
title: i18next.t("resource:Format"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "fileFormat",
|
|
|
|
key: "fileFormat",
|
|
|
|
width: "80px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
...this.getColumnSearchProps("fileFormat"),
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("resource:File size"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "fileSize",
|
|
|
|
key: "fileSize",
|
|
|
|
width: "100px",
|
2021-12-25 10:55:10 +08:00
|
|
|
sorter: true,
|
2021-08-15 00:17:53 +08:00
|
|
|
render: (text, record, index) => {
|
|
|
|
return Setting.getFriendlyFileSize(text);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("general:Preview"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "preview",
|
|
|
|
key: "preview",
|
|
|
|
width: "100px",
|
2021-08-15 00:17:53 +08:00
|
|
|
render: (text, record, index) => {
|
|
|
|
if (record.fileType === "image") {
|
|
|
|
return (
|
2022-06-05 20:56:31 +08:00
|
|
|
<a target="_blank" rel="noreferrer" href={record.url}>
|
2022-12-03 00:57:11 +08:00
|
|
|
<img src={record.url} alt={record.name} width={200} />
|
2021-08-15 00:17:53 +08:00
|
|
|
</a>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-08-15 00:17:53 +08:00
|
|
|
} else if (record.fileType === "video") {
|
|
|
|
return (
|
2022-12-03 00:57:11 +08:00
|
|
|
<video width={200} controls>
|
|
|
|
<source src={record.url} type="video/mp4" />
|
|
|
|
</video>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("general:URL"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "url",
|
|
|
|
key: "url",
|
|
|
|
width: "120px",
|
2021-08-15 00:17:53 +08:00
|
|
|
render: (text, record, index) => {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Button type="normal" onClick={() => {
|
|
|
|
copy(record.url);
|
|
|
|
Setting.showMessage("success", i18next.t("resource:Link copied to clipboard successfully"));
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{i18next.t("resource:Copy Link")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: i18next.t("general:Action"),
|
2022-07-10 15:45:55 +08:00
|
|
|
dataIndex: "",
|
|
|
|
key: "op",
|
|
|
|
width: "70px",
|
2021-08-15 00:17:53 +08:00
|
|
|
fixed: (Setting.isMobile()) ? "false" : "right",
|
|
|
|
render: (text, record, index) => {
|
|
|
|
return (
|
|
|
|
<div>
|
2022-07-10 15:45:55 +08:00
|
|
|
{/* <Button style={{marginTop: '10px', marginBottom: '10px', marginRight: '10px'}} type="primary" onClick={() => this.props.history.push(`/resources/${record.name}`)}>{i18next.t("general:Edit")}</Button>*/}
|
2021-08-15 00:17:53 +08:00
|
|
|
<Popconfirm
|
|
|
|
title={`Sure to delete resource: ${record.name} ?`}
|
|
|
|
onConfirm={() => this.deleteResource(index)}
|
|
|
|
okText={i18next.t("user:OK")}
|
|
|
|
cancelText={i18next.t("user:Cancel")}
|
|
|
|
>
|
2022-12-04 23:05:30 +08:00
|
|
|
<Button type="primary" danger>{i18next.t("general:Delete")}</Button>
|
2021-08-15 00:17:53 +08:00
|
|
|
</Popconfirm>
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-08-06 23:54:56 +08:00
|
|
|
},
|
2021-08-15 00:17:53 +08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
const paginationProps = {
|
2021-12-25 10:55:10 +08:00
|
|
|
total: this.state.pagination.total,
|
2021-11-06 11:32:22 +08:00
|
|
|
showQuickJumper: true,
|
|
|
|
showSizeChanger: true,
|
2021-12-25 10:55:10 +08:00
|
|
|
showTotal: () => i18next.t("general:{total} in total").replace("{total}", this.state.pagination.total),
|
2021-11-06 11:32:22 +08:00
|
|
|
};
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2022-07-10 15:45:55 +08:00
|
|
|
<Table scroll={{x: "max-content"}} columns={columns} dataSource={resources} rowKey="name" size="middle" bordered pagination={paginationProps}
|
|
|
|
title={() => (
|
|
|
|
<div>
|
|
|
|
{i18next.t("general:Resources")}
|
|
|
|
{/* <Button type="primary" size="small" onClick={this.addResource.bind(this)}>{i18next.t("general:Add")}</Button>*/}
|
|
|
|
{
|
|
|
|
this.renderUpload()
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
loading={this.state.loading}
|
|
|
|
onChange={this.handleTableChange}
|
2021-08-15 00:17:53 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:55:10 +08:00
|
|
|
fetch = (params = {}) => {
|
2022-08-08 23:35:24 +08:00
|
|
|
const field = params.searchedColumn, value = params.searchText;
|
|
|
|
const sortField = params.sortField, sortOrder = params.sortOrder;
|
2022-07-10 15:45:55 +08:00
|
|
|
this.setState({loading: true});
|
2021-12-25 10:55:10 +08:00
|
|
|
ResourceBackend.getResources(this.props.account.owner, this.props.account.name, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.status === "ok") {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
data: res.data,
|
|
|
|
pagination: {
|
|
|
|
...params.pagination,
|
|
|
|
total: res.data2,
|
|
|
|
},
|
|
|
|
searchText: params.searchText,
|
|
|
|
searchedColumn: params.searchedColumn,
|
|
|
|
});
|
2023-01-06 15:04:13 +08:00
|
|
|
} else {
|
|
|
|
if (res.data.includes("Please login first")) {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
isAuthorized: false,
|
|
|
|
});
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
2021-12-25 10:55:10 +08:00
|
|
|
});
|
|
|
|
};
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ResourceListPage;
|