From 883f39e4b50b8a05add792fa55e1c34a18f27d94 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 19 Dec 2023 14:09:21 +0100 Subject: [PATCH] Add basic download support on the web --- .../ui/src/components/context-menus.tsx | 2 +- front/packages/ui/src/downloads/index.tsx | 2 +- front/packages/ui/src/downloads/index.web.tsx | 48 +++++++++++++++++++ front/packages/ui/src/downloads/state.web.tsx | 0 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 front/packages/ui/src/downloads/index.web.tsx delete mode 100644 front/packages/ui/src/downloads/state.web.tsx diff --git a/front/packages/ui/src/components/context-menus.tsx b/front/packages/ui/src/components/context-menus.tsx index 80ae18eb..ac3aa9c4 100644 --- a/front/packages/ui/src/components/context-menus.tsx +++ b/front/packages/ui/src/components/context-menus.tsx @@ -27,7 +27,7 @@ import Download from "@material-symbols/svg-400/rounded/download.svg"; import { WatchStatusV, queryFn, useAccount } from "@kyoo/models"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { watchListIcon } from "./watchlist-info"; -import { useDownloader } from "../downloads/state"; +import { useDownloader } from "../downloads"; import { Platform } from "react-native"; import { useYoshiki } from "yoshiki/native"; diff --git a/front/packages/ui/src/downloads/index.tsx b/front/packages/ui/src/downloads/index.tsx index 39485cc1..d662c71e 100644 --- a/front/packages/ui/src/downloads/index.tsx +++ b/front/packages/ui/src/downloads/index.tsx @@ -19,4 +19,4 @@ */ export { DownloadPage } from "./page"; -export { DownloadProvider } from "./state"; +export { DownloadProvider, useDownloader } from "./state"; diff --git a/front/packages/ui/src/downloads/index.web.tsx b/front/packages/ui/src/downloads/index.web.tsx new file mode 100644 index 00000000..5ec26b5c --- /dev/null +++ b/front/packages/ui/src/downloads/index.web.tsx @@ -0,0 +1,48 @@ +/* + * Kyoo - A portable and vast media library solution. + * Copyright (c) Kyoo. + * + * See AUTHORS.md and LICENSE file in the project root for full license information. + * + * Kyoo is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * Kyoo is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Kyoo. If not, see . + */ + +import { WatchInfo, kyooApiUrl, queryFn, toQueryKey } from "@kyoo/models"; +import { Player } from "../player"; +import { getCurrentAccount } from "@kyoo/models/src/account-internal"; + +export const useDownloader = () => { + return async (type: "episode" | "movie", slug: string) => { + const account = getCurrentAccount(); + const query = Player.infoQuery(type, slug); + const info: WatchInfo = await queryFn( + { + queryKey: toQueryKey(query), + signal: undefined as any, + meta: undefined, + apiUrl: account?.apiUrl, + }, + query.parser, + account?.token.access_token, + ); + + // TODO: This methods does not work with auth. + const a = document.createElement("a"); + a.style.display = "none"; + a.href = `${kyooApiUrl}/video/${type}/${slug}/direct`; + a.download = `${slug}.${info.extension}`; + document.body.appendChild(a); + a.click(); + }; +}; diff --git a/front/packages/ui/src/downloads/state.web.tsx b/front/packages/ui/src/downloads/state.web.tsx deleted file mode 100644 index e69de29b..00000000