chore: adding download sorting

This commit is contained in:
Chubby Granny Chaser
2025-02-01 23:52:12 +00:00
parent 7fed104afd
commit 7e54a6d0a9
15 changed files with 141 additions and 66 deletions

View File

@@ -1,11 +1,12 @@
import { registerEvent } from "../register-event";
import type { Download, StartGameDownloadPayload } from "@types";
import { DownloadManager, HydraApi } from "@main/services";
import { DownloadManager, HydraApi, logger } from "@main/services";
import { steamGamesWorker } from "@main/workers";
import { createGame } from "@main/services/library-sync";
import { steamUrlBuilder } from "@shared";
import { Downloader, DownloadError, steamUrlBuilder } from "@shared";
import { downloadsSublevel, gamesSublevel, levelKeys } from "@main/level";
import { AxiosError } from "axios";
const startGameDownload = async (
_event: Electron.IpcMainInvokeEvent,
@@ -77,21 +78,49 @@ const startGameDownload = async (
await downloadsSublevel.put(gameKey, download);
await DownloadManager.startDownload(download);
try {
await DownloadManager.startDownload(download);
const updatedGame = await gamesSublevel.get(gameKey);
const updatedGame = await gamesSublevel.get(gameKey);
await Promise.all([
createGame(updatedGame!).catch(() => {}),
HydraApi.post(
"/games/download",
{
objectId,
shop,
},
{ needsAuth: false }
).catch(() => {}),
]);
await Promise.all([
createGame(updatedGame!).catch(() => {}),
HydraApi.post(
"/games/download",
{
objectId,
shop,
},
{ needsAuth: false }
).catch(() => {}),
]);
return { ok: true };
} catch (err: unknown) {
logger.error("Failed to start download", err);
if (err instanceof AxiosError) {
if (err.response?.status === 429 && downloader === Downloader.Gofile) {
return { ok: false, error: DownloadError.GofileQuotaExceeded };
}
if (
err.response?.status === 403 &&
downloader === Downloader.RealDebrid
) {
return {
ok: false,
error: DownloadError.RealDebridAccountNotAuthorized,
};
}
}
if (err instanceof Error) {
return { ok: false, error: err.message };
}
return { ok: false };
}
};
registerEvent("startGameDownload", startGameDownload);

View File

@@ -1,4 +1,4 @@
import { Downloader } from "@shared";
import { Downloader, DownloadError } from "@shared";
import { WindowManager } from "../window-manager";
import { publishDownloadCompleteNotification } from "../notifications";
import type { Download, DownloadProgress, UserPreferences } from "@types";
@@ -263,6 +263,8 @@ export class DownloadManager {
const token = await GofileApi.authorize();
const downloadLink = await GofileApi.getDownloadLink(id!);
await GofileApi.checkDownloadUrl(downloadLink);
return {
action: "start",
game_id: downloadId,
@@ -318,10 +320,7 @@ export class DownloadManager {
case Downloader.RealDebrid: {
const downloadUrl = await RealDebridClient.getDownloadUrl(download.uri);
if (!downloadUrl)
throw new Error(
"This download is not available on Real-Debrid and polling download status from Real-Debrid is not yet available."
);
if (!downloadUrl) throw new Error(DownloadError.NotCachedInRealDebrid);
return {
action: "start",

View File

@@ -60,4 +60,12 @@ export class GofileApi {
throw new Error("Failed to get download link");
}
public static async checkDownloadUrl(url: string) {
return axios.head(url, {
headers: {
Cookie: `accountToken=${this.token}`,
},
});
}
}