X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/9525ed95bef2a8de63b48a0682c342465d29bae9..98d3f7708860701b02055643da4ba9412720bf4b:/src/common/redirect-to.ts diff --git a/src/common/redirect-to.ts b/src/common/redirect-to.ts index 86fac71c..73c94843 100644 --- a/src/common/redirect-to.ts +++ b/src/common/redirect-to.ts @@ -2,28 +2,63 @@ // // 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'; +export const REDIRECT_TO_KEY = 'redirectTo'; + +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; + case href.indexOf(`${REDIRECT_TO_KEY}=`) > -1: + return REDIRECT_TO_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 = () => { - if (window.location.href.indexOf(REDIRECT_TO_KEY) > -1) { - const { location: { href }, sessionStorage } = window; - const redirectUrl = href.split(`${REDIRECT_TO_KEY}=`)[1]; - - if (sessionStorage) { - sessionStorage.setItem(REDIRECT_TO_KEY, redirectUrl); - } + const { location: { href }, localStorage } = window; + const redirectKey = getRedirectKeyFromUrl(href); + + // Change old redirectTo -> redirectToPreview when storing redirect + const redirectStoreKey = redirectKey === REDIRECT_TO_KEY ? REDIRECT_TO_PREVIEW_KEY : redirectKey; + + if (localStorage && redirectKey && redirectStoreKey) { + localStorage.setItem(redirectStoreKey, href.split(`${redirectKey}=`)[1]); } }; -export const handleRedirects = (config: Config) => { - const { sessionStorage } = window; - const { keepWebServiceUrl } = config; +export const handleRedirects = (token: string, config: Config) => { + const { localStorage } = window; + const { keepWebServiceUrl, keepWebInlineServiceUrl } = config; + + if (localStorage) { + const redirectKey = getRedirectKeyFromStorage(localStorage); + const redirectPath = redirectKey ? localStorage.getItem(redirectKey) : ''; + redirectKey && localStorage.removeItem(redirectKey); - if (sessionStorage && sessionStorage.getItem(REDIRECT_TO_KEY)) { - const redirectUrl = sessionStorage.getItem(REDIRECT_TO_KEY); - sessionStorage.removeItem(REDIRECT_TO_KEY); - window.location.href = `${keepWebServiceUrl}${redirectUrl}`; + 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; + } } -}; \ No newline at end of file +};