X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f62ba0aeff590ef1c57669d4f46e43b5ee169522..5a2f4597393f94b9fa7984f4445d4b599c139f27:/src/views-components/context-menu/actions/helpers.ts diff --git a/src/views-components/context-menu/actions/helpers.ts b/src/views-components/context-menu/actions/helpers.ts index 0ad2dc3c..f196074d 100644 --- a/src/views-components/context-menu/actions/helpers.ts +++ b/src/views-components/context-menu/actions/helpers.ts @@ -2,6 +2,9 @@ // // SPDX-License-Identifier: AGPL-3.0 +import { REDIRECT_TO_DOWNLOAD_KEY, REDIRECT_TO_PREVIEW_KEY } from "common/redirect-to"; +import { extractUuidKind, ResourceKind } from "models/resource"; + export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => { const [prefix, suffix] = href.split('/t='); const [token1, token2, token3, ...rest] = suffix.split('/'); @@ -11,9 +14,42 @@ export const sanitizeToken = (href: string, tokenAsQueryParam = true): string => return `${[prefix, ...rest].join('/')}${tokenAsQueryParam ? `${sep}api_token=${token}` : ''}`; }; -export const getClipboardUrl = (href: string, shouldSanitizeToken = true): string => { +export const getClipboardUrl = (href: string, shouldSanitizeToken = true, inline = false): string => { const { origin } = window.location; const url = shouldSanitizeToken ? sanitizeToken(href, false) : href; + const redirectKey = inline ? REDIRECT_TO_PREVIEW_KEY : REDIRECT_TO_DOWNLOAD_KEY; + + return shouldSanitizeToken ? `${origin}?${redirectKey}=${url}` : `${origin}${url}`; +}; - return shouldSanitizeToken ? `${origin}?redirectTo=${url}` : `${origin}${url}`; +export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => { + const collMatch = url.match(/\/c=([a-z0-9-+]+)\//); + if (collMatch === null) { return ''; } + if (extractUuidKind(collMatch[1]) !== ResourceKind.COLLECTION) { return ''; } + const collId = collMatch[1].replace('+', '-'); + let inlineUrl = keepWebInlineSvcUrl !== "" + ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl) + : url; + let uuidOnHostname = false; + // Inline URLs as 'https://*.collections.example.com' or + // 'https://*--collections.example.com' should get the uuid on their hostnames + // See: https://doc.arvados.org/v2.1/api/keep-web-urls.html + if (inlineUrl.indexOf('*.') > -1) { + inlineUrl = inlineUrl.replace('*.', `${collId}.`); + uuidOnHostname = true; + } else if (inlineUrl.indexOf('*--') > -1) { + inlineUrl = inlineUrl.replace('*--', `${collId}--`); + uuidOnHostname = true; + } + if (uuidOnHostname) { + inlineUrl = inlineUrl.replace(`/c=${collMatch[1]}`, ''); + } + return inlineUrl; }; + +export const isInlineFileUrlSafe = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): boolean => { + let inlineUrl = keepWebInlineSvcUrl !== "" + ? url.replace(keepWebSvcUrl, keepWebInlineSvcUrl) + : url; + return inlineUrl.indexOf('*.') > -1; +}