From 52b5bb0ea3c28cd2095837fea61509a70eb792fb Mon Sep 17 00:00:00 2001 From: Stephen Smith Date: Tue, 21 Jun 2022 09:58:17 -0400 Subject: [PATCH] 19153: Add support for inline sharing links Arvados-DCO-1.1-Signed-off-by: Stephen Smith --- src/common/redirect-to.test.ts | 6 +- src/common/redirect-to.ts | 57 +++++++++++++------ .../actions/copy-to-clipboard-action.tsx | 4 +- .../context-menu/actions/helpers.ts | 6 +- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/common/redirect-to.test.ts b/src/common/redirect-to.test.ts index e25d7be9..f7f5cb38 100644 --- a/src/common/redirect-to.test.ts +++ b/src/common/redirect-to.test.ts @@ -36,7 +36,7 @@ describe('redirect-to', () => { delete window.location; window.location = { ...locationTemplate, - href: `${location.href}?redirectTo=${redirectTo}`, + href: `${location.href}?redirectToPreview=${redirectTo}`, } as any; Object.defineProperty(window, 'localStorage', { value: { @@ -51,7 +51,7 @@ describe('redirect-to', () => { storeRedirects(); // then - expect(window.localStorage.setItem).toHaveBeenCalledWith('redirectTo', redirectTo); + expect(window.localStorage.setItem).toHaveBeenCalledWith('redirectToPreview', redirectTo); }); }); @@ -60,7 +60,7 @@ describe('redirect-to', () => { delete window.location; window.location = { ...locationTemplate, - href: `${location.href}?redirectTo=${redirectTo}`, + href: `${location.href}?redirectToPreview=${redirectTo}`, } as any;; Object.defineProperty(window, 'localStorage', { value: { diff --git a/src/common/redirect-to.ts b/src/common/redirect-to.ts index 77be742f..d8fecde4 100644 --- a/src/common/redirect-to.ts +++ b/src/common/redirect-to.ts @@ -2,34 +2,57 @@ // // SPDX-License-Identifier: AGPL-3.0 +import { getInlineFileUrl } from 'views-components/context-menu/actions/helpers'; import { Config } from './config'; -const REDIRECT_TO_KEY = 'redirectTo'; +export const REDIRECT_TO_DOWNLOAD_KEY = 'redirectToDownload'; +export const REDIRECT_TO_PREVIEW_KEY = 'redirectToPreview'; + +const getRedirectKeyFromUrl = (href: string): string | null => { + switch (true) { + case href.indexOf(REDIRECT_TO_DOWNLOAD_KEY) > -1: + return REDIRECT_TO_DOWNLOAD_KEY; + case href.indexOf(REDIRECT_TO_PREVIEW_KEY) > -1: + return REDIRECT_TO_PREVIEW_KEY; + default: + return null; + } +} + +const getRedirectKeyFromStorage = (localStorage: Storage): string | null => { + if (localStorage.getItem(REDIRECT_TO_DOWNLOAD_KEY)) { + return REDIRECT_TO_DOWNLOAD_KEY; + } else if (localStorage.getItem(REDIRECT_TO_PREVIEW_KEY)) { + return REDIRECT_TO_PREVIEW_KEY; + } + return null; +} export const storeRedirects = () => { - let redirectUrl; const { location: { href }, localStorage } = window; + const redirectKey = getRedirectKeyFromUrl(href); - if (href.indexOf(REDIRECT_TO_KEY) > -1) { - redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1]; - } - - if (localStorage && redirectUrl) { - localStorage.setItem(REDIRECT_TO_KEY, redirectUrl); + if (localStorage && redirectKey) { + localStorage.setItem(redirectKey, href.split(`${redirectKey}=`)[1]); } }; export const handleRedirects = (token: string, config: Config) => { const { localStorage } = window; - const { keepWebServiceUrl } = config; - - if (localStorage && localStorage.getItem(REDIRECT_TO_KEY)) { - const redirectUrl = localStorage.getItem(REDIRECT_TO_KEY); - localStorage.removeItem(REDIRECT_TO_KEY); - - if (redirectUrl) { - const sep = redirectUrl.indexOf("?") > -1 ? "&" : "?"; - window.location.href = `${keepWebServiceUrl}${redirectUrl}${sep}api_token=${token}`; + const { keepWebServiceUrl, keepWebInlineServiceUrl } = config; + + if (localStorage) { + const redirectKey = getRedirectKeyFromStorage(localStorage); + const redirectPath = redirectKey ? localStorage.getItem(redirectKey) : ''; + redirectKey && localStorage.removeItem(redirectKey); + + if (redirectKey && redirectPath) { + const sep = redirectPath.indexOf("?") > -1 ? "&" : "?"; + let redirectUrl = `${keepWebServiceUrl}${redirectPath}${sep}api_token=${token}`; + if (redirectKey === REDIRECT_TO_PREVIEW_KEY) { + redirectUrl = getInlineFileUrl(redirectUrl, keepWebServiceUrl, keepWebInlineServiceUrl); + } + window.location.href = redirectUrl; } } }; diff --git a/src/views-components/context-menu/actions/copy-to-clipboard-action.tsx b/src/views-components/context-menu/actions/copy-to-clipboard-action.tsx index a1dc5950..c3408740 100644 --- a/src/views-components/context-menu/actions/copy-to-clipboard-action.tsx +++ b/src/views-components/context-menu/actions/copy-to-clipboard-action.tsx @@ -11,7 +11,7 @@ import { getClipboardUrl } from "./helpers"; export const CopyToClipboardAction = (props: { href?: any, download?: any, onClick?: () => void, kind?: string, currentCollectionUuid?: string; }) => { const copyToClipboard = () => { if (props.href) { - const clipboardUrl = getClipboardUrl(props.href); + const clipboardUrl = getClipboardUrl(props.href, true, true); copy(clipboardUrl); } @@ -30,4 +30,4 @@ export const CopyToClipboardAction = (props: { href?: any, download?: any, onCli : null; -}; \ No newline at end of file +}; diff --git a/src/views-components/context-menu/actions/helpers.ts b/src/views-components/context-menu/actions/helpers.ts index 159b1c18..f196074d 100644 --- a/src/views-components/context-menu/actions/helpers.ts +++ b/src/views-components/context-menu/actions/helpers.ts @@ -2,6 +2,7 @@ // // 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 => { @@ -13,11 +14,12 @@ 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}?redirectTo=${url}` : `${origin}${url}`; + return shouldSanitizeToken ? `${origin}?${redirectKey}=${url}` : `${origin}${url}`; }; export const getInlineFileUrl = (url: string, keepWebSvcUrl: string, keepWebInlineSvcUrl: string): string => { -- 2.30.2