import axios from 'axios'; import React, { Component } from 'react'; import Button from 'react-bootstrap/Button'; import Col from 'react-bootstrap/Col'; import Form from 'react-bootstrap/Form'; import Spinner from 'react-bootstrap/Spinner'; import { handleChange, RequiredField, validateRequiredFields } from './forms'; import { SetupFilesystem } from './SetupFilesystem'; import { SetupGCS } from './SetupGCS'; import { SetupS3 } from './SetupS3'; import { SetupB2 } from "./SetupB2"; import { SetupAzure } from './SetupAzure'; import { SetupSFTP } from './SetupSFTP'; import { SetupToken } from './SetupToken'; import { SetupWebDAV } from './SetupWebDAV'; const supportedProviders = [ { provider: "filesystem", description: "Filesystem", component: SetupFilesystem }, { provider: "gcs", description: "Google Cloud Storage", component: SetupGCS }, { provider: "s3", description: "Amazon S3, Minio, Wasabi, etc.", component: SetupS3 }, { provider: "b2", description: "Backblaze B2", component: SetupB2 }, { provider: "azureBlob", description: "Azure Blob Storage", component: SetupAzure }, { provider: "sftp", description: "SFTP server", component: SetupSFTP }, { provider: "webdav", description: "WebDAV server", component: SetupWebDAV }, { provider: "_token", description: "(use token)", component: SetupToken }, ]; export class SetupRepository extends Component { constructor() { super(); this.state = { confirmCreate: false, isLoading: false, }; this.handleChange = handleChange.bind(this); this.optionsEditor = React.createRef(); this.initRepository = this.initRepository.bind(this); this.connectToRepository = this.connectToRepository.bind(this); this.cancelCreate = this.cancelCreate.bind(this); } componentDidMount() { axios.get('/api/v1/repo/algorithms').then(result => { this.setState({ algorithms: result.data, hash: result.data.defaultHash, encryption: result.data.defaultEncryption, splitter: result.data.defaultSplitter, }); }); } validate() { const ed = this.optionsEditor.current; let valid = true; if (this.state.provider !== "_token") { if (!validateRequiredFields(this, ["password"])) { valid = false; } } if (ed && !ed.validate()) { valid = false; } if (this.state.confirmCreate) { if (!validateRequiredFields(this, ["confirmPassword"])) { valid = false; } if (valid && this.state.password !== this.state.confirmPassword) { alert("Passwords don't match"); return false; } } return valid; } initRepository() { if (!this.validate()) { return; } const ed = this.optionsEditor.current; if (!ed) { return } const request = { storage: { type: this.state.provider, config: ed.state, }, password: this.state.password, options: { blockFormat: { hash: this.state.hash, encryption: this.state.encryption, }, objectFormat: { splitter: this.state.splitter, }, }, }; axios.post('/api/v1/repo/create', request).then(result => { window.location.replace("/"); }).catch(error => { alert('failed to create repository: ' + JSON.stringify(error.response.data)); }); } connectToRepository() { if (!this.validate()) { return; } const ed = this.optionsEditor.current; if (!ed) { return } let request = null; if (this.state.provider === "_token") { request = { token: ed.state.token, } } else { request = { storage: { type: this.state.provider, config: ed.state, }, password: this.state.password, } } this.setState({ isLoading: true }); axios.post('/api/v1/repo/connect', request).then(result => { this.setState({ isLoading: false }); window.location.replace("/"); }).catch(error => { this.setState({ isLoading: false }); if (error.response.data) { if (error.response.data.code === "NOT_INITIALIZED") { this.setState({ confirmCreate: true, connectError: null, }); } else { this.setState({ confirmCreate: false, connectError: error.response.data.code + ": " + error.response.data.error, }); } } }); } cancelCreate() { this.setState({ confirmCreate: false }); } render() { let SelectedProvider = null; for (const prov of supportedProviders) { if (prov.provider === this.state.provider) { SelectedProvider = prov.component; } } return
{!this.state.confirmCreate && Provider {supportedProviders.map(x => )} } {SelectedProvider && <>
{this.state.confirmCreate && <> Kopia repository was not found in the provided location and needs to be set up.
Please provide strong password to protect repository contents and optionally choose additional parameters.
} {this.state.provider !== "_token" && {RequiredField(this, "Repository Password", "password", { type: "password", placeholder: "enter repository password" })} {this.state.confirmCreate && RequiredField(this, "Confirm Repository Password", "confirmPassword", { type: "password", placeholder: "enter repository password again" })} } {!this.state.confirmCreate && } {this.state.connectError && Connect Error: {this.state.connectError} } {this.state.confirmCreate && <> Encryption {this.state.algorithms.encryption.map(x => )} Hash Algorithm {this.state.algorithms.hash.map(x => )} Splitter {this.state.algorithms.splitter.map(x => )} Additional parameters can be set when creating repository using command line   } } {/*
                {JSON.stringify(this.state)}
            
*/}
; } }