Merge branch '21900-groups-panel-toolbar'
[arvados.git] / services / workbench2 / 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 { REDIRECT_TO_DOWNLOAD_KEY, REDIRECT_TO_PREVIEW_KEY } from "common/redirect-to";
6 import { extractUuidKind, ResourceKind } from "models/resource";
7
8 export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => {
9     const [prefix, suffix] = href.split('/t=');
10     const [token1, token2, token3, ...rest] = suffix.split('/');
11     const token = `${token1}/${token2}/${token3}`;
12     const sep = href.indexOf("?") > -1 ? "&" : "?";
13
14     return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`;
15 };
16
17 export const replaceCollectionId = (href: string, rep: string): string => {
18     const [prefix, suffix] = href.split('/c=');
19     const [uuid, ...rest] = suffix.split('/');
20
21     if (rep) {
22         return `${prefix}/c=${rep}/${rest.join('/')}`;
23     } else {
24         return `${prefix}/${rest.join('/')}`;
25     }
26 };
27
28 /**
29  * @returns A shareable token-free WB2 url that redirects to keep-web after login
30  */
31 export const getCollectionItemClipboardUrl = (href: string, keepWebServiceUrl: string, keepWebInlineServiceUrl: string): string => {
32     const url = sanitizeToken(href, false);
33     return getInlineFileUrl(url, keepWebServiceUrl, keepWebInlineServiceUrl);
34 };
35
36 export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => {
37     const collMatch = url.match(/\/c=([a-z0-9-+]+)\//);
38     if (collMatch === null) { return ''; }
39     if (extractUuidKind(collMatch[1]) !== ResourceKind.COLLECTION) { return ''; }
40     const collId = collMatch[1].replace('+', '-');
41     let inlineUrl = keepWebInlineSvcUrl !== ""
42         ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
43         : url;
44     let uuidOnHostname = false;
45     // Inline URLs as 'https://*.collections.example.com' or
46     // 'https://*--collections.example.com' should get the uuid on their hostnames
47     // See: https://doc.arvados.org/v2.1/api/keep-web-urls.html
48     if (inlineUrl.indexOf('*.') > -1) {
49         inlineUrl = inlineUrl.replace('*.', `${collId}.`);
50         uuidOnHostname = true;
51     } else if (inlineUrl.indexOf('*--') > -1) {
52         inlineUrl = inlineUrl.replace('*--', `${collId}--`);
53         uuidOnHostname = true;
54     }
55     if (uuidOnHostname) {
56         inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
57     }
58     return inlineUrl;
59 };
60
61 export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => {
62   let inlineUrl = keepWebInlineSvcUrl !== ""
63       ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
64       : url;
65   return inlineUrl.indexOf('*.') > -1;
66 }