2024-05-05 21:18:48 +03:00
|
|
|
import { gameRepository } from "@main/repository";
|
|
|
|
|
|
|
|
import type { Game } from "@main/entity";
|
|
|
|
import { Downloader } from "@shared";
|
|
|
|
|
|
|
|
import { writePipe } from "./fifo";
|
2024-05-06 11:53:18 +03:00
|
|
|
import { RealDebridDownloader } from "./downloaders";
|
2024-05-05 21:18:48 +03:00
|
|
|
|
|
|
|
export class DownloadManager {
|
|
|
|
private static gameDownloading: Game;
|
|
|
|
|
|
|
|
static async getGame(gameId: number) {
|
|
|
|
return gameRepository.findOne({
|
|
|
|
where: { id: gameId, isDeleted: false },
|
|
|
|
relations: {
|
|
|
|
repack: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async cancelDownload() {
|
|
|
|
if (
|
|
|
|
this.gameDownloading &&
|
|
|
|
this.gameDownloading.downloader === Downloader.Torrent
|
|
|
|
) {
|
|
|
|
writePipe.write({ action: "cancel" });
|
|
|
|
} else {
|
2024-05-06 11:53:18 +03:00
|
|
|
RealDebridDownloader.destroy();
|
2024-05-05 21:18:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async pauseDownload() {
|
|
|
|
if (
|
|
|
|
this.gameDownloading &&
|
|
|
|
this.gameDownloading.downloader === Downloader.Torrent
|
|
|
|
) {
|
|
|
|
writePipe.write({ action: "pause" });
|
|
|
|
} else {
|
2024-05-06 11:53:18 +03:00
|
|
|
RealDebridDownloader.destroy();
|
2024-05-05 21:18:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async resumeDownload(gameId: number) {
|
|
|
|
const game = await this.getGame(gameId);
|
|
|
|
|
|
|
|
if (game!.downloader === Downloader.Torrent) {
|
|
|
|
writePipe.write({
|
|
|
|
action: "start",
|
|
|
|
game_id: game!.id,
|
|
|
|
magnet: game!.repack.magnet,
|
|
|
|
save_path: game!.downloadPath,
|
|
|
|
});
|
|
|
|
} else {
|
2024-05-06 11:53:18 +03:00
|
|
|
RealDebridDownloader.startDownload(game!);
|
2024-05-05 21:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.gameDownloading = game!;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async downloadGame(gameId: number) {
|
|
|
|
const game = await this.getGame(gameId);
|
|
|
|
|
|
|
|
if (game!.downloader === Downloader.Torrent) {
|
|
|
|
writePipe.write({
|
|
|
|
action: "start",
|
|
|
|
game_id: game!.id,
|
|
|
|
magnet: game!.repack.magnet,
|
|
|
|
save_path: game!.downloadPath,
|
|
|
|
});
|
|
|
|
} else {
|
2024-05-06 11:53:18 +03:00
|
|
|
RealDebridDownloader.startDownload(game!);
|
2024-05-05 21:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.gameDownloading = game!;
|
|
|
|
}
|
|
|
|
}
|