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