17782: Fixes almost all tests (4 left) mostly by fixing namespace-type imports.
[arvados-workbench2.git] / src / views-components / webdav-s3-dialog / webdav-s3-dialog.tsx
index 6e5c7c0fac160e110e1caedeb9bbfae57df55d33..181e6e28a27a8ba60c84bf5316ae32702c551bb4 100644 (file)
@@ -2,14 +2,14 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from "react";
+import React from "react";
 import { Dialog, DialogActions, Button, StyleRulesCallback, WithStyles, withStyles, CardHeader, Tab, Tabs } from '@material-ui/core';
-import { withDialog } from "~/store/dialog/with-dialog";
-import { COLLECTION_WEBDAV_S3_DIALOG_NAME, WebDavS3InfoDialogData } from '~/store/collections/collection-info-actions';
-import { WithDialogProps } from '~/store/dialog/with-dialog';
+import { withDialog } from "store/dialog/with-dialog";
+import { COLLECTION_WEBDAV_S3_DIALOG_NAME, WebDavS3InfoDialogData } from 'store/collections/collection-info-actions';
+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";
+import { DetailsAttribute } from "components/details-attribute/details-attribute";
+import { DownloadIcon } from "components/icon/icon";
 
 export type CssRules = 'details' | 'downloadButton';
 
@@ -44,43 +44,57 @@ function TabPanel(props: TabPanelData) {
     );
 }
 
+const isValidIpAddress = (ipAddress: string): Boolean => {
+    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)) {
+        return true;
+    }
+
+    return false;
+};
+
 const mountainduckTemplate = ({
-    uuid, 
+    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');
+    cyberDavStr,
+    collectionsUrl
+}: any) => {
+
+    return `<?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://', ``).replace('*', uuid).split(':')[0]}</string>
+            <key>Port</key>
+            <string>${(cyberDavStr.split(':')[2] || '443').split('/')[0]}</string>
+            <key>Username</key>
+            <string>${username}</string>${isValidIpAddress(collectionsUrl.replace('https://', ``).split(':')[0])?
+            `
+            <key>Path</key>
+            <string>/c=${uuid}</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);
 };
 
@@ -193,12 +207,12 @@ export const WebDavS3InfoDialog = compose(
                     <Button
                         data-cy='download-button'
                         className={props.classes.downloadButton}
-                        onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate(props.data))}
+                        onClick={() => downloadMountainduckFileHandler(`${props.data.collectionName || props.data.uuid}.duck`, mountainduckTemplate({ ...props.data, cyberDavStr }))}
                         variant='contained'
                         color='primary'
                         size='small'>
                         <DownloadIcon />
-                        Download config
+                        Download Cyber/Mountain Duck bookmark
                     </Button>
 
                     <h3>Gnome</h3>