Merge branch '17595-Selecting-multiple-collections-as-inputs-to-a-workflow'
[arvados.git] / src / views-components / context-menu / actions / helpers.ts
index 3836860626a8363554ae1df135975a7a07c204ba..261f5a87ce0d73986a9fe5b164874abce1927c79 100644 (file)
@@ -2,16 +2,45 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-export const sanitizeToken = (href: string, tokenAsQueryParam: boolean = true): string => {
+import { extractUuidKind, ResourceKind } from "~/models/resource";
+
+export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => {
     const [prefix, suffix] = href.split('/t=');
-    const [token, ...rest] = suffix.split('/');
+    const [token1, token2, token3, ...rest] = suffix.split('/');
+    const token = `${token1}/${token2}/${token3}`;
+    const sep = href.indexOf("?") > -1 ? "&" : "?";
 
-    return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `?api_token=${token}` : ''}`;
+    return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`;
 };
 
-export const getClipboardUrl = (href: string): string => {
+export const getClipboardUrl = (href: string, shouldSanitizeToken = true): string => {
     const { origin } = window.location;
-    const url = sanitizeToken(href, false);
+    const url = shouldSanitizeToken ? sanitizeToken(href, false) : href;
+
+    return shouldSanitizeToken ? `${origin}?redirectTo=${url}` : `${origin}${url}`;
+};
 
-    return `${origin}?redirectTo=${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;
 };
\ No newline at end of file