17109: Handles keep-web's inline wildcard urls correctly.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Tue, 12 Jan 2021 21:33:50 +0000 (18:33 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Tue, 12 Jan 2021 21:33:50 +0000 (18:33 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/components/file-tree/file-thumbnail.tsx
src/views-components/context-menu/actions/collection-file-viewer-action.tsx
src/views-components/context-menu/actions/file-viewer-action.tsx
src/views-components/context-menu/actions/helpers.test.ts
src/views-components/context-menu/actions/helpers.ts

index 40631961a8a526143c7d7c6b3835945adb940284..a56dcf2f749a7d23dcaf467ccc248042d63bc9b7 100644 (file)
@@ -7,7 +7,9 @@ import isImage from 'is-image';
 import { withStyles, WithStyles } from '@material-ui/core';
 import { FileTreeData } from '~/components/file-tree/file-tree-data';
 import { CollectionFileType } from '~/models/collection-file';
-import { sanitizeToken } from "~/views-components/context-menu/actions/helpers";
+import { getInlineFileUrl, sanitizeToken } from "~/views-components/context-menu/actions/helpers";
+import { connect } from "react-redux";
+import { RootState } from "~/store/store";
 
 export interface FileThumbnailProps {
     file: FileTreeData;
@@ -28,10 +30,20 @@ const imageFileThumbnailStyle = withStyles<ImageFileThumbnailCssRules>(theme =>
     }
 }));
 
-const ImageFileThumbnail = imageFileThumbnailStyle(
-    ({ classes, file }: WithStyles<ImageFileThumbnailCssRules> & FileThumbnailProps) =>
+interface ImageFileThumbnailProps {
+    keepWebServiceUrl: string;
+    keepWebInlineServiceUrl: string;
+}
+
+const mapStateToProps = ({ auth }: RootState): ImageFileThumbnailProps => ({
+    keepWebServiceUrl: auth.config.keepWebServiceUrl,
+    keepWebInlineServiceUrl: auth.config.keepWebInlineServiceUrl,
+});
+
+const ImageFileThumbnail = connect(mapStateToProps)(imageFileThumbnailStyle(
+    ({ classes, file, keepWebServiceUrl, keepWebInlineServiceUrl }: WithStyles<ImageFileThumbnailCssRules> & FileThumbnailProps & ImageFileThumbnailProps) =>
         <img
             className={classes.thumbnail}
             alt={file.name}
-            src={sanitizeToken(file.url)} />
-);
+            src={sanitizeToken(getInlineFileUrl(file.url, keepWebServiceUrl, keepWebInlineServiceUrl))} />
+));
index dfc9d14acd7e5552e5daa23e9256b04ab819f764..aba355346bdbdf5fa9398a08d3faa53cd963818c 100644 (file)
@@ -7,6 +7,7 @@ import { RootState } from "../../../store/store";
 import { FileViewerAction } from '~/views-components/context-menu/actions/file-viewer-action';
 import { getNodeValue } from "~/models/tree";
 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
+import { getInlineFileUrl, sanitizeToken } from "./helpers";
 
 const mapStateToProps = (state: RootState) => {
     const { resource } = state.contextMenu;
@@ -16,8 +17,12 @@ const mapStateToProps = (state: RootState) => {
         resource.menuKind === ContextMenuKind.READONLY_COLLECTION_FILES_ITEM)) {
         const file = getNodeValue(resource.uuid)(state.collectionPanelFiles);
         if (file) {
+            const fileUrl = sanitizeToken(getInlineFileUrl(
+                file.url,
+                state.auth.config.keepWebServiceUrl,
+                state.auth.config.keepWebInlineServiceUrl), true);
             return {
-                href: file.url.replace(state.auth.config.keepWebServiceUrl, state.auth.config.keepWebInlineServiceUrl),
+                href: fileUrl,
                 kind: 'file',
                 currentCollectionUuid
             };
index a631424e67bde5f46dbca5e370c3e6bdaa70eb29..e31bb10d441b5b135a6bf7c79fde8de1726768a8 100644 (file)
@@ -3,22 +3,14 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from "react";
-import { connect } from 'react-redux';
 import { ListItemIcon, ListItemText, ListItem } from "@material-ui/core";
 import { OpenIcon } from "~/components/icon/icon";
-import { sanitizeToken } from "./helpers";
-import { RootState } from "~/store/store";
 
 export const FileViewerAction = (props: any) => {
-    const {
-        keepWebServiceUrl,
-        keepWebInlineServiceUrl,
-    } = props;
-
     return props.href
         ? <a
             style={{ textDecoration: 'none' }}
-            href={sanitizeToken(props.href.replace(keepWebServiceUrl, keepWebInlineServiceUrl), true)}
+            href={props.href}
             target="_blank"
             onClick={props.onClick}>
             <ListItem button>
@@ -32,11 +24,3 @@ export const FileViewerAction = (props: any) => {
         </a>
         : null;
 };
-
-const mapStateToProps = ({ auth }: RootState): any => ({
-    keepWebServiceUrl: auth.config.keepWebServiceUrl,
-    keepWebInlineServiceUrl: auth.config.keepWebInlineServiceUrl,
-});
-
-
-export default connect(mapStateToProps, null)(FileViewerAction);
index 4234a8cc3a5ea594ef62f760a505dd5fab3255b8..c3a31691383764aaa5f33d47828bc72d4a961626 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { sanitizeToken, getClipboardUrl } from "./helpers";
+import { sanitizeToken, getClipboardUrl, getInlineFileUrl } from "./helpers";
 
 describe('helpers', () => {
     // given
@@ -27,4 +27,39 @@ describe('helpers', () => {
             expect(result).toBe('http://localhost?redirectTo=https://example.com/c=zzzzz/LIMS/1.html');
         });
     });
+
+    describe('getInlineFileUrl', () => {
+        it('should add the collection\'s uuid to the hostname', () => {
+            // when
+            const webDavUrlA = 'https://*.collections.example.com/';
+            const webDavUrlB = 'https://*--collections.example.com/';
+            const webDavDownloadUrl = 'https://example.com/';
+
+            // then
+            expect(getInlineFileUrl(url, webDavDownloadUrl, webDavUrlA))
+                .toBe('https://zzzzz.collections.example.com/t=v2/a/b/LIMS/1.html');
+            expect(getInlineFileUrl(url, webDavDownloadUrl, webDavUrlB))
+                .toBe('https://zzzzz--collections.example.com/t=v2/a/b/LIMS/1.html');
+        });
+
+        it('should keep the url the same when no inline url available', () => {
+            // when
+            const webDavUrl = '';
+            const webDavDownloadUrl = 'https://example.com/';
+            const result = getInlineFileUrl(url, webDavDownloadUrl, webDavUrl);
+
+            // then
+            expect(result).toBe('https://example.com/c=zzzzz/t=v2/a/b/LIMS/1.html');
+        });
+
+        it('should replace the url when available', () => {
+            // when
+            const webDavUrl = 'https://download.example.com/';
+            const webDavDownloadUrl = 'https://example.com/';
+            const result = getInlineFileUrl(url, webDavDownloadUrl, webDavUrl);
+
+            // then
+            expect(result).toBe('https://download.example.com/c=zzzzz/t=v2/a/b/LIMS/1.html');
+        });
+    });
 });
\ No newline at end of file
index 0ad2dc3c56c2c2ecd25a2f397467f377e2928bef..ce5dd8b1b2030f1e706f609ef381960760e184cf 100644 (file)
@@ -17,3 +17,27 @@ export const getClipboardUrl = (href: string, shouldSanitizeToken = true): strin
 
     return shouldSanitizeToken ? `${origin}?redirectTo=${url}` : `${origin}${url}`;
 };
+
+export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => {
+    const collUuidMatch = url.match(/\/c=([a-z0-9-]+)\//);
+    if (collUuidMatch === null) { return ''; }
+    const collUuid = collUuidMatch[1];
+    let inlineUrl = keepWebInlineSvcUrl !== ""
+        ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
+        : url;
+    let uuidOnHostname = false;
+    // Inline URLs as 'https://*.collections.example.com' or
+    // 'https://*--collections.example.com' should get the uuid on their hostnames
+    // See: https://doc.arvados.org/v2.1/api/keep-web-urls.html
+    if (inlineUrl.indexOf('*.') > -1) {
+        inlineUrl = inlineUrl.replace('*.', `${collUuid}.`);
+        uuidOnHostname = true;
+    } else if (inlineUrl.indexOf('*--') > -1) {
+        inlineUrl = inlineUrl.replace('*--', `${collUuid}--`);
+        uuidOnHostname = true;
+    }
+    if (uuidOnHostname) {
+        inlineUrl = inlineUrl.replace(`/c=${collUuid}`, '');
+    }
+    return inlineUrl;
+};
\ No newline at end of file