17526: Remove filename placeholder from webdav dialog curl command
[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 import { DefaultCodeSnippet } from "components/default-code-snippet/default-code-snippet";
14
15 export type CssRules = 'details' | 'downloadButton' | 'detailsAttrValWithCode';
16
17 const styles: StyleRulesCallback<CssRules> = theme => ({
18     details: {
19         marginLeft: theme.spacing.unit * 3,
20         marginRight: theme.spacing.unit * 3,
21     },
22     downloadButton: {
23         marginTop: theme.spacing.unit * 2,
24     },
25     detailsAttrValWithCode: {
26         display: "flex",
27         alignItems: "center",
28     }
29 });
30
31 interface TabPanelData {
32     children: React.ReactElement<any>[];
33     value: number;
34     index: number;
35 }
36
37 function TabPanel(props: TabPanelData) {
38     const { children, value, index } = props;
39
40     return (
41         <div
42             role="tabpanel"
43             hidden={value !== index}
44             id={`simple-tabpanel-${index}`}
45             aria-labelledby={`simple-tab-${index}`}
46         >
47             {value === index && children}
48         </div>
49     );
50 }
51
52 const isValidIpAddress = (ipAddress: string): Boolean => {
53     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)) {
54         return true;
55     }
56
57     return false;
58 };
59
60 const mountainduckTemplate = ({
61     uuid,
62     username,
63     cyberDavStr,
64     collectionsUrl
65 }: any) => {
66
67     return `<?xml version="1.0" encoding="UTF-8"?>
68         <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
69         <plist version="1.0">
70         <dict>
71             <key>Protocol</key>
72             <string>davs</string>
73             <key>Provider</key>
74             <string>iterate GmbH</string>
75             <key>UUID</key>
76             <string>${uuid}</string>
77             <key>Hostname</key>
78             <string>${collectionsUrl.replace('https://', ``).replace('*', uuid).split(':')[0]}</string>
79             <key>Port</key>
80             <string>${(cyberDavStr.split(':')[2] || '443').split('/')[0]}</string>
81             <key>Username</key>
82             <string>${username}</string>${isValidIpAddress(collectionsUrl.replace('https://', ``).split(':')[0])?
83             `
84             <key>Path</key>
85             <string>/c=${uuid}</string>` : ''}
86             <key>Labels</key>
87             <array>
88             </array>
89         </dict>
90         </plist>`.split(/\r?\n/).join('\n');
91 };
92
93 const downloadMountainduckFileHandler = (filename: string, text: string) => {
94     const element = document.createElement('a');
95     element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
96     element.setAttribute('download', filename);
97
98     element.style.display = 'none';
99     document.body.appendChild(element);
100
101     element.click();
102
103     document.body.removeChild(element);
104 };
105
106 export const WebDavS3InfoDialog = compose(
107     withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
108     withStyles(styles),
109 )(
110     (props: WithDialogProps<WebDavS3InfoDialogData> & WithStyles<CssRules>) => {
111         if (!props.data.downloadUrl) { return null; }
112
113         let winDav;
114         let cyberDav;
115
116         if (props.data.collectionsUrl.indexOf("*") > -1) {
117             const withuuid = props.data.collectionsUrl.replace("*", props.data.uuid);
118             winDav = new URL(withuuid);
119             cyberDav = new URL(withuuid);
120         } else {
121             winDav = new URL(props.data.downloadUrl);
122             cyberDav = new URL(props.data.downloadUrl);
123             winDav.pathname = `/by_id/${props.data.uuid}`;
124             cyberDav.pathname = `/by_id/${props.data.uuid}`;
125         }
126
127         cyberDav.username = props.data.username;
128         const cyberDavStr = "dav" + cyberDav.toString().slice(4);
129
130         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
131
132         const sp = props.data.token.split("/");
133         let tokenUuid;
134         let tokenSecret;
135         if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
136             tokenUuid = sp[1];
137             tokenSecret = sp[2];
138         } else {
139             tokenUuid = props.data.token.replace(/\//g, "_");
140             tokenSecret = tokenUuid;
141         }
142
143         const isCollection = (props.data.uuid.indexOf("-4zz18-") === 5);
144
145         let activeTab = props.data.activeTab;
146         if (!isCollection) {
147             activeTab = 2;
148         }
149
150         const wgetCommand = `wget --http-user=${props.data.username} --http-passwd=${props.data.token} --mirror --no-parent --no-host --cut-dirs=0 ${winDav.toString()}`;
151         const curlCommand = `curl -O -u ${props.data.username}:${props.data.token} ${winDav.toString()}`;
152
153         return <Dialog
154             open={props.open}
155             maxWidth="md"
156             onClose={props.closeDialog}
157             style={{ alignSelf: 'stretch' }}>
158             <CardHeader
159                 title={`Open with 3rd party client`} />
160             <div className={props.classes.details} >
161                 <Tabs value={activeTab} onChange={props.data.setActiveTab}>
162                     {isCollection && <Tab value={0} key="cyberduck" label="WebDAV" />}
163                     {isCollection && <Tab value={1} key="windows" label="Windows or MacOS" />}
164                     <Tab value={2} key="s3" label="S3 bucket" />
165                     {isCollection && <Tab value={3} key="cli" label="wget / curl" />}
166                 </Tabs>
167
168                 <TabPanel index={1} value={activeTab}>
169                     <h2>Settings</h2>
170
171                     <DetailsAttribute
172                         label='Internet address'
173                         value={<a href={winDav.toString()} target="_blank" rel="noopener noreferrer">{winDav.toString()}</a>}
174                         copyValue={winDav.toString()} />
175
176                     <DetailsAttribute
177                         label='Username'
178                         value={props.data.username}
179                         copyValue={props.data.username} />
180
181                     <DetailsAttribute
182                         label='Password'
183                         value={props.data.token}
184                         copyValue={props.data.token} />
185
186                     <h3>Windows</h3>
187                     <ol>
188                         <li>Open File Explorer</li>
189                         <li>Click on "This PC", then go to Computer &rarr; Add a Network Location</li>
190                         <li>Click Next, then choose "Add a custom network location", then click Next</li>
191                         <li>Use the "internet address" and credentials listed under Settings, above</li>
192                     </ol>
193
194                     <h3>MacOS</h3>
195                     <ol>
196                         <li>Open Finder</li>
197                         <li>Click Go &rarr; Connect to server</li>
198                         <li>Use the "internet address" and credentials listed under Settings, above</li>
199                     </ol>
200                 </TabPanel>
201
202                 <TabPanel index={0} value={activeTab}>
203                     <DetailsAttribute
204                         label='Server'
205                         value={<a href={cyberDavStr} target="_blank" rel="noopener noreferrer">{cyberDavStr}</a>}
206                         copyValue={cyberDavStr} />
207
208                     <DetailsAttribute
209                         label='Username'
210                         value={props.data.username}
211                         copyValue={props.data.username} />
212
213                     <DetailsAttribute
214                         label='Password'
215                         value={props.data.token}
216                         copyValue={props.data.token} />
217
218                     <h3>Cyberduck/Mountain Duck</h3>
219
220                     <Button
221                         data-cy='download-button'
222                         className={props.classes.downloadButton}
223                         onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate({ ...props.data, cyberDavStr }))}
224                         variant='contained'
225                         color='primary'
226                         size='small'>
227                         <DownloadIcon />
228                         Download Cyber/Mountain Duck bookmark
229                     </Button>
230
231                     <h3>GNOME</h3>
232                     <ol>
233                         <li>Open Files</li>
234                         <li>Select +Other Locations</li>
235                         <li>Connect to Server &rarr; Enter server address</li>
236                     </ol>
237
238                 </TabPanel>
239
240                 <TabPanel index={2} value={activeTab}>
241                     <DetailsAttribute
242                         label='Endpoint'
243                         value={s3endpoint.host}
244                         copyValue={s3endpoint.host} />
245
246                     <DetailsAttribute
247                         label='Bucket'
248                         value={props.data.uuid}
249                         copyValue={props.data.uuid} />
250
251                     <DetailsAttribute
252                         label='Access Key'
253                         value={tokenUuid}
254                         copyValue={tokenUuid} />
255
256                     <DetailsAttribute
257                         label='Secret Key'
258                         value={tokenSecret}
259                         copyValue={tokenSecret} />
260
261                 </TabPanel>
262
263                 <TabPanel index={3} value={activeTab}>
264
265                     <DetailsAttribute
266                         label='Wget command'
267                         copyValue={wgetCommand}
268                         classValue={props.classes.detailsAttrValWithCode}>
269                         <DefaultCodeSnippet
270                             lines={[wgetCommand]} />
271                     </DetailsAttribute>
272
273                     <DetailsAttribute
274                         label='Curl command'
275                         copyValue={curlCommand}
276                         classValue={props.classes.detailsAttrValWithCode}>
277                         <DefaultCodeSnippet
278                             lines={[curlCommand]} />
279                     </DetailsAttribute>
280
281                     <p>
282                       Note: This curl command downloads single files.
283                       Append the desired filename to the end of the URL.
284                     </p>
285
286                 </TabPanel>
287
288             </div>
289             <DialogActions>
290                 <Button
291                     variant='text'
292                     color='primary'
293                     onClick={props.closeDialog}>
294                     Close
295                 </Button>
296             </DialogActions>
297
298         </Dialog >;
299     }
300 );