handle new format of shareURL

This commit is contained in:
Abhinav 2022-01-24 14:51:31 +05:30
parent f89be2fffb
commit 17fd62be4d
3 changed files with 30 additions and 23 deletions

View file

@ -43,10 +43,18 @@ function CollectionShare(props: Props) {
const [sharableLinkError, setSharableLinkError] = useState(null);
const [publicShareUrl, setPublicShareUrl] = useState<string>(null);
useEffect(
() => setPublicShareUrl(props.collection?.publicURLs?.[0]?.url || null),
[props.collection]
);
useEffect(() => {
const main = async () => {
if (props.collection?.publicURLs?.[0]?.url) {
const t = await transformShareURLForHost(
props.collection?.publicURLs?.[0]?.url,
props.collection.key
);
setPublicShareUrl(t);
}
};
main();
}, [props.collection]);
const collectionShare = async (
{ email }: formValues,
@ -250,10 +258,7 @@ function CollectionShare(props: Props) {
<CodeBlock
key={publicShareUrl}
height={'160px'}
code={transformShareURLForHost(
publicShareUrl,
props.collection.key
)}
code={publicShareUrl}
/>
</div>
)}

View file

@ -26,6 +26,7 @@ import Container from 'components/Container';
import constants from 'utils/strings/constants';
import EnteSpinner from 'components/EnteSpinner';
import LoadingBar from 'react-top-loading-bar';
import CryptoWorker from 'utils/crypto';
export default function PublicCollectionGallery() {
const token = useRef<string>(null);
@ -46,22 +47,22 @@ export default function PublicCollectionGallery() {
const closeMessageDialog = () => setMessageDialogView(false);
const startLoading = () => loadingBar.current?.continuousStart();
const finishLoading = () => loadingBar.current.complete();
const finishLoading = () => loadingBar.current?.complete();
useEffect(() => {
const main = async () => {
const worker = await new CryptoWorker();
url.current = window.location.href;
const urlParams = new URLSearchParams(window.location.search);
const eToken = urlParams.get('accessToken');
const eCollectionKey = decodeURIComponent(
urlParams.get('collectionKey')
);
if (!eToken || !eCollectionKey) {
const urlS = new URL(url.current);
const eToken = urlS.searchParams.get('t');
const eCollectionKey = urlS.hash.slice(1);
const decodedCollectionKey = await worker.fromHex(eCollectionKey);
if (!eToken || !decodedCollectionKey) {
setLoading(false);
return;
}
token.current = eToken;
collectionKey.current = eCollectionKey;
collectionKey.current = decodedCollectionKey;
url.current = window.location.href;
const localCollection = await getLocalPublicCollection(
eCollectionKey

View file

@ -16,6 +16,7 @@ import { logError } from 'utils/sentry';
import constants from 'utils/strings/constants';
import { Collection } from 'types/collection';
import { CollectionType } from 'constants/collection';
import CryptoWorker from 'utils/crypto';
export enum COLLECTION_OPS_TYPE {
ADD,
@ -108,19 +109,19 @@ export async function downloadCollection(
}
}
export function transformShareURLForHost(url: string, collectionKey: string) {
export async function transformShareURLForHost(
url: string,
collectionKey: string
) {
const worker = await new CryptoWorker();
if (!url) {
return null;
}
const host = window.location.host;
const sharableURL = new URL(url);
sharableURL.host = host;
const accessToken = sharableURL.pathname.slice(
-sharableURL.pathname.lastIndexOf('/') + 1
);
sharableURL.pathname = '/shared-album/';
sharableURL.searchParams.append('accessToken', accessToken);
sharableURL.searchParams.append('collectionKey', collectionKey);
sharableURL.pathname = '/shared-album';
sharableURL.hash = await worker.toHex(collectionKey);
sharableURL.protocol = 'http';
return sharableURL.href;
}