17526: Fix webdav dialog > wget tab command copy button only copying url
[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 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 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)) {
49         return true;
50     }
51
52     return false;
53 };
54
55 const mountainduckTemplate = ({
56     uuid,
57     username,
58     cyberDavStr,
59     collectionsUrl
60 }: any) => {
61
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">
64         <plist version="1.0">
65         <dict>
66             <key>Protocol</key>
67             <string>davs</string>
68             <key>Provider</key>
69             <string>iterate GmbH</string>
70             <key>UUID</key>
71             <string>${uuid}</string>
72             <key>Hostname</key>
73             <string>${collectionsUrl.replace('https://', ``).replace('*', uuid).split(':')[0]}</string>
74             <key>Port</key>
75             <string>${(cyberDavStr.split(':')[2] || '443').split('/')[0]}</string>
76             <key>Username</key>
77             <string>${username}</string>${isValidIpAddress(collectionsUrl.replace('https://', ``).split(':')[0])?
78             `
79             <key>Path</key>
80             <string>/c=${uuid}</string>` : ''}
81             <key>Labels</key>
82             <array>
83             </array>
84         </dict>
85         </plist>`.split(/\r?\n/).join('\n');
86 };
87
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);
92
93     element.style.display = 'none';
94     document.body.appendChild(element);
95
96     element.click();
97
98     document.body.removeChild(element);
99 };
100
101 export const WebDavS3InfoDialog = compose(
102     withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
103     withStyles(styles),
104 )(
105     (props: WithDialogProps<WebDavS3InfoDialogData> & WithStyles<CssRules>) => {
106         if (!props.data.downloadUrl) { return null; }
107
108         let winDav;
109         let cyberDav;
110
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);
115         } else {
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}`;
120         }
121
122         cyberDav.username = props.data.username;
123         const cyberDavStr = "dav" + cyberDav.toString().slice(4);
124
125         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
126
127         const sp = props.data.token.split("/");
128         let tokenUuid;
129         let tokenSecret;
130         if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
131             tokenUuid = sp[1];
132             tokenSecret = sp[2];
133         } else {
134             tokenUuid = props.data.token.replace(/\//g, "_");
135             tokenSecret = tokenUuid;
136         }
137
138         const isCollection = (props.data.uuid.indexOf("-4zz18-") === 5);
139
140         let activeTab = props.data.activeTab;
141         if (!isCollection) {
142             activeTab = 2;
143         }
144
145         const wgetCommand = `wget --http-user=${props.data.username} --http-passwd=${props.data.token} --mirror --no-parent --no-host --cut-dirs=0 ${winDav.toString()}`;
146
147         return <Dialog
148             open={props.open}
149             maxWidth="md"
150             onClose={props.closeDialog}
151             style={{ alignSelf: 'stretch' }}>
152             <CardHeader
153                 title={`Open with 3rd party client`} />
154             <div className={props.classes.details} >
155                 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
156                     {isCollection && <Tab value={0} key="cyberduck" label="WebDAV" />}
157                     {isCollection && <Tab value={1} key="windows" label="Windows or MacOS" />}
158                     <Tab value={2} key="s3" label="S3 bucket" />
159                     {isCollection && <Tab value={3} key="cli" label="wget / curl" />}
160                 </Tabs>
161
162                 <TabPanel index={1} value={activeTab}>
163                     <h2>Settings</h2>
164
165                     <DetailsAttribute
166                         label='Internet address'
167                         value={<a href={winDav.toString()} target="_blank" rel="noopener noreferrer">{winDav.toString()}</a>}
168                         copyValue={winDav.toString()} />
169
170                     <DetailsAttribute
171                         label='Username'
172                         value={props.data.username}
173                         copyValue={props.data.username} />
174
175                     <DetailsAttribute
176                         label='Password'
177                         value={props.data.token}
178                         copyValue={props.data.token} />
179
180                     <h3>Windows</h3>
181                     <ol>
182                         <li>Open File Explorer</li>
183                         <li>Click on "This PC", then go to Computer &rarr; Add a Network Location</li>
184                         <li>Click Next, then choose "Add a custom network location", then click Next</li>
185                         <li>Use the "internet address" and credentials listed under Settings, above</li>
186                     </ol>
187
188                     <h3>MacOS</h3>
189                     <ol>
190                         <li>Open Finder</li>
191                         <li>Click Go &rarr; Connect to server</li>
192                         <li>Use the "internet address" and credentials listed under Settings, above</li>
193                     </ol>
194                 </TabPanel>
195
196                 <TabPanel index={0} value={activeTab}>
197                     <DetailsAttribute
198                         label='Server'
199                         value={<a href={cyberDavStr} target="_blank" rel="noopener noreferrer">{cyberDavStr}</a>}
200                         copyValue={cyberDavStr} />
201
202                     <DetailsAttribute
203                         label='Username'
204                         value={props.data.username}
205                         copyValue={props.data.username} />
206
207                     <DetailsAttribute
208                         label='Password'
209                         value={props.data.token}
210                         copyValue={props.data.token} />
211
212                     <h3>Cyberduck/Mountain Duck</h3>
213
214                     <Button
215                         data-cy='download-button'
216                         className={props.classes.downloadButton}
217                         onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate({ ...props.data, cyberDavStr }))}
218                         variant='contained'
219                         color='primary'
220                         size='small'>
221                         <DownloadIcon />
222                         Download Cyber/Mountain Duck bookmark
223                     </Button>
224
225                     <h3>Gnome</h3>
226                     <ol>
227                         <li>Open Files</li>
228                         <li>Select +Other Locations</li>
229                         <li>Connect to Server &rarr; Enter server address</li>
230                     </ol>
231
232                 </TabPanel>
233
234                 <TabPanel index={2} value={activeTab}>
235                     <DetailsAttribute
236                         label='Endpoint'
237                         value={s3endpoint.host}
238                         copyValue={s3endpoint.host} />
239
240                     <DetailsAttribute
241                         label='Bucket'
242                         value={props.data.uuid}
243                         copyValue={props.data.uuid} />
244
245                     <DetailsAttribute
246                         label='Access Key'
247                         value={tokenUuid}
248                         copyValue={tokenUuid} />
249
250                     <DetailsAttribute
251                         label='Secret Key'
252                         value={tokenSecret}
253                         copyValue={tokenSecret} />
254
255                 </TabPanel>
256
257                 <TabPanel index={3} value={activeTab}>
258
259                     <DetailsAttribute
260                         label='Wget command'
261                         value={wgetCommand}
262                         copyValue={wgetCommand} />
263
264                     <DetailsAttribute
265                         label='Username'
266                         value={props.data.username}
267                         copyValue={props.data.username} />
268
269                     <DetailsAttribute
270                         label='Password'
271                         value={props.data.token}
272                         copyValue={props.data.token} />
273
274                 </TabPanel>
275
276             </div>
277             <DialogActions>
278                 <Button
279                     variant='text'
280                     color='primary'
281                     onClick={props.closeDialog}>
282                     Close
283                 </Button>
284             </DialogActions>
285
286         </Dialog >;
287     }
288 );