1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { Dialog, DialogActions, Button, StyleRulesCallback, WithStyles, withStyles, CardHeader, Tab, Tabs } from '@material-ui/core';
7 import { withDialog } from "~/store/dialog/with-dialog";
8 import { COLLECTION_WEBDAV_S3_DIALOG_NAME, WebDavS3InfoDialogData } from '~/store/collections/collection-info-actions';
9 import { WithDialogProps } from '~/store/dialog/with-dialog';
10 import { compose } from 'redux';
11 import { DetailsAttribute } from "~/components/details-attribute/details-attribute";
12 import { DownloadIcon } from "~/components/icon/icon";
14 export type CssRules = 'details' | 'downloadButton';
16 const styles: StyleRulesCallback<CssRules> = theme => ({
18 marginLeft: theme.spacing.unit * 3,
19 marginRight: theme.spacing.unit * 3,
22 marginTop: theme.spacing.unit * 2,
26 interface TabPanelData {
27 children: React.ReactElement<any>[];
32 function TabPanel(props: TabPanelData) {
33 const { children, value, index } = props;
38 hidden={value !== index}
39 id={`simple-tabpanel-${index}`}
40 aria-labelledby={`simple-tab-${index}`}
42 {value === index && children}
47 const isValidIpAddress = (ipAddress: string): Boolean => {
48 if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipAddress)) {
55 const mountainduckTemplate = ({
62 return `<?xml version="1.0" encoding="UTF-8"?>
63 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
69 <string>iterate GmbH</string>
71 <string>${uuid}</string>
73 <string>${collectionsUrl.replace('https://', ``).replace('*', uuid).split(':')[0]}</string>
75 <string>${(cyberDavStr.split(':')[2] || '443').split('/')[0]}</string>
77 <string>${username}</string>${isValidIpAddress(collectionsUrl.replace('https://', ``).split(':')[0])?
80 <string>/c=${uuid}</string>` : ''}
85 </plist>`.split(/\r?\n/).join('\n');
88 const downloadMountainduckFileHandler = (filename: string, text: string) => {
89 const element = document.createElement('a');
90 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
91 element.setAttribute('download', filename);
93 element.style.display = 'none';
94 document.body.appendChild(element);
98 document.body.removeChild(element);
101 export const WebDavS3InfoDialog = compose(
102 withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
105 (props: WithDialogProps<WebDavS3InfoDialogData> & WithStyles<CssRules>) => {
106 if (!props.data.downloadUrl) { return null; }
111 if (props.data.collectionsUrl.indexOf("*") > -1) {
112 const withuuid = props.data.collectionsUrl.replace("*", props.data.uuid);
113 winDav = new URL(withuuid);
114 cyberDav = new URL(withuuid);
116 winDav = new URL(props.data.downloadUrl);
117 cyberDav = new URL(props.data.downloadUrl);
118 winDav.pathname = `/by_id/${props.data.uuid}`;
119 cyberDav.pathname = `/by_id/${props.data.uuid}`;
122 cyberDav.username = props.data.username;
123 const cyberDavStr = "dav" + cyberDav.toString().slice(4);
125 const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
127 const sp = props.data.token.split("/");
130 if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
134 tokenUuid = props.data.token.replace(/\//g, "_");
135 tokenSecret = tokenUuid;
138 const supportsWebdav = (props.data.uuid.indexOf("-4zz18-") === 5);
140 let activeTab = props.data.activeTab;
141 if (!supportsWebdav) {
148 onClose={props.closeDialog}
149 style={{ alignSelf: 'stretch' }}>
151 title={`Open as Network Folder or S3 Bucket`} />
152 <div className={props.classes.details} >
153 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
154 {supportsWebdav && <Tab value={0} key="cyberduck" label="Cyberduck/Mountain Duck or Gnome Files" />}
155 {supportsWebdav && <Tab value={1} key="windows" label="Windows or MacOS" />}
156 <Tab value={2} key="s3" label="S3 bucket" />
159 <TabPanel index={1} value={activeTab}>
163 label='Internet address'
164 value={<a href={winDav.toString()} target="_blank">{winDav.toString()}</a>}
165 copyValue={winDav.toString()} />
169 value={props.data.username}
170 copyValue={props.data.username} />
174 value={props.data.token}
175 copyValue={props.data.token} />
179 <li>Open File Explorer</li>
180 <li>Click on "This PC", then go to Computer → Add a Network Location</li>
181 <li>Click Next, then choose "Add a custom network location", then click Next</li>
187 <li>Click Go → Connect to server</li>
191 <TabPanel index={0} value={activeTab}>
194 value={<a href={cyberDavStr} target="_blank">{cyberDavStr}</a>}
195 copyValue={cyberDavStr} />
199 value={props.data.username}
200 copyValue={props.data.username} />
204 value={props.data.token}
205 copyValue={props.data.token} />
208 data-cy='download-button'
209 className={props.classes.downloadButton}
210 onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate({ ...props.data, cyberDavStr }))}
215 Download Cyber/Mountain Duck bookmark
221 <li>Select +Other Locations</li>
222 <li>Connect to Server → Enter server address</li>
227 <TabPanel index={2} value={activeTab}>
230 value={s3endpoint.host}
231 copyValue={s3endpoint.host} />
235 value={props.data.uuid}
236 copyValue={props.data.uuid} />
241 copyValue={tokenUuid} />
246 copyValue={tokenSecret} />
255 onClick={props.closeDialog}>