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

This commit is contained in:
wukko 2024-09-22 15:05:40 +06:00
parent 5ba83f3d56
commit dfaef913c4
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 25 additions and 25 deletions

View file

@ -6,9 +6,6 @@
import { createDialog } from "$lib/dialogs"; import { createDialog } from "$lib/dialogs";
import { downloadFile } from "$lib/download"; 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"; import type { DialogInfo } from "$lib/types/dialog";
export let url: string; export let url: string;
@ -66,26 +63,6 @@
export const download = async (link: string) => { export const download = async (link: string) => {
changeDownloadButton("think"); 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); const response = await API.request(link);
if (!response) { if (!response) {

View file

@ -5,8 +5,9 @@ import lazySettingGetter from "$lib/settings/lazy-get";
import { getSession } from "$lib/api/session"; import { getSession } from "$lib/api/session";
import { currentApiURL } from "$lib/api/api-url"; 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 { apiOverrideWarning } from "$lib/api/safety-warning";
import { cachedInfo, getServerInfo } from "$lib/api/server-info";
import type { Optional } from "$lib/types/generic"; import type { Optional } from "$lib/types/generic";
import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api"; import type { CobaltAPIResponse, CobaltErrorResponse } from "$lib/types/api";
@ -37,9 +38,31 @@ const request = async (url: string) => {
await apiOverrideWarning(); 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 api = currentApiURL();
const session = get(cachedInfo)?.info?.cobalt?.turnstileSitekey const session = getCachedInfo?.info?.cobalt?.turnstileSitekey
? await getSession() : undefined; ? await getSession() : undefined;
let extraHeaders = {} let extraHeaders = {}