19079: Don't use collection specific url processing for search result clipboard actions
[arvados-workbench2.git] / src / views-components / context-menu / actions / helpers.ts
index 578af2053f74d543cf8d00fec26b87d1e4c8c8c6..9140e457afef89affcb573f0c84303640d4014e0 100644 (file)
@@ -2,7 +2,8 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { REDIRECT_TO_APPLY_TO_PATH } from "~/common/redirect-to";
+import { REDIRECT_TO_DOWNLOAD_KEY, REDIRECT_TO_PREVIEW_KEY } from "common/redirect-to";
+import { extractUuidKind, ResourceKind } from "models/resource";
 
 export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => {
     const [prefix, suffix] = href.split('/t=');
@@ -13,9 +14,45 @@ export const sanitizeToken = (href: string, tokenAsQueryParam = true): string =>
     return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`;
 };
 
-export const getClipboardUrl = (href: string, shouldSanitizeToken = true): string => {
+/**
+ * @returns A shareable token-free WB2 url that redirects to keep-web after login
+ */
+export const getCollectionItemClipboardUrl = (href: string, shouldSanitizeToken = true, inline = false): string => {
     const { origin } = window.location;
     const url = shouldSanitizeToken ? sanitizeToken(href, false) : href;
+    const redirectKey = inline ? REDIRECT_TO_PREVIEW_KEY : REDIRECT_TO_DOWNLOAD_KEY;
 
-    return `${origin}${!shouldSanitizeToken ? `?${REDIRECT_TO_APPLY_TO_PATH}=true&` : `?`}redirectTo=${url}`;
+    return shouldSanitizeToken ? `${origin}?${redirectKey}=${url}` : `${origin}${url}`;
 };
+
+export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => {
+    const collMatch = url.match(/\/c=([a-z0-9-+]+)\//);
+    if (collMatch === null) { return ''; }
+    if (extractUuidKind(collMatch[1]) !== ResourceKind.COLLECTION) { return ''; }
+    const collId = collMatch[1].replace('+', '-');
+    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('*.', `${collId}.`);
+        uuidOnHostname = true;
+    } else if (inlineUrl.indexOf('*--') > -1) {
+        inlineUrl = inlineUrl.replace('*--', `${collId}--`);
+        uuidOnHostname = true;
+    }
+    if (uuidOnHostname) {
+        inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
+    }
+    return inlineUrl;
+};
+
+export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => {
+  let inlineUrl = keepWebInlineSvcUrl !== ""
+      ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
+      : url;
+  return inlineUrl.indexOf('*.') > -1;
+}