From 2b7e5f207489eb32a43d22e7c7cacb5e22174eb7 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 18 Nov 2021 10:12:01 +0530 Subject: [PATCH 01/14] one more place with same issue --- src/services/upload/thumbnailService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/upload/thumbnailService.ts b/src/services/upload/thumbnailService.ts index f4c8fd855..dd79222f3 100644 --- a/src/services/upload/thumbnailService.ts +++ b/src/services/upload/thumbnailService.ts @@ -44,7 +44,7 @@ export async function generateThumbnail( ); } catch (e) { logError(e, 'failed to generate thumbnail using ffmpeg', { - type: fileTypeInfo.exactType, + fileFormat: fileTypeInfo.exactType, }); canvas = await generateVideoThumbnail(file); } From 879ab66c081568bd5621f5f51db668d9eeb4389d Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 22 Nov 2021 21:54:14 +0530 Subject: [PATCH 02/14] save redirectURL in localStorage before auth redirect --- src/pages/credentials/index.tsx | 12 +++++++++--- src/pages/gallery/index.tsx | 2 ++ src/utils/storage/localStorage.ts | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 9b92b07ed..7a91bd9e8 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -1,7 +1,12 @@ import React, { useContext, useEffect, useState } from 'react'; import constants from 'utils/strings/constants'; -import { clearData, getData, LS_KEYS } from 'utils/storage/localStorage'; +import { + clearData, + getData, + LS_KEYS, + setData, +} from 'utils/storage/localStorage'; import { useRouter } from 'next/router'; import { KeyAttributes, PAGES } from 'types'; import { SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; @@ -75,8 +80,9 @@ export default function Credentials() { } await SaveKeyInSessionStore(SESSION_KEYS.ENCRYPTION_KEY, key); await decryptAndStoreToken(key); - - router.push(PAGES.GALLERY); + const redirectURL = getData(LS_KEYS.REDIRECT)?.url; + setData(LS_KEYS.REDIRECT, null); + router.push(redirectURL ? redirectURL : PAGES.GALLERY); } catch (e) { logError(e, 'user entered a wrong password'); setFieldError('passphrase', constants.INCORRECT_PASSPHRASE); diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 57ae25e43..29f9f5d24 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -97,6 +97,7 @@ import DeleteBtn from 'components/DeleteBtn'; import FixCreationTime, { FixCreationTimeAttributes, } from 'components/FixCreationTime'; +import { LS_KEYS, setData } from 'utils/storage/localStorage'; export const DeadCenter = styled.div` flex: 1; @@ -214,6 +215,7 @@ export default function Gallery() { useEffect(() => { const key = getKey(SESSION_KEYS.ENCRYPTION_KEY); if (!key) { + setData(LS_KEYS.REDIRECT, { url: router.asPath }); router.push(PAGES.ROOT); return; } diff --git a/src/utils/storage/localStorage.ts b/src/utils/storage/localStorage.ts index fddfe119e..2d23aff69 100644 --- a/src/utils/storage/localStorage.ts +++ b/src/utils/storage/localStorage.ts @@ -13,6 +13,7 @@ export enum LS_KEYS { EXPORT = 'export', AnonymizeUserID = 'anonymizedUserID', THUMBNAIL_FIX_STATE = 'thumbnailFixState', + REDIRECT = 'redirect', } export const setData = (key: LS_KEYS, value: object) => { From 4d5fae1591024caf709fe26cce722c6175ed10d2 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 22 Nov 2021 22:09:25 +0530 Subject: [PATCH 03/14] preserve other query param during collection url set --- src/pages/gallery/index.tsx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 29f9f5d24..26da721ac 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -269,19 +269,26 @@ export default function Gallery() { if (typeof activeCollection === 'undefined') { return; } - let collectionURL = ''; + let collectionQuery = ''; if (activeCollection !== ALL_SECTION) { - collectionURL += '?collection='; if (activeCollection === ARCHIVE_SECTION) { - collectionURL += constants.ARCHIVE; + collectionQuery += constants.ARCHIVE; } else if (activeCollection === TRASH_SECTION) { - collectionURL += constants.TRASH; + collectionQuery += constants.TRASH; } else { - collectionURL += activeCollection; + collectionQuery += activeCollection; } } - const href = `/gallery${collectionURL}`; - router.push(href, undefined, { shallow: true }); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { collection, ...rest } = router.query ?? {}; + + router.replace({ + pathname: PAGES.GALLERY, + query: { + ...rest, + ...(collectionQuery ? { collection: collectionQuery } : {}), + }, + }); }, [activeCollection]); const syncWithRemote = async (force = false, silent = false) => { From 97a3eef4651bba47e2a80474cdb1e7f99c3d0144 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 22 Nov 2021 22:10:05 +0530 Subject: [PATCH 04/14] preserve rest queryParam after checkSubscriptionPurchase --- src/utils/billingUtil.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/utils/billingUtil.ts b/src/utils/billingUtil.ts index 555bd9016..4a62132e9 100644 --- a/src/utils/billingUtil.ts +++ b/src/utils/billingUtil.ts @@ -10,6 +10,7 @@ import { SetLoading } from 'pages/gallery'; import { getData, LS_KEYS } from './storage/localStorage'; import { CustomError } from './common/errorUtil'; import { logError } from './sentry'; +import { PAGES } from 'types'; const STRIPE = 'stripe'; @@ -191,17 +192,14 @@ export async function checkSubscriptionPurchase( router: NextRouter, setLoading: SetLoading ) { + const { sessionId, status, reason, ...rest } = router.query ?? {}; try { - const urlParams = new URLSearchParams(window.location.search); - const sessionId = urlParams.get('session_id'); - const status = urlParams.get('status'); - const reason = urlParams.get('reason'); if (status === RESPONSE_STATUS.fail) { - handleFailureReason(reason, setDialogMessage, setLoading); + handleFailureReason(reason as string, setDialogMessage, setLoading); } else if (status === RESPONSE_STATUS.success) { try { const subscription = await billingService.verifySubscription( - sessionId + sessionId as string ); setDialogMessage({ title: constants.SUBSCRIPTION_PURCHASE_SUCCESS_TITLE, @@ -221,7 +219,9 @@ export async function checkSubscriptionPurchase( } catch (e) { // ignore } finally { - router.push('gallery', undefined, { shallow: true }); + router.replace({ pathname: PAGES.GALLERY, query: rest }, undefined, { + shallow: true, + }); } } From 4bdbc36b25a3d9c2030d4b192424426ffae28ffb Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 22 Nov 2021 22:32:30 +0530 Subject: [PATCH 05/14] fix change --- src/pages/gallery/index.tsx | 2 +- src/utils/billingUtil.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 26da721ac..8eb2eec00 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -285,8 +285,8 @@ export default function Gallery() { router.replace({ pathname: PAGES.GALLERY, query: { - ...rest, ...(collectionQuery ? { collection: collectionQuery } : {}), + ...rest, }, }); }, [activeCollection]); diff --git a/src/utils/billingUtil.ts b/src/utils/billingUtil.ts index 4a62132e9..41b5c007d 100644 --- a/src/utils/billingUtil.ts +++ b/src/utils/billingUtil.ts @@ -192,7 +192,12 @@ export async function checkSubscriptionPurchase( router: NextRouter, setLoading: SetLoading ) { - const { sessionId, status, reason, ...rest } = router.query ?? {}; + const { + session_id: sessionId, + status, + reason, + ...rest + } = router.query ?? {}; try { if (status === RESPONSE_STATUS.fail) { handleFailureReason(reason as string, setDialogMessage, setLoading); From 0c8ad4b67c285a7356597094efd68bb77e27acf5 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 22 Nov 2021 22:59:34 +0530 Subject: [PATCH 06/14] wait for router ready before checkSubscriptionPurchase --- src/pages/gallery/index.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 8eb2eec00..8e7700be5 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -235,11 +235,6 @@ export default function Gallery() { setCollections(collections); setTrash(trash); await setDerivativeState(collections, files); - await checkSubscriptionPurchase( - setDialogMessage, - router, - setLoading - ); await syncWithRemote(true); setIsFirstLoad(false); setJustSignedUp(false); @@ -266,7 +261,7 @@ export default function Gallery() { ); useEffect(() => { - if (typeof activeCollection === 'undefined') { + if (typeof activeCollection === 'undefined' || !router.isReady) { return; } let collectionQuery = ''; @@ -291,6 +286,12 @@ export default function Gallery() { }); }, [activeCollection]); + useEffect(() => { + if (router.isReady) { + checkSubscriptionPurchase(setDialogMessage, router, setLoading); + } + }, [router.isReady]); + const syncWithRemote = async (force = false, silent = false) => { if (syncInProgress.current && !force) { resync.current = true; From cb3775c690dc9e6180b7d36eebd33500b8da2be8 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 23 Nov 2021 00:01:42 +0530 Subject: [PATCH 07/14] dont need shallow update --- src/utils/billingUtil.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/billingUtil.ts b/src/utils/billingUtil.ts index 41b5c007d..ed6cce184 100644 --- a/src/utils/billingUtil.ts +++ b/src/utils/billingUtil.ts @@ -224,9 +224,7 @@ export async function checkSubscriptionPurchase( } catch (e) { // ignore } finally { - router.replace({ pathname: PAGES.GALLERY, query: rest }, undefined, { - shallow: true, - }); + router.replace({ pathname: PAGES.GALLERY, query: rest }); } } From f592a04ad2591a87ba013f4eb75bc6a15aef5752 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 23 Nov 2021 09:40:19 +0530 Subject: [PATCH 08/14] prevent router loop --- src/pages/gallery/index.tsx | 6 ++---- src/utils/billingUtil.ts | 10 +--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 8e7700be5..7e5851e7d 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -212,6 +212,7 @@ export default function Gallery() { const [fixCreationTimeView, setFixCreationTimeView] = useState(false); const [fixCreationTimeAttributes, setFixCreationTimeAttributes] = useState(null); + useEffect(() => { const key = getKey(SESSION_KEYS.ENCRYPTION_KEY); if (!key) { @@ -261,7 +262,7 @@ export default function Gallery() { ); useEffect(() => { - if (typeof activeCollection === 'undefined' || !router.isReady) { + if (typeof activeCollection === 'undefined') { return; } let collectionQuery = ''; @@ -274,14 +275,11 @@ export default function Gallery() { collectionQuery += activeCollection; } } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { collection, ...rest } = router.query ?? {}; router.replace({ pathname: PAGES.GALLERY, query: { ...(collectionQuery ? { collection: collectionQuery } : {}), - ...rest, }, }); }, [activeCollection]); diff --git a/src/utils/billingUtil.ts b/src/utils/billingUtil.ts index ed6cce184..43e1e1924 100644 --- a/src/utils/billingUtil.ts +++ b/src/utils/billingUtil.ts @@ -10,7 +10,6 @@ import { SetLoading } from 'pages/gallery'; import { getData, LS_KEYS } from './storage/localStorage'; import { CustomError } from './common/errorUtil'; import { logError } from './sentry'; -import { PAGES } from 'types'; const STRIPE = 'stripe'; @@ -192,12 +191,7 @@ export async function checkSubscriptionPurchase( router: NextRouter, setLoading: SetLoading ) { - const { - session_id: sessionId, - status, - reason, - ...rest - } = router.query ?? {}; + const { session_id: sessionId, status, reason } = router.query ?? {}; try { if (status === RESPONSE_STATUS.fail) { handleFailureReason(reason as string, setDialogMessage, setLoading); @@ -223,8 +217,6 @@ export async function checkSubscriptionPurchase( } } catch (e) { // ignore - } finally { - router.replace({ pathname: PAGES.GALLERY, query: rest }); } } From 62a761e5ca5f38f99ab0a35d8a612261b807fcfe Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 23 Nov 2021 09:45:19 +0530 Subject: [PATCH 09/14] handle missing key or token --- src/pages/gallery/index.tsx | 3 ++- src/services/billingService.ts | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 7e5851e7d..ae5f812e8 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -285,7 +285,8 @@ export default function Gallery() { }, [activeCollection]); useEffect(() => { - if (router.isReady) { + const key = getKey(SESSION_KEYS.ENCRYPTION_KEY); + if (router.isReady && key) { checkSubscriptionPurchase(setDialogMessage, router, setLoading); } }, [router.isReady]); diff --git a/src/services/billingService.ts b/src/services/billingService.ts index 630dc0f34..7c09d5fba 100644 --- a/src/services/billingService.ts +++ b/src/services/billingService.ts @@ -134,6 +134,10 @@ class billingService { sessionID: string = null ): Promise { try { + const token = getToken(); + if (!token) { + return; + } const response = await HTTPService.post( `${ENDPOINT}/billing/verify-subscription`, { @@ -143,7 +147,7 @@ class billingService { }, null, { - 'X-Auth-Token': getToken(), + 'X-Auth-Token': token, } ); const { subscription } = response.data; From 7bd3a4ad422cca33288add071c7bcdde10e0ba2c Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 23 Nov 2021 10:44:56 +0530 Subject: [PATCH 10/14] Revert "preserve other query param during collection url set" This reverts commit 4d5fae1591024caf709fe26cce722c6175ed10d2. --- src/pages/gallery/index.tsx | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index ae5f812e8..b740e9d2f 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -265,23 +265,19 @@ export default function Gallery() { if (typeof activeCollection === 'undefined') { return; } - let collectionQuery = ''; + let collectionURL = ''; if (activeCollection !== ALL_SECTION) { + collectionURL += '?collection='; if (activeCollection === ARCHIVE_SECTION) { - collectionQuery += constants.ARCHIVE; + collectionURL += constants.ARCHIVE; } else if (activeCollection === TRASH_SECTION) { - collectionQuery += constants.TRASH; + collectionURL += constants.TRASH; } else { - collectionQuery += activeCollection; + collectionURL += activeCollection; } } - - router.replace({ - pathname: PAGES.GALLERY, - query: { - ...(collectionQuery ? { collection: collectionQuery } : {}), - }, - }); + const href = `/gallery${collectionURL}`; + router.push(href, undefined, { shallow: true }); }, [activeCollection]); useEffect(() => { From e204fb2aef5908addd25988e90a811231dd2c059 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 11 Nov 2021 14:55:41 +0530 Subject: [PATCH 11/14] fix multiple folder detection --- src/components/pages/gallery/Upload.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index 7d4095f0a..9482fc5b7 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -134,7 +134,8 @@ export default function Upload(props: Props) { return null; } const paths: string[] = props.acceptedFiles.map((file) => file['path']); - paths.sort(); + const getCharCount = (str: string) => (str.match(/\//g) ?? []).length; + paths.sort((path1, path2) => getCharCount(path1) - getCharCount(path2)); const firstPath = paths[0]; const lastPath = paths[paths.length - 1]; const L = firstPath.length; @@ -154,6 +155,7 @@ export default function Upload(props: Props) { ); } } + console.log(firstFileFolder, lastFileFolder, paths); return { suggestedCollectionName: commonPathPrefix, multipleFolders: firstFileFolder !== lastFileFolder, From 18878ec240f2e252819b9bc77784ecb661e364e1 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 12 Nov 2021 10:21:57 +0530 Subject: [PATCH 12/14] remove console logs --- src/components/pages/gallery/Upload.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index 9482fc5b7..df694188b 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -155,7 +155,6 @@ export default function Upload(props: Props) { ); } } - console.log(firstFileFolder, lastFileFolder, paths); return { suggestedCollectionName: commonPathPrefix, multipleFolders: firstFileFolder !== lastFileFolder, From ca86255bd3ae9fcb0c00bb5ff8a8333643c3efdb Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 23 Nov 2021 17:52:32 +0530 Subject: [PATCH 13/14] use app context instead of locl storage key --- src/pages/_app.tsx | 5 +++++ src/pages/credentials/index.tsx | 13 ++++--------- src/pages/gallery/index.tsx | 3 +-- src/utils/storage/localStorage.ts | 1 - 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 8ba26963f..5d170715b 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -479,6 +479,8 @@ type AppContextType = { sharedFiles: File[]; resetSharedFiles: () => void; setDisappearingFlashMessage: (message: FlashMessage) => void; + redirectUrl: string; + setRedirectUrl: (url: string) => void; }; export enum FLASH_MESSAGE_TYPE { @@ -508,6 +510,7 @@ export default function App({ Component, err }) { const [sharedFiles, setSharedFiles] = useState(null); const [redirectName, setRedirectName] = useState(null); const [flashMessage, setFlashMessage] = useState(null); + const [redirectUrl, setRedirectUrl] = useState(null); useEffect(() => { if ( !('serviceWorker' in navigator) || @@ -641,6 +644,8 @@ export default function App({ Component, err }) { sharedFiles, resetSharedFiles, setDisappearingFlashMessage, + redirectUrl, + setRedirectUrl, }}> {loading ? ( diff --git a/src/pages/credentials/index.tsx b/src/pages/credentials/index.tsx index 7a91bd9e8..fff9be33f 100644 --- a/src/pages/credentials/index.tsx +++ b/src/pages/credentials/index.tsx @@ -1,12 +1,7 @@ import React, { useContext, useEffect, useState } from 'react'; import constants from 'utils/strings/constants'; -import { - clearData, - getData, - LS_KEYS, - setData, -} from 'utils/storage/localStorage'; +import { clearData, getData, LS_KEYS } from 'utils/storage/localStorage'; import { useRouter } from 'next/router'; import { KeyAttributes, PAGES } from 'types'; import { SESSION_KEYS, getKey } from 'utils/storage/sessionStorage'; @@ -80,9 +75,9 @@ export default function Credentials() { } await SaveKeyInSessionStore(SESSION_KEYS.ENCRYPTION_KEY, key); await decryptAndStoreToken(key); - const redirectURL = getData(LS_KEYS.REDIRECT)?.url; - setData(LS_KEYS.REDIRECT, null); - router.push(redirectURL ? redirectURL : PAGES.GALLERY); + const redirectUrl = appContext.redirectUrl; + appContext.setRedirectUrl(null); + router.push(redirectUrl ?? PAGES.GALLERY); } catch (e) { logError(e, 'user entered a wrong password'); setFieldError('passphrase', constants.INCORRECT_PASSPHRASE); diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index b740e9d2f..1b2372cb0 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -97,7 +97,6 @@ import DeleteBtn from 'components/DeleteBtn'; import FixCreationTime, { FixCreationTimeAttributes, } from 'components/FixCreationTime'; -import { LS_KEYS, setData } from 'utils/storage/localStorage'; export const DeadCenter = styled.div` flex: 1; @@ -216,7 +215,7 @@ export default function Gallery() { useEffect(() => { const key = getKey(SESSION_KEYS.ENCRYPTION_KEY); if (!key) { - setData(LS_KEYS.REDIRECT, { url: router.asPath }); + appContext.setRedirectUrl(router.asPath); router.push(PAGES.ROOT); return; } diff --git a/src/utils/storage/localStorage.ts b/src/utils/storage/localStorage.ts index 2d23aff69..fddfe119e 100644 --- a/src/utils/storage/localStorage.ts +++ b/src/utils/storage/localStorage.ts @@ -13,7 +13,6 @@ export enum LS_KEYS { EXPORT = 'export', AnonymizeUserID = 'anonymizedUserID', THUMBNAIL_FIX_STATE = 'thumbnailFixState', - REDIRECT = 'redirect', } export const setData = (key: LS_KEYS, value: object) => { From 7f8d2b84c80f2e9d72397f8c9a8e1aa463e42e03 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 25 Nov 2021 11:28:16 +0530 Subject: [PATCH 14/14] clear failed ffmpeg instance..so new load is trigerred --- src/services/ffmpegService.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/services/ffmpegService.ts b/src/services/ffmpegService.ts index 2b4bf2970..1184593b4 100644 --- a/src/services/ffmpegService.ts +++ b/src/services/ffmpegService.ts @@ -19,6 +19,8 @@ class FFmpegService { this.isLoading = null; } catch (e) { logError(e, 'ffmpeg load failed'); + this.ffmpeg = null; + this.isLoading = null; throw e; } }