From dfaef913c4da711b4c58d450808026470dab156e Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 22 Sep 2024 15:05:40 +0600 Subject: [PATCH] web/DownloadButton: move server info cache checks to the api lib --- .../save/buttons/DownloadButton.svelte | 23 ---------------- web/src/lib/api/api.ts | 27 +++++++++++++++++-- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/web/src/components/save/buttons/DownloadButton.svelte b/web/src/components/save/buttons/DownloadButton.svelte index 6437958e..e8d24f0f 100644 --- a/web/src/components/save/buttons/DownloadButton.svelte +++ b/web/src/components/save/buttons/DownloadButton.svelte @@ -6,9 +6,6 @@ import { createDialog } from "$lib/dialogs"; import { downloadFile } from "$lib/download"; - import { cachedInfo, getServerInfo } from "$lib/api/server-info"; - import { turnstileLoaded } from "$lib/state/turnstile"; - import type { DialogInfo } from "$lib/types/dialog"; export let url: string; @@ -66,26 +63,6 @@ export const download = async (link: string) => { changeDownloadButton("think"); - await getServerInfo(); - - if (!$cachedInfo) { - changeDownloadButton("error"); - - return createDialog({ - ...defaultErrorPopup, - bodyText: $t("error.api.unreachable"), - }); - } - - if ($cachedInfo?.info?.cobalt?.turnstileSitekey && !$turnstileLoaded) { - changeDownloadButton("error"); - - return createDialog({ - ...defaultErrorPopup, - bodyText: $t("error.captcha_ongoing"), - }); - } - const response = await API.request(link); if (!response) { diff --git a/web/src/lib/api/api.ts b/web/src/lib/api/api.ts index 69ed0691..19f27c94 100644 --- a/web/src/lib/api/api.ts +++ b/web/src/lib/api/api.ts @@ -5,8 +5,9 @@ import lazySettingGetter from "$lib/settings/lazy-get"; import { getSession } from "$lib/api/session"; import { currentApiURL } from "$lib/api/api-url"; -import { cachedInfo } from "$lib/api/server-info"; +import { turnstileLoaded } from "$lib/state/turnstile"; import { apiOverrideWarning } from "$lib/api/safety-warning"; +import { cachedInfo, getServerInfo } from "$lib/api/server-info"; import type { Optional } from "$lib/types/generic"; import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api"; @@ -37,9 +38,31 @@ const request = async (url: string) => { await apiOverrideWarning(); + await getServerInfo(); + + const getCachedInfo = get(cachedInfo); + + if (!getCachedInfo) { + return { + status: "error", + error: { + code: "error.api.unreachable" + } + } as CobaltErrorResponse; + } + + if (getCachedInfo?.info?.cobalt?.turnstileSitekey && !get(turnstileLoaded)) { + return { + status: "error", + error: { + code: "error.captcha_ongoing" + } + } as CobaltErrorResponse; + } + const api = currentApiURL(); - const session = get(cachedInfo)?.info?.cobalt?.turnstileSitekey + const session = getCachedInfo?.info?.cobalt?.turnstileSitekey ? await getSession() : undefined; let extraHeaders = {}