18834: Unit tests added
[arvados-workbench2.git] / src / views-components / context-menu / actions / collection-file-viewer-action.tsx
index dfc9d14acd7e5552e5daa23e9256b04ab819f764..f736f0bf2705bf8d3481eb974cfdb93a3a1cc23f 100644 (file)
@@ -4,20 +4,32 @@
 
 import { connect } from "react-redux";
 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 { 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, isInlineFileUrlSafe } from "./helpers";
 
 const mapStateToProps = (state: RootState) => {
     const { resource } = state.contextMenu;
     const currentCollectionUuid = state.collectionPanel.item ? state.collectionPanel.item.uuid : '';
-    if (resource && (
-        resource.menuKind === ContextMenuKind.COLLECTION_FILES_ITEM ||
-        resource.menuKind === ContextMenuKind.READONLY_COLLECTION_FILES_ITEM)) {
+    if (resource && [
+        ContextMenuKind.COLLECTION_FILE_ITEM,
+        ContextMenuKind.READONLY_COLLECTION_FILE_ITEM,
+        ContextMenuKind.COLLECTION_DIRECTORY_ITEM,
+        ContextMenuKind.READONLY_COLLECTION_DIRECTORY_ITEM ].indexOf(resource.menuKind as ContextMenuKind) > -1) {
         const file = getNodeValue(resource.uuid)(state.collectionPanelFiles);
-        if (file) {
+        const shouldShowInlineUrl = isInlineFileUrlSafe(
+                                file ? file.url : "",
+                                state.auth.config.keepWebServiceUrl,
+                                state.auth.config.keepWebInlineServiceUrl
+                              ) || state.auth.config.clusterConfig.Collections.TrustAllContent;
+        if (file && shouldShowInlineUrl) {
+            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
             };