c1acfd80b7241695ee4179949c4c60d0ccba412e
[arvados-workbench2.git] / src / views-components / webdav-s3-dialog / webdav-s3-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
13
14 export type CssRules = 'details' | 'downloadButton';
15
16 const styles: StyleRulesCallback<CssRules> = theme => ({
17     details: {
18         marginLeft: theme.spacing.unit * 3,
19         marginRight: theme.spacing.unit * 3,
20     },
21     downloadButton: {
22         marginTop: theme.spacing.unit * 2,
23     }
24 });
25
26 interface TabPanelData {
27     children: React.ReactElement<any>[];
28     value: number;
29     index: number;
30 }
31
32 function TabPanel(props: TabPanelData) {
33     const { children, value, index } = props;
34
35     return (
36         <div
37             role="tabpanel"
38             hidden={value !== index}
39             id={`simple-tabpanel-${index}`}
40             aria-labelledby={`simple-tab-${index}`}
41         >
42             {value === index && children}
43         </div>
44     );
45 }
46
47 const mountainduckTemplate = ({
48     uuid, 
49     username,
50     cyberDavStr,
51     collectionsUrl
52 }: any) => `<?xml version="1.0" encoding="UTF-8"?>
53 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
54 <plist version="1.0">
55    <dict>
56       <key>Protocol</key>
57       <string>davs</string>
58       <key>Provider</key>
59       <string>iterate GmbH</string>
60       <key>UUID</key>
61       <string>${uuid}</string>
62       <key>Hostname</key>
63       <string>${collectionsUrl.replace('https://', ``).replace('*', uuid).split(':')[0]}</string>
64       <key>Port</key>
65       <string>${(cyberDavStr.split(':')[2] || '443').split('/')[0]}</string>
66       <key>Username</key>
67       <string>${username}</string>
68       <key>Labels</key>
69       <array>
70       </array>
71    </dict>
72 </plist>
73 `.split(/\r?\n/).join('\n');
74
75 const downloadMountainduckFileHandler = (filename: string, text: string) => {
76     const element = document.createElement('a');
77     element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
78     element.setAttribute('download', filename);
79   
80     element.style.display = 'none';
81     document.body.appendChild(element);
82   
83     element.click();
84   
85     document.body.removeChild(element);
86 };
87
88 export const WebDavS3InfoDialog = compose(
89     withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
90     withStyles(styles),
91 )(
92     (props: WithDialogProps<WebDavS3InfoDialogData> & WithStyles<CssRules>) => {
93         if (!props.data.downloadUrl) { return null; }
94
95         let winDav;
96         let cyberDav;
97
98         if (props.data.collectionsUrl.indexOf("*") > -1) {
99             const withuuid = props.data.collectionsUrl.replace("*", props.data.uuid);
100             winDav = new URL(withuuid);
101             cyberDav = new URL(withuuid);
102         } else {
103             winDav = new URL(props.data.downloadUrl);
104             cyberDav = new URL(props.data.downloadUrl);
105             winDav.pathname = `/by_id/${props.data.uuid}`;
106             cyberDav.pathname = `/by_id/${props.data.uuid}`;
107         }
108
109         cyberDav.username = props.data.username;
110         const cyberDavStr = "dav" + cyberDav.toString().slice(4);
111
112         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
113
114         const sp = props.data.token.split("/");
115         let tokenUuid;
116         let tokenSecret;
117         if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
118             tokenUuid = sp[1];
119             tokenSecret = sp[2];
120         } else {
121             tokenUuid = props.data.token.replace(/\//g, "_");
122             tokenSecret = tokenUuid;
123         }
124
125         const supportsWebdav = (props.data.uuid.indexOf("-4zz18-") === 5);
126
127         let activeTab = props.data.activeTab;
128         if (!supportsWebdav) {
129             activeTab = 2;
130         }
131
132         return <Dialog
133             open={props.open}
134             maxWidth="md"
135             onClose={props.closeDialog}
136             style={{ alignSelf: 'stretch' }}>
137             <CardHeader
138                 title={`Open as Network Folder or S3 Bucket`} />
139             <div className={props.classes.details} >
140                 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
141                     {supportsWebdav && <Tab value={0} key="cyberduck" label="Cyberduck/Mountain Duck or Gnome Files" />}
142                     {supportsWebdav && <Tab value={1} key="windows" label="Windows or MacOS" />}
143                     <Tab value={2} key="s3" label="S3 bucket" />
144                 </Tabs>
145
146                 <TabPanel index={1} value={activeTab}>
147                     <h2>Settings</h2>
148
149                     <DetailsAttribute
150                         label='Internet address'
151                         value={<a href={winDav.toString()} target="_blank">{winDav.toString()}</a>}
152                         copyValue={winDav.toString()} />
153
154                     <DetailsAttribute
155                         label='Username'
156                         value={props.data.username}
157                         copyValue={props.data.username} />
158
159                     <DetailsAttribute
160                         label='Password'
161                         value={props.data.token}
162                         copyValue={props.data.token} />
163
164                     <h3>Windows</h3>
165                     <ol>
166                         <li>Open File Explorer</li>
167                         <li>Click on "This PC", then go to Computer &rarr; Add a Network Location</li>
168                         <li>Click Next, then choose "Add a custom network location", then click Next</li>
169                     </ol>
170
171                     <h3>MacOS</h3>
172                     <ol>
173                         <li>Open Finder</li>
174                         <li>Click Go &rarr; Connect to server</li>
175                     </ol>
176                 </TabPanel>
177
178                 <TabPanel index={0} value={activeTab}>
179                     <DetailsAttribute
180                         label='Server'
181                         value={<a href={cyberDavStr} target="_blank">{cyberDavStr}</a>}
182                         copyValue={cyberDavStr} />
183
184                     <DetailsAttribute
185                         label='Username'
186                         value={props.data.username}
187                         copyValue={props.data.username} />
188
189                     <DetailsAttribute
190                         label='Password'
191                         value={props.data.token}
192                         copyValue={props.data.token} />
193
194                     <Button
195                         data-cy='download-button'
196                         className={props.classes.downloadButton}
197                         onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate({ ...props.data, cyberDavStr }))}
198                         variant='contained'
199                         color='primary'
200                         size='small'>
201                         <DownloadIcon />
202                         Download config
203                     </Button>
204
205                     <h3>Gnome</h3>
206                     <ol>
207                         <li>Open Files</li>
208                         <li>Select +Other Locations</li>
209                         <li>Connect to Server &rarr; Enter server address</li>
210                     </ol>
211
212                 </TabPanel>
213
214                 <TabPanel index={2} value={activeTab}>
215                     <DetailsAttribute
216                         label='Endpoint'
217                         value={s3endpoint.host}
218                         copyValue={s3endpoint.host} />
219
220                     <DetailsAttribute
221                         label='Bucket'
222                         value={props.data.uuid}
223                         copyValue={props.data.uuid} />
224
225                     <DetailsAttribute
226                         label='Access Key'
227                         value={tokenUuid}
228                         copyValue={tokenUuid} />
229
230                     <DetailsAttribute
231                         label='Secret Key'
232                         value={tokenSecret}
233                         copyValue={tokenSecret} />
234
235                 </TabPanel>
236
237             </div>
238             <DialogActions>
239                 <Button
240                     variant='text'
241                     color='primary'
242                     onClick={props.closeDialog}>
243                     Close
244                 </Button>
245             </DialogActions>
246
247         </Dialog >;
248     }
249 );