refactor: rename of the http downloader to real debrid downloader given that it only supports downloading games through real debrid

This commit is contained in:
lilezek 2024-05-06 10:53:18 +02:00
parent 73c65640e1
commit 9cd8188f06
6 changed files with 40 additions and 41 deletions

View file

@ -26,7 +26,7 @@ const startGameDownload = async (
});
const downloader = userPreferences?.realDebridApiToken
? Downloader.Http
? Downloader.RealDebrid
: Downloader.Torrent;
const [game, repack] = await Promise.all([

View file

@ -4,7 +4,7 @@ import type { Game } from "@main/entity";
import { Downloader } from "@shared";
import { writePipe } from "./fifo";
import { HTTPDownloader } from "./downloaders";
import { RealDebridDownloader } from "./downloaders";
export class DownloadManager {
private static gameDownloading: Game;
@ -25,7 +25,7 @@ export class DownloadManager {
) {
writePipe.write({ action: "cancel" });
} else {
HTTPDownloader.destroy();
RealDebridDownloader.destroy();
}
}
@ -36,7 +36,7 @@ export class DownloadManager {
) {
writePipe.write({ action: "pause" });
} else {
HTTPDownloader.destroy();
RealDebridDownloader.destroy();
}
}
@ -51,7 +51,7 @@ export class DownloadManager {
save_path: game!.downloadPath,
});
} else {
HTTPDownloader.startDownload(game!);
RealDebridDownloader.startDownload(game!);
}
this.gameDownloading = game!;
@ -68,7 +68,7 @@ export class DownloadManager {
save_path: game!.downloadPath,
});
} else {
HTTPDownloader.startDownload(game!);
RealDebridDownloader.startDownload(game!);
}
this.gameDownloading = game!;

View file

@ -1,2 +1,2 @@
export * from "./http.downloader";
export * from "./real-debrid.downloader";
export * from "./torrent.downloader";

View file

@ -7,7 +7,7 @@ import { GameStatus } from "@shared";
import { Downloader } from "./downloader";
import { RealDebridClient } from "../real-debrid";
export class HTTPDownloader extends Downloader {
export class RealDebridDownloader extends Downloader {
private static download: EasyDL;
private static downloadSize = 0;
@ -21,43 +21,14 @@ export class HTTPDownloader extends Downloader {
return 1;
}
static async getDownloadUrl(game: Game) {
const torrents = await RealDebridClient.getAllTorrentsFromUser();
const hash = RealDebridClient.extractSHA1FromMagnet(game!.repack.magnet);
let torrent = torrents.find((t) => t.hash === hash);
if (!torrent) {
const magnet = await RealDebridClient.addMagnet(game!.repack.magnet);
if (magnet && magnet.id) {
await RealDebridClient.selectAllFiles(magnet.id);
torrent = await RealDebridClient.getInfo(magnet.id);
}
}
if (torrent) {
const { links } = torrent;
const { download } = await RealDebridClient.unrestrictLink(links[0]);
if (!download) {
throw new Error("Torrent not cached on Real Debrid");
}
return download;
}
throw new Error();
}
static async startDownload(game: Game) {
if (this.download) this.download.destroy();
const download = await this.getDownloadUrl(game);
const downloadUrl = await RealDebridClient.getDownloadUrl(game);
this.download = new EasyDL(
download,
path.join(game.downloadPath!, game.repack.title)
downloadUrl,
path.join(game.downloadPath!, ".rd")
);
const metadata = await this.download.metadata();
this.downloadSize = metadata.size;

View file

@ -62,6 +62,34 @@ export class RealDebridClient {
return magnet.match(/btih:([0-9a-fA-F]*)/)?.[1].toLowerCase();
}
static async getDownloadUrl(game: Game) {
const torrents = await RealDebridClient.getAllTorrentsFromUser();
const hash = RealDebridClient.extractSHA1FromMagnet(game!.repack.magnet);
let torrent = torrents.find((t) => t.hash === hash);
if (!torrent) {
const magnet = await RealDebridClient.addMagnet(game!.repack.magnet);
if (magnet && magnet.id) {
await RealDebridClient.selectAllFiles(magnet.id);
torrent = await RealDebridClient.getInfo(magnet.id);
}
}
if (torrent) {
const { links } = torrent;
const { download } = await RealDebridClient.unrestrictLink(links[0]);
if (!download) {
throw new Error("Torrent not cached on Real Debrid");
}
return download;
}
throw new Error();
}
static async authorize(apiToken: string) {
this.instance = axios.create({
baseURL: base,

View file

@ -9,7 +9,7 @@ export enum GameStatus {
}
export enum Downloader {
Http,
RealDebrid,
Torrent,
}