17782: Fixes absolute import paths from '~/somedir/...' to 'somedir/...'
[arvados-workbench2.git] / src / views-components / context-menu / actions / helpers.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { extractUuidKind, ResourceKind } from "models/resource";
6
7 export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => {
8     const [prefix, suffix] = href.split('/t=');
9     const [token1, token2, token3, ...rest] = suffix.split('/');
10     const token = `${token1}/${token2}/${token3}`;
11     const sep = href.indexOf("?") > -1 ? "&" : "?";
12
13     return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`;
14 };
15
16 export const getClipboardUrl = (href: string, shouldSanitizeToken = true): string => {
17     const { origin } = window.location;
18     const url = shouldSanitizeToken ? sanitizeToken(href, false) : href;
19
20     return shouldSanitizeToken ? `${origin}?redirectTo=${url}` : `${origin}${url}`;
21 };
22
23 export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => {
24     const collMatch = url.match(/\/c=([a-z0-9-+]+)\//);
25     if (collMatch === null) { return ''; }
26     if (extractUuidKind(collMatch[1]) !== ResourceKind.COLLECTION) { return ''; }
27     const collId = collMatch[1].replace('+', '-');
28     let inlineUrl = keepWebInlineSvcUrl !== ""
29         ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
30         : url;
31     let uuidOnHostname = false;
32     // Inline URLs as 'https://*.collections.example.com' or
33     // 'https://*--collections.example.com' should get the uuid on their hostnames
34     // See: https://doc.arvados.org/v2.1/api/keep-web-urls.html
35     if (inlineUrl.indexOf('*.') > -1) {
36         inlineUrl = inlineUrl.replace('*.', `${collId}.`);
37         uuidOnHostname = true;
38     } else if (inlineUrl.indexOf('*--') > -1) {
39         inlineUrl = inlineUrl.replace('*--', `${collId}--`);
40         uuidOnHostname = true;
41     }
42     if (uuidOnHostname) {
43         inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
44     }
45     return inlineUrl;
46 };