diff --git a/src/backend/metadata/getmeta.ts b/src/backend/metadata/getmeta.ts index 26464299..b6f90f26 100644 --- a/src/backend/metadata/getmeta.ts +++ b/src/backend/metadata/getmeta.ts @@ -163,3 +163,18 @@ export function decodeMWId( id, }; } + +export async function convertLegacyUrl( + url: string +): Promise { + if (url.startsWith("/media/JW")) { + const urlParts = url.split("/").slice(2); + const [, type, id] = urlParts[0].split("-", 3); + const meta = await getLegacyMetaFromId(TTVMediaToMediaType(type), id); + if (!meta) return undefined; + const tmdbId = meta.tmdbId; + if (!tmdbId) return undefined; + return `/media/MW-${type}-${tmdbId}`; + } + return undefined; +} diff --git a/src/setup/App.tsx b/src/setup/App.tsx index 992549e0..7be4d581 100644 --- a/src/setup/App.tsx +++ b/src/setup/App.tsx @@ -1,6 +1,13 @@ import { lazy } from "react"; -import { Redirect, Route, Switch } from "react-router-dom"; +import { + Redirect, + Route, + Switch, + useHistory, + useLocation, +} from "react-router-dom"; +import { convertLegacyUrl } from "@/backend/metadata/getmeta"; import { MWMediaType } from "@/backend/metadata/types"; import { BannerContextProvider } from "@/hooks/useBanner"; import { Layout } from "@/setup/Layout"; @@ -13,6 +20,15 @@ import { V2MigrationView } from "@/views/other/v2Migration"; import { SearchView } from "@/views/search/SearchView"; function App() { + const location = useLocation(); + const history = useHistory(); + + // Call the conversion function and redirect if necessary + convertLegacyUrl(location.pathname).then((convertedUrl) => { + if (convertedUrl) { + history.replace(convertedUrl); + } + }); return (