17415: Created mountainduck bookmark files generator
[arvados-workbench2.git] / src / views-components / webdav-s3-dialog / webdav-s3-dialog.tsx
index 217f9a2a27985e356b94adb890ccffe8f6dd2a4d..6e5c7c0fac160e110e1caedeb9bbfae57df55d33 100644 (file)
@@ -9,13 +9,17 @@ import { COLLECTION_WEBDAV_S3_DIALOG_NAME, WebDavS3InfoDialogData } from '~/stor
 import { WithDialogProps } from '~/store/dialog/with-dialog';
 import { compose } from 'redux';
 import { DetailsAttribute } from "~/components/details-attribute/details-attribute";
+import { DownloadIcon } from "~/components/icon/icon";
 
-type CssRules = 'details';
+export type CssRules = 'details' | 'downloadButton';
 
 const styles: StyleRulesCallback<CssRules> = theme => ({
     details: {
         marginLeft: theme.spacing.unit * 3,
         marginRight: theme.spacing.unit * 3,
+    },
+    downloadButton: {
+        marginTop: theme.spacing.unit * 2,
     }
 });
 
@@ -40,6 +44,46 @@ function TabPanel(props: TabPanelData) {
     );
 }
 
+const mountainduckTemplate = ({
+    uuid, 
+    username,
+    collectionsUrl,
+}: any) => `<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+   <dict>
+      <key>Protocol</key>
+      <string>davs</string>
+      <key>Provider</key>
+      <string>iterate GmbH</string>
+      <key>UUID</key>
+      <string>${uuid}</string>
+      <key>Hostname</key>
+      <string>${collectionsUrl.replace('https://', `davs://${username}@`).replace('*', uuid)}</string>
+      <key>Port</key>
+      <string>443</string>
+      <key>Username</key>
+      <string>${username}</string>
+      <key>Labels</key>
+      <array>
+      </array>
+   </dict>
+</plist>
+`.split(/\r?\n/).join('\n');
+
+const downloadMountainduckFileHandler = (filename: string, text: string) => {
+    const element = document.createElement('a');
+    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
+    element.setAttribute('download', filename);
+  
+    element.style.display = 'none';
+    document.body.appendChild(element);
+  
+    element.click();
+  
+    document.body.removeChild(element);
+};
+
 export const WebDavS3InfoDialog = compose(
     withDialog(COLLECTION_WEBDAV_S3_DIALOG_NAME),
     withStyles(styles),
@@ -57,18 +101,19 @@ export const WebDavS3InfoDialog = compose(
         } else {
             winDav = new URL(props.data.downloadUrl);
             cyberDav = new URL(props.data.downloadUrl);
-            winDav.pathname = `/by_id/${props.data.uuid}/`;
-            cyberDav.pathname = `/by_id/${props.data.uuid}/`;
+            winDav.pathname = `/by_id/${props.data.uuid}`;
+            cyberDav.pathname = `/by_id/${props.data.uuid}`;
         }
 
-        cyberDav.protocol = { "http:": "dav:", "https:": "davs:" }[cyberDav.protocol];
+        cyberDav.username = props.data.username;
+        const cyberDavStr = "dav" + cyberDav.toString().slice(4);
 
         const s3endpoint = new URL(props.data.collectionsUrl.replace(/\/\*(--[^.]+)?\./, "/"));
 
         const sp = props.data.token.split("/");
         let tokenUuid;
         let tokenSecret;
-        if (sp.length === 3 && sp[0] === "v2" && props.data.homeCluster === props.data.localCluster) {
+        if (sp.length === 3 && sp[0] === "v2" && sp[1].slice(0, 5) === props.data.localCluster) {
             tokenUuid = sp[1];
             tokenSecret = sp[2];
         } else {
@@ -97,12 +142,12 @@ export const WebDavS3InfoDialog = compose(
                     <Tab value={2} key="s3" label="S3 bucket" />
                 </Tabs>
 
-                <TabPanel index={0} value={activeTab}>
+                <TabPanel index={1} value={activeTab}>
                     <h2>Settings</h2>
 
                     <DetailsAttribute
                         label='Internet address'
-                        value={<a href={winDav.toString()}>{winDav.toString()}</a>}
+                        value={<a href={winDav.toString()} target="_blank">{winDav.toString()}</a>}
                         copyValue={winDav.toString()} />
 
                     <DetailsAttribute
@@ -129,11 +174,11 @@ export const WebDavS3InfoDialog = compose(
                     </ol>
                 </TabPanel>
 
-                <TabPanel index={1} value={activeTab}>
+                <TabPanel index={0} value={activeTab}>
                     <DetailsAttribute
                         label='Server'
-                        value={<a href={cyberDav.toString()}>{cyberDav.toString()}</a>}
-                        copyValue={cyberDav.toString()} />
+                        value={<a href={cyberDavStr} target="_blank">{cyberDavStr}</a>}
+                        copyValue={cyberDavStr} />
 
                     <DetailsAttribute
                         label='Username'
@@ -145,6 +190,17 @@ export const WebDavS3InfoDialog = compose(
                         value={props.data.token}
                         copyValue={props.data.token} />
 
+                    <Button
+                        data-cy='download-button'
+                        className={props.classes.downloadButton}
+                        onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate(props.data))}
+                        variant='contained'
+                        color='primary'
+                        size='small'>
+                        <DownloadIcon />
+                        Download config
+                    </Button>
+
                     <h3>Gnome</h3>
                     <ol>
                         <li>Open Files</li>