fix redirection issues

This commit is contained in:
mrjvs 2023-06-21 21:35:25 +02:00
parent 394271857f
commit f892a3037f
2 changed files with 104 additions and 102 deletions

View File

@ -201,29 +201,33 @@ export function decodeTMDBId(
}; };
} }
export function isLegacyUrl(url: string): boolean {
if (url.startsWith("/media/JW")) return true;
return false;
}
export async function convertLegacyUrl( export async function convertLegacyUrl(
url: string url: string
): Promise<string | undefined> { ): Promise<string | undefined> {
if (url.startsWith("/media/JW")) { if (!isLegacyUrl(url)) return undefined;
const urlParts = url.split("/").slice(2);
const [, type, id] = urlParts[0].split("-", 3);
const mediaType = TMDBMediaToMediaType(type); const urlParts = url.split("/").slice(2);
const meta = await getLegacyMetaFromId(mediaType, id); const [, type, id] = urlParts[0].split("-", 3);
if (!meta) return undefined; const mediaType = TMDBMediaToMediaType(type);
const { tmdbId, imdbId } = meta; const meta = await getLegacyMetaFromId(mediaType, id);
if (!tmdbId && !imdbId) return undefined;
// movies always have an imdb id on tmdb if (!meta) return undefined;
if (imdbId && mediaType === MWMediaType.MOVIE) { const { tmdbId, imdbId } = meta;
const movieId = await getMovieFromExternalId(imdbId); if (!tmdbId && !imdbId) return undefined;
if (movieId) return `/media/tmdb-movie-${movieId}`;
}
if (tmdbId) { // movies always have an imdb id on tmdb
return `/media/tmdb-${type}-${tmdbId}`; if (imdbId && mediaType === MWMediaType.MOVIE) {
} const movieId = await getMovieFromExternalId(imdbId);
if (movieId) return `/media/tmdb-movie-${movieId}`;
}
if (tmdbId) {
return `/media/tmdb-${type}-${tmdbId}`;
} }
return undefined;
} }

View File

@ -1,7 +1,13 @@
import { lazy, useEffect, useState } from "react"; import { ReactElement, lazy, useEffect } from "react";
import { Redirect, Route, Switch, useLocation } from "react-router-dom"; import {
Redirect,
Route,
Switch,
useHistory,
useLocation,
} from "react-router-dom";
import { convertLegacyUrl } from "@/backend/metadata/getmeta"; import { convertLegacyUrl, isLegacyUrl } from "@/backend/metadata/getmeta";
import { MWMediaType } from "@/backend/metadata/types/mw"; import { MWMediaType } from "@/backend/metadata/types/mw";
import { BannerContextProvider } from "@/hooks/useBanner"; import { BannerContextProvider } from "@/hooks/useBanner";
import { Layout } from "@/setup/Layout"; import { Layout } from "@/setup/Layout";
@ -13,27 +19,21 @@ import { NotFoundPage } from "@/views/notfound/NotFoundView";
import { V2MigrationView } from "@/views/other/v2Migration"; import { V2MigrationView } from "@/views/other/v2Migration";
import { SearchView } from "@/views/search/SearchView"; import { SearchView } from "@/views/search/SearchView";
// eslint-disable-next-line react/function-component-definition, react/prop-types function LegacyUrlView({ children }: { children: ReactElement }) {
const LegacyUrlView: React.FC = ({ children }) => {
const location = useLocation(); const location = useLocation();
const [redirectUrl, setRedirectUrl] = useState<string | null>(null); const { replace } = useHistory();
useEffect(() => { useEffect(() => {
// Call the conversion function and set the redirect URL if necessary const url = location.pathname;
if (!isLegacyUrl(url)) return;
convertLegacyUrl(location.pathname).then((convertedUrl) => { convertLegacyUrl(location.pathname).then((convertedUrl) => {
if (convertedUrl) { replace(convertedUrl ?? "/");
setRedirectUrl(convertedUrl);
}
}); });
}, [location.pathname]); }, [location.pathname, replace]);
if (redirectUrl) { if (isLegacyUrl(location.pathname)) return null;
return <Redirect to={redirectUrl} />; return children;
} }
// eslint-disable-next-line react/jsx-no-useless-fragment
return <>{children}</>;
};
function App() { function App() {
return ( return (
@ -42,76 +42,74 @@ function App() {
<BookmarkContextProvider> <BookmarkContextProvider>
<BannerContextProvider> <BannerContextProvider>
<Layout> <Layout>
<LegacyUrlView> <Switch>
<Switch> {/* functional routes */}
{/* functional routes */} <Route exact path="/v2-migration" component={V2MigrationView} />
<Route <Route exact path="/">
exact <Redirect to={`/search/${MWMediaType.MOVIE}`} />
path="/v2-migration" </Route>
component={V2MigrationView}
/>
<Route exact path="/">
<Redirect to={`/search/${MWMediaType.MOVIE}`} />
</Route>
{/* pages */} {/* pages */}
<Route exact path="/media/:media" component={MediaView} /> <Route exact path="/media/:media">
<Route <LegacyUrlView>
exact <MediaView />
path="/media/:media/:season/:episode" </LegacyUrlView>
component={MediaView} </Route>
/> <Route exact path="/media/:media/:season/:episode">
<Route <LegacyUrlView>
exact <MediaView />
path="/search/:type/:query?" </LegacyUrlView>
component={SearchView} </Route>
/> <Route
exact
path="/search/:type/:query?"
component={SearchView}
/>
{/* other */} {/* other */}
<Route <Route
exact exact
path="/dev" path="/dev"
component={lazy( component={lazy(
() => import("@/views/developer/DeveloperView") () => import("@/views/developer/DeveloperView")
)} )}
/> />
<Route <Route
exact exact
path="/dev/video" path="/dev/video"
component={lazy( component={lazy(
() => import("@/views/developer/VideoTesterView") () => import("@/views/developer/VideoTesterView")
)} )}
/> />
{/* developer routes that can abuse workers are disabled in production */} {/* developer routes that can abuse workers are disabled in production */}
{process.env.NODE_ENV === "development" ? ( {process.env.NODE_ENV === "development" ? (
<> <>
<Route <Route
exact exact
path="/dev/test" path="/dev/test"
component={lazy( component={lazy(
() => import("@/views/developer/TestView") () => import("@/views/developer/TestView")
)} )}
/> />
<Route <Route
exact exact
path="/dev/providers" path="/dev/providers"
component={lazy( component={lazy(
() => import("@/views/developer/ProviderTesterView") () => import("@/views/developer/ProviderTesterView")
)} )}
/> />
<Route <Route
exact exact
path="/dev/embeds" path="/dev/embeds"
component={lazy( component={lazy(
() => import("@/views/developer/EmbedTesterView") () => import("@/views/developer/EmbedTesterView")
)} )}
/> />
</> </>
) : null} ) : null}
<Route path="*" component={NotFoundPage} /> <Route path="*" component={NotFoundPage} />
</Switch> </Switch>
</LegacyUrlView>
</Layout> </Layout>
</BannerContextProvider> </BannerContextProvider>
</BookmarkContextProvider> </BookmarkContextProvider>