21700: Install Bundler system-wide in Rails postinst
[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 /**
18  * @returns A shareable token-free WB2 url that redirects to keep-web after login
19  */
20 export const getCollectionItemClipboardUrl = (href: string, shouldSanitizeToken = true, inline = false): string => {
21     const { origin } = window.location;
22     const url = shouldSanitizeToken ? sanitizeToken(href, false) : href;
23     const redirectKey = inline ? REDIRECT_TO_PREVIEW_KEY : REDIRECT_TO_DOWNLOAD_KEY;
24
25     return shouldSanitizeToken ? `${origin}?${redirectKey}=${url}` : `${origin}${url}`;
26 };
27
28 export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => {
29     const collMatch = url.match(/\/c=([a-z0-9-+]+)\//);
30     if (collMatch === null) { return ''; }
31     if (extractUuidKind(collMatch[1]) !== ResourceKind.COLLECTION) { return ''; }
32     const collId = collMatch[1].replace('+', '-');
33     let inlineUrl = keepWebInlineSvcUrl !== ""
34         ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
35         : url;
36     let uuidOnHostname = false;
37     // Inline URLs as 'https://*.collections.example.com' or
38     // 'https://*--collections.example.com' should get the uuid on their hostnames
39     // See: https://doc.arvados.org/v2.1/api/keep-web-urls.html
40     if (inlineUrl.indexOf('*.') > -1) {
41         inlineUrl = inlineUrl.replace('*.', `${collId}.`);
42         uuidOnHostname = true;
43     } else if (inlineUrl.indexOf('*--') > -1) {
44         inlineUrl = inlineUrl.replace('*--', `${collId}--`);
45         uuidOnHostname = true;
46     }
47     if (uuidOnHostname) {
48         inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
49     }
50     return inlineUrl;
51 };
52
53 export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => {
54   let inlineUrl = keepWebInlineSvcUrl !== ""
55       ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
56       : url;
57   return inlineUrl.indexOf('*.') > -1;
58 }