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}` : ''}`;
17 export const replaceCollectionId = (href: string, rep: string): string => {
18 const [prefix, suffix] = href.split('/c=');
19 const [uuid, ...rest] = suffix.split('/');
22 return `${prefix}/c=${rep}/${rest.join('/')}`;
24 return `${prefix}/${rest.join('/')}`;
29 * @returns A shareable token-free WB2 url that redirects to keep-web after login
31 export const getCollectionItemClipboardUrl = (href: string, keepWebServiceUrl: string, keepWebInlineServiceUrl: string): string => {
32 const url = sanitizeToken(href, false);
33 return getInlineFileUrl(url, keepWebServiceUrl, keepWebInlineServiceUrl);
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)
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;
56 inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, '');
61 export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => {
62 let inlineUrl = keepWebInlineSvcUrl !== ""
63 ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl)
65 return inlineUrl.indexOf('*.') > -1;