1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { REDIRECT_TO_DOWNLOAD_KEY, REDIRECT_TO_PREVIEW_KEY } from "common/redirect-to";
6 import { extractUuidKind, ResourceKind } from "models/resource";
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 ? "&" : "?";
14 return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`;
18 * @returns A shareable token-free WB2 url that redirects to keep-web after login
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;
25 return shouldSanitizeToken ? `${origin}?${redirectKey}=${url}` : `${origin}${url}`;
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)
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;
48 inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
53 export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => {
54 let inlineUrl = keepWebInlineSvcUrl !== ""
55 ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
57 return inlineUrl.indexOf('*.') > -1;