2021-01-07 20:47:43 +01:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2021-01-09 23:34:45 +01:00
|
|
|
import API from "./api";
|
2021-01-10 01:08:03 +01:00
|
|
|
import Icon from "./elements/icon";
|
|
|
|
import {FileBrowser} from "./elements/file-browser";
|
|
|
|
import {TokenList} from "./elements/token-list";
|
2021-01-07 20:47:43 +01:00
|
|
|
|
|
|
|
class FileControlPanel extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.api = new API();
|
|
|
|
this.state = {
|
2021-01-10 01:08:03 +01:00
|
|
|
loaded: false,
|
|
|
|
validatingToken: false,
|
|
|
|
errorMessage: "",
|
|
|
|
user: { },
|
|
|
|
token: { valid: false, value: "", validUntil: null, type: null },
|
|
|
|
files: [],
|
2021-01-07 20:47:43 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-12 15:49:41 +01:00
|
|
|
onValidateToken(token = null) {
|
|
|
|
if (token === null) {
|
|
|
|
this.setState({ ...this.state, validatingToken: true, errorMessage: "" });
|
|
|
|
token = this.state.token.value;
|
|
|
|
}
|
|
|
|
this.api.validateToken(token).then((res) => {
|
|
|
|
let newState = { ...this.state, loaded: true, validatingToken: false };
|
2021-01-10 01:08:03 +01:00
|
|
|
if (res.success) {
|
2021-01-12 15:49:41 +01:00
|
|
|
newState.token = { ...this.state.token, valid: true, validUntil: res.token.valid_until, type: res.token.type };
|
|
|
|
if (!newState.token.value) {
|
|
|
|
newState.token.value = token;
|
|
|
|
}
|
|
|
|
newState.files = res.files;
|
2021-01-10 01:08:03 +01:00
|
|
|
} else {
|
2021-01-12 15:49:41 +01:00
|
|
|
newState.errorMessage = res.msg;
|
2021-01-10 01:08:03 +01:00
|
|
|
}
|
2021-01-12 15:49:41 +01:00
|
|
|
|
|
|
|
this.setState(newState);
|
2021-01-10 01:08:03 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpdateToken(e) {
|
|
|
|
this.setState({ ...this.state, token: { ...this.state.token, value: e.target.value } });
|
|
|
|
}
|
|
|
|
|
2021-01-07 20:47:43 +01:00
|
|
|
render() {
|
2021-01-10 01:08:03 +01:00
|
|
|
const self = this;
|
|
|
|
const errorMessageShown = !!this.state.errorMessage;
|
2021-01-07 22:01:56 +01:00
|
|
|
|
2021-01-09 23:34:45 +01:00
|
|
|
if (!this.state.loaded) {
|
2021-01-12 15:49:41 +01:00
|
|
|
|
|
|
|
let checkUser = true;
|
|
|
|
let pathName = window.location.pathname;
|
|
|
|
if (pathName.length > 1) {
|
|
|
|
let end = (pathName.endsWith("/") ? pathName.length - 2 : pathName.length - 1);
|
|
|
|
let start = (pathName.startsWith("/files/") ? ("/files/").length : 1);
|
|
|
|
let token = pathName.substr(start, end);
|
|
|
|
if (token) {
|
|
|
|
// this.setState({ ...this.state, loaded: true, token: { ...this.state.token, value: token } });
|
|
|
|
this.onValidateToken(token);
|
|
|
|
checkUser = false;
|
2021-01-10 01:08:03 +01:00
|
|
|
}
|
2021-01-12 15:49:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (checkUser) {
|
|
|
|
this.api.fetchUser().then((isLoggedIn) => {
|
|
|
|
if (isLoggedIn) {
|
|
|
|
this.api.listFiles().then((res) => {
|
|
|
|
this.setState({ ...this.state, loaded: true, user: this.api.user, files: res.files });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({ ...this.state, loaded: true, user: this.api.user });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-10 01:08:03 +01:00
|
|
|
return <>Loading… <Icon icon={"spinner"} /></>;
|
|
|
|
} else if (this.api.loggedIn || this.state.token.valid) {
|
|
|
|
let tokenList = (this.api.loggedIn) ?
|
|
|
|
<div className={"row"}>
|
2021-01-10 23:41:00 +01:00
|
|
|
<div className={"col-lg-8 col-md-10 col-sm-12 mx-auto"}>
|
2021-01-10 01:08:03 +01:00
|
|
|
<TokenList api={this.api} />
|
|
|
|
</div>
|
|
|
|
</div> :
|
|
|
|
<></>;
|
2021-01-07 22:01:56 +01:00
|
|
|
|
2021-01-13 01:36:04 +01:00
|
|
|
return <>
|
|
|
|
<div className={"container mt-4"}>
|
|
|
|
<div className={"row"}>
|
|
|
|
<div className={"col-lg-8 col-md-10 col-sm-12 mx-auto"}>
|
|
|
|
<h2>File Control Panel</h2>
|
|
|
|
<FileBrowser files={this.state.files} token={this.state.token} api={this.api} />
|
|
|
|
</div>
|
2021-01-10 01:08:03 +01:00
|
|
|
</div>
|
2021-01-13 01:36:04 +01:00
|
|
|
{ tokenList }
|
2021-01-10 01:08:03 +01:00
|
|
|
</div>
|
2021-01-13 01:36:04 +01:00
|
|
|
</>;
|
2021-01-07 22:01:56 +01:00
|
|
|
} else {
|
2021-01-10 01:08:03 +01:00
|
|
|
return <div className={"container mt-4"}>
|
|
|
|
<div className={"row"}>
|
2021-01-10 23:41:00 +01:00
|
|
|
<div className={"col-lg-8 col-md-10 col-sm-12 mx-auto"}>
|
2021-01-10 01:08:03 +01:00
|
|
|
<h2>File Control Panel</h2>
|
|
|
|
<form onSubmit={(e) => e.preventDefault()}>
|
|
|
|
<label htmlFor={"token"}>Enter a file token to download or upload files</label>
|
|
|
|
<input type={"text"} className={"form-control"} name={"token"} placeholder={"Enter token…"} maxLength={36}
|
|
|
|
value={this.state.token.value} onChange={(e) => self.onUpdateToken(e)}/>
|
2021-01-12 15:49:41 +01:00
|
|
|
<button className={"btn btn-success mt-2"} onClick={() => this.onValidateToken()} disabled={this.state.validatingToken}>
|
2021-01-10 01:08:03 +01:00
|
|
|
{ this.state.validatingToken ? <>Validating… <Icon icon={"spinner"}/></> : "Submit" }
|
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
<div className={"alert alert-danger mt-2"} hidden={!errorMessageShown}>
|
|
|
|
{ this.state.errorMessage }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>;
|
2021-01-07 22:01:56 +01:00
|
|
|
}
|
2021-01-07 20:47:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<FileControlPanel />,
|
|
|
|
document.getElementById('root')
|
|
|
|
);
|