Skip to content

best way to save what you love

web/DownloadButton: move server info cache checks to the api lib

wukkodfaef913c4da
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 = {}